x3c2c5

Untitled

Jan 21st, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int n;
  7.     cin >> n;
  8.  
  9.     int A[n][n] ;
  10.  
  11.     // input matrix
  12.     for (int i = 0; i < n; i++)
  13.     {
  14.         for (int j = 0; j < n; j++)
  15.         {
  16.             cin >> A[i][j];
  17.         }
  18.     }
  19.  
  20.     int min_element = A[0][0], min_col = 0;
  21.  
  22.     // find the first minimal element on the main diagonal and its column
  23.     for (int i = 0; i < n; i++)
  24.     {
  25.         if (A[i][i] < min_element)
  26.         {
  27.             min_element = A[i][i];
  28.             min_col = i;
  29.         }
  30.     }
  31.  
  32.     int row;
  33.     cin >> row;
  34.  
  35.     // swap the column with the minimal element on the main diagonal with the specified row
  36.     for (int i = 0; i < n; i++)
  37.     {
  38.         swap(A[i][min_col], A[i][row]);
  39.     }
  40.  
  41.     // output the new matrix
  42.     for (int i = 0; i < n; i++)
  43.     {
  44.         for (int j = 0; j < n; j++)
  45.         {
  46.             cout << A[i][j] << " ";
  47.         }
  48.         cout << endl;
  49.     }
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment