bogdanNiculeasa

sortare doar o coloana din matrice si doar elemente pare

Feb 1st, 2024
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int matrice[6][4] = {
  6.             {2, 3, 4, 5},
  7.             {8, 7, 8, 5},
  8.             {1, 3, 5, 7},
  9.             {3, 0, 2, 9},
  10.             {3, 5, 1, 6},
  11.             {7, 3, 0, 2},
  12.     };
  13.     int numere[6];
  14.  
  15.     // extrag coloana 3 intr-un vector separat
  16.     for(int i = 0; i < 6; i++) {
  17.         numere[i] = matrice[i][2];
  18.     }
  19.  
  20.     // sortam vectorul extras
  21.     for (int i = 0; i < 6; i++) {
  22.         for (int j = 0; j < 6; j++) {
  23.             if (numere[i] % 2 == 0 && numere[j] % 2 == 0) {
  24.                 if (numere[i] < numere[j]) {
  25.                     int aux = numere[i];
  26.                     numere[i] = numere[j];
  27.                     numere[j] = aux;
  28.                 }
  29.             }
  30.         }
  31.     }
  32.  
  33.     // introducem valorile updatate in coloana respectiva
  34.     for(int i = 0; i < 6; i++) {
  35.         matrice[i][2] =numere[i];
  36.     }
  37.  
  38.  
  39.     // afisam matricea
  40.     for(int i = 0; i < 6; i++) {
  41.         for (int j = 0; j < 4; j++) {
  42.             cout << matrice[i][j] << " ";
  43.         }
  44.         cout << endl;
  45.     }
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment