Advertisement
WadeRollins2710

Excersise 2

Oct 17th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. //Tran Viet Anh - 9:33PM
  2. #include<iostream>
  3. #include<string.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     cout << "Input number of row: ";
  9.     int n; cin >> n;
  10.     cout << "Input number of column: ";
  11.     int m; cin >> m;
  12.     int a[n][m];
  13.     for (int i = 0; i < n; i++)
  14.         for (int j = 0; j < m; j++)
  15.         {
  16.             cout << "a[" << i << "," << j << "]: ";
  17.             cin >> a[i][j];
  18.         }
  19.     int maxRow[n];
  20.     int minColumn[m];
  21.     int countMax[n];
  22.     int countMin[m];
  23.  
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         countMax[i] = 0;
  27.         maxRow[i] = -INT_MAX;
  28.     }
  29.  
  30.     for (int j = 0; j < m; j++)
  31.     {
  32.         countMin[j] = 0;
  33.         minColumn[j] = INT_MAX;
  34.     }
  35.  
  36.     for (int i = 0; i < n; i++)
  37.         for (int j = 0; j < m; j++)
  38.         {
  39.             if (a[i][j] > maxRow[i])
  40.             {
  41.                 countMax[i] = 1;
  42.                 maxRow[i] = a[i][j];
  43.             }
  44.             else if (a[i][j] == maxRow[i]) countMax[i]++;
  45.             if (a[i][j] < minColumn[j])
  46.             {
  47.                 countMin[j] = 1;
  48.                 minColumn[j] = a[i][j];
  49.             }
  50.             else if (a[i][j] == minColumn[j]) countMin[j]++;
  51.         }
  52.  
  53.     for (int i = 0; i < n; i++)
  54.         for (int j = 0; j < m; j++)
  55.             if (a[i][j] == maxRow[i] && countMax[i] == 1 && a[i][j] == minColumn[j] && countMin[j] == 1)
  56.                 cout << a[i][j] << " ";
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement