Haval1

Rotate main diameter 3 times A[4][4]

May 24th, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     //def.
  6.     int a[4][4], i, j;
  7.     int b[5];
  8.     //input.
  9.     for (j = 0; j <= 3; j++) {
  10.         for (i = 0; i <= 3; i++) {
  11.             cin >> a[j][i];
  12.         }
  13.     }
  14. //process.
  15. cout << "before rotation : ";
  16.     for (j = 0; j <= 3; j++) {
  17.         cout << endl << endl;
  18.         for (i = 0; i <= 3; i++) {
  19.         cout << a[j][i] << "\t";
  20.         }
  21.     }
  22.     for (i = 0; i <= 3; i++) {
  23.         b[i] = a[i][i];
  24.     }//transferring the values to the 1D array
  25.     for (j = 1; j <= 3; j++) {
  26.         for (i = 4; i >= 1; i--) {
  27.             b[i] = b[i - 1];
  28.         }
  29.     b[0] = b[4];
  30.     }//rotate
  31.     for (i = 0; i <= 3; i++) {
  32.         a[i][i] = b[i];
  33.     }//transferring the values to the 2D array
  34. //print.
  35. cout << "\nafter rotation : ";
  36.     for (j = 0; j <= 3; j++) {
  37.         cout << endl << endl;
  38.         for (i = 0; i <= 3; i++) {
  39.             cout << a[j][i] << "\t";
  40.         }
  41.     }
  42.     cout << endl;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment