Skip to main content

Posts

Showing posts with the label Print Diagonally

Print Diagonally

Task: Given a N*N matrix, print the values of matrix in anti diagonal way. Example: Input: 1 1 2 3 4 5 6  7 8 9 Output: 1 2 4 5 6 7 6 8 9 // 1-2,4-3,5,7-6,8-9. Here is the Code: Program in C++: #include<iostream> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n][n];     for(int i=0;i<n;i++)     {         for(int j=0;j<n;j++)         {             cin>>a[i][j];         }     }     int i,k;         for(int j=0;j<n;j++)         {             i=0,k=j;             while(k>=0)             {                 cout<<a[i++][k--]<<" ";             }         }         int j;         for(i=1;i<n;i++)         {             j=n-1,k=i;             while(k<n)             {                 cout<<a[k++][j--]<<" ";             }         }         cout<<endl; } return 0; } Here is the Video: