Advertisement
StoneHaos

nastya23

Jun 2nd, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int n, m;
  8.  
  9. bool is_null(int *mass, int size) {
  10.     for (int i = 0; i < size; ++ i)
  11.         if (mass[i] == 0)
  12.             return true;
  13.     return false;
  14. }
  15.  
  16. template<class T>
  17. void bubsort(T* mass, int size, int (*compare)(T, T)) {
  18.     for (int i = 0; i < size; ++ i) {
  19.         for (int j = i - 1; j >= 0; -- j) {
  20.             if (compare(mass[j + 1], mass[j]) == -1) {
  21.                 T a = mass[j + 1];
  22.                 mass[j + 1] = mass[j];
  23.                 mass[j] = a;
  24.             }
  25.         }
  26.     }
  27. }
  28.  
  29. int compare(int* a, int* b) {
  30.     int sa = 0, sb = 0;
  31.     for (int i = 0; i < m; ++ i) {
  32.         if (a[i] > 0)
  33.             sa += a[i];
  34.         if (b[i] > 0)
  35.             sb += b[i];
  36.     }
  37.     if (sa > sb)
  38.         return 1;
  39.     else if (sa < sb)
  40.         return -1;
  41.     else
  42.         return 0;
  43. }
  44.  
  45. int main(void) {
  46.     srand(time(NULL));
  47.  
  48.     cin >> n >> m;
  49.     int **mass = new int*[n];
  50.     int *column = new int[n];
  51.  
  52.     cout << "Old:\n";
  53.     for (int i = 0; i < n; ++ i) {
  54.         mass[i] = new int[m];
  55.         for (int j = 0; j < m; ++ j) {
  56.             mass[i][j] = rand() % 100 - 50;
  57.             cout << setw(4) << mass[i][j] << " ";
  58.         }
  59.         cout << "\n";
  60.     }
  61.  
  62.     int a = 0;
  63.     for (int i = 0; i < m; ++ i) {
  64.         for (int j = 0; j < n; ++ j)
  65.             column[j] = mass[j][i];
  66.         if (!is_null(column, n))
  67.             ++ a;
  68.     }
  69.     cout << "Columns: " << a << "\n";
  70.  
  71.     bubsort(mass, n, compare);
  72.  
  73.     cout << "\nSorted:\n";
  74.     for (int i = 0; i < n; ++ i) {
  75.         for (int j = 0; j < m; ++ j)
  76.             cout << setw(4) << mass[i][j] << " ";
  77.         cout << "\n";
  78.     }
  79.  
  80.     for (int i = 0; i < n; ++ i)
  81.         delete [] mass[i];
  82.     delete [] mass;
  83.     delete [] column;
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement