Advertisement
Sanlover

Untitled

Nov 23rd, 2021
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6.   double** matrix;
  7.   int m, n;
  8.   cout << "Enter m: ";
  9.   cin >> m;
  10.   if (m <= 0) {
  11.     cout << "M must be positive";
  12.     return 0;
  13.   }
  14.   cout << "Enter n: ";
  15.   cin >> n;
  16.   if (n <= 0) {
  17.     cout << "N must be positive";
  18.     return 0;
  19.   }
  20.  
  21.   matrix = new double*[m];
  22.   for (int i = 0; i < m; i++) {
  23.     matrix[i] = new double[n];
  24.   }
  25.  
  26.   int maxI = 0, maxJ = 0;
  27.   cout << "Fill matrix:" << endl;
  28.   for (int i = 0; i < m; i++) {
  29.     for (int j = 0; j < n; j++) {
  30.       cout << "[" << i << "][" << j << "]) ";
  31.       cin >> matrix[i][j];
  32.       if (matrix[maxI][maxJ] < matrix[i][j]) {
  33.         maxI = i;
  34.         maxJ = j;
  35.       }
  36.     }
  37.   }
  38.  
  39.   cout << endl << "Your array:" << endl;
  40.   for (int i = 0; i < m; i++) {
  41.     for (int j = 0; j < n; j++) {
  42.       cout << setw(4) << matrix[i][j] << " ";
  43.     }
  44.     cout << endl;
  45.   }
  46.   // swaping row
  47.   double* temp = matrix[0];
  48.   matrix[0] = matrix[maxI];
  49.   matrix[maxI] = temp;
  50.  
  51.   // u can remove
  52.   cout << endl << "Your array after swapping row:" << endl;
  53.   for (int i = 0; i < m; i++) {
  54.     for (int j = 0; j < n; j++) {
  55.       cout << setw(4) << matrix[i][j] << " ";
  56.     }
  57.     cout << endl;
  58.   }
  59.   // swaping column
  60.   for (int i = 0; i < m; i++) {
  61.     double temp = matrix[i][0];
  62.     matrix[i][0] = matrix[i][maxJ];
  63.     matrix[i][maxJ] = temp;
  64.   }
  65.  
  66.   cout << endl << "Your array after swapping column:" << endl;
  67.   for (int i = 0; i < m; i++) {
  68.     for (int j = 0; j < n; j++) {
  69.       cout << setw(4) << matrix[i][j] << " ";
  70.     }
  71.     cout << endl;
  72.   }
  73.   return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement