Reputation: 31
I tried using this code but I'm unable to get the required output. I gave a 3*3 matrix as input 1 2 3|4 5 6| 7 8 9 and resultant matrix expected is 7 4 1 | 8 5 6 | 9 6 3 .
#include <bits/stdc++.h>
using namespace std;
int main(){
int size;
cin>>size;
int matrix[size][size];
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
cin>>matrix[i][j];
}
}
cout<<"Original Matrix"<<endl;
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
cout<<"After left rotating"<<endl;
int tempMatrix[size][size];
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
tempMatrix[i][j]=matrix[j][i];
}
}
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
cout<<tempMatrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Upvotes: 1
Views: 45
Reputation: 21
#include <bits/stdc++.h>
using namespace std;
int main()
{
int size;
cin>>size;
int matrix[size][size];
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin>>matrix[i][j];
}
}
cout<<"Original Matrix"<<endl;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
cout<<"After left rotating"<<endl;
int tempMatrix[size][size];
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
tempMatrix[i][j]=matrix[size-j-1][i];
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<tempMatrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Hope this works. Thanks.
Upvotes: 2