+ 1

Your task is to rotate given matrix on 90 degree counterclockwise.

What is wrong? #include <iostream> using namespace std; int main() { int a[100][100]; int n,m; cin>>n>>m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } for(int j=0;j<m;j++){ for(int i=n-1;i>=0;i--){ cout<<a[i][j]; }cout<<endl; } return 0; }

3rd Oct 2020, 9:58 AM
Azat Malgazhdar
Azat Malgazhdar - avatar
1 Answer
+ 1
Example Input 1 2 3 4 5 6 7 8 9 Your Output 7 4 1 8 5 2 9 6 3 Expected Output 3 6 9 2 5 8 1 4 7 The problem is for(int j=0;j<m;j++){ for(int i=n-1;i>=0;i--){ cout<<a[i][j]; }cout<<endl; } Correct one for(int j=m-1;j>=0;--j){ for(int i=0;i<n;++i){ cout<<a[i][j]<<" "; }cout<<endl; } Just try to dry run your code first, most of the time we find the flaw during that phase, I hope this helps.
3rd Oct 2020, 10:49 AM
Adarsh Kunwar
Adarsh Kunwar - avatar