Haval1

Question 2

Jan 17th, 2019
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. // sol-final-ex1.cpp : Defines the entry point for the console application.
  2. /*
  3. swap main with secondary diagonal bewteen a[4][4],b[4][4] using pointer concepts in (enters , swap , print)
  4. */
  5.  
  6. #include "stdafx.h"
  7. #include "iostream"
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     int a[4][4], b[4][4], i, j,temp,x[4],y[4];
  13.     int *c[4][4],*d[4][4],*f[4],*g[4];
  14.     for (i = 0;i < 4;i++)
  15.         for (j = 0;j < 4;j++) {
  16.             c[i][j] = &a[i][j];
  17.             d[i][j] = &b[i][j];
  18.         }
  19.     for (i = 0;i < 4;i++) {
  20.         f[i] = &x[i];
  21.         g[i] = &y[i];
  22.     }
  23.     for(i=0;i<4;i++)
  24.         for (j = 0;j < 4;j++)
  25.             cin >> *c[i][j];
  26.     cout << "\n=================\n";
  27.     for (i = 0;i<4;i++)
  28.         for (j = 0;j < 4;j++)
  29.             cin >> *d[i][j];
  30.     for (i = 0;i < 4;i++)
  31.         *f[i] = *c[i][i];
  32.     for (i = 0;i < 4;i++)
  33.         for (j = 0;j < 4;j++)
  34.             if (i + j == 3)
  35.                 *g[i] = *d[i][j];
  36.     cout << "\n=================\n";
  37.     for (i = 0;i < 4;i++) {
  38.         temp = *f[i];
  39.         *f[i] = *g[i];
  40.         *g[i] = temp;
  41.     }
  42.     for (i = 0;i < 4;i++)
  43.          *c[i][i] = *f[i];
  44.     for (i = 0;i < 4;i++)
  45.         for (j = 0;j < 4;j++)
  46.             if (i + j == 3)
  47.                 *d[i][j] = *g[i];
  48.     cout << "After swap between main and secondary diagonal \n";
  49.     for (i = 0;i < 4;i++) {
  50.         for (j = 0;j < 4;j++) {
  51.             cout << *c[i][j] << "  ";
  52.         }cout << endl;
  53.     }
  54.     cout << "\n==========\n";
  55.     for (i = 0;i < 4;i++) {
  56.         for (j = 0;j < 4;j++) {
  57.             cout << *d[i][j] << "  ";
  58.         }cout << endl;
  59.     }
  60.  
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment