Advertisement
wnan42

7/24/12reverseDiagonal

Jul 24th, 2012
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int MAX_ROW=20;
  4.  
  5. void swap (int &a, int &b)
  6. {
  7.      int t=a;
  8.      a=b;
  9.      b=t;
  10. }
  11. void reverseDiagonal(int matrix[MAX_ROW][MAX_ROW], int row)
  12. {
  13.      for(int i=0;i<row/2;i++)
  14.      {
  15.              swap(matrix[i][i],matrix[row-i-1][row-i-1]);
  16.              swap(matrix[row-i-1][i],matrix[i][row-i-1]);
  17.      }
  18. }
  19. void printMatrix(int matrix[MAX_ROW][MAX_ROW], int row)
  20. {
  21.      for(int i=0;i<row;i++)
  22.      {
  23.              for(int j=0;j<row;j++)
  24.              {
  25.                      cout<<matrix[i][j]<< " ";
  26.              }
  27.              cout<<endl;
  28.      }
  29. }
  30.  
  31. int main(){
  32.     cout<<"First case: "<<endl;
  33.     int myMatrix[MAX_ROW][MAX_ROW]={{96, 26 ,31 ,81},{41, 71, 66, 56},{61, 51, 46, 76},{36, 86, 91, 21}};
  34.     printMatrix(myMatrix, 4);
  35.     reverseDiagonal(myMatrix, 4);
  36.     cout<<"After reverse Diagonal: "<<endl;
  37.     printMatrix(myMatrix, 4);
  38.    
  39.     cout<<"Second case: "<<endl;
  40.     int myMatrix2[MAX_ROW][MAX_ROW]={{1, 2 ,3 ,4,5},{1, 2 ,3 ,4,5},{1, 2 ,3 ,4,5},{1, 2 ,3 ,4,5},{1, 2 ,3 ,4,5}};
  41.      printMatrix(myMatrix2, 5);
  42.     reverseDiagonal(myMatrix2, 5);
  43.     cout<<"After reverse Diagonal: "<<endl;
  44.     printMatrix(myMatrix2, 5);
  45.     system("pause");
  46.     return 0;
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement