Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- int n, m;
- bool is_null(int *mass, int size) {
- for (int i = 0; i < size; ++ i)
- if (mass[i] == 0)
- return true;
- return false;
- }
- template<class T>
- void bubsort(T* mass, int size, int (*compare)(T, T)) {
- for (int i = 0; i < size; ++ i) {
- for (int j = i - 1; j >= 0; -- j) {
- if (compare(mass[j + 1], mass[j]) == -1) {
- T a = mass[j + 1];
- mass[j + 1] = mass[j];
- mass[j] = a;
- }
- }
- }
- }
- int compare(int* a, int* b) {
- int sa = 0, sb = 0;
- for (int i = 0; i < m; ++ i) {
- if (a[i] > 0)
- sa += a[i];
- if (b[i] > 0)
- sb += b[i];
- }
- if (sa > sb)
- return 1;
- else if (sa < sb)
- return -1;
- else
- return 0;
- }
- int main(void) {
- srand(time(NULL));
- cin >> n >> m;
- int **mass = new int*[n];
- int *column = new int[n];
- cout << "Old:\n";
- for (int i = 0; i < n; ++ i) {
- mass[i] = new int[m];
- for (int j = 0; j < m; ++ j) {
- mass[i][j] = rand() % 100 - 50;
- cout << setw(4) << mass[i][j] << " ";
- }
- cout << "\n";
- }
- int a = 0;
- for (int i = 0; i < m; ++ i) {
- for (int j = 0; j < n; ++ j)
- column[j] = mass[j][i];
- if (!is_null(column, n))
- ++ a;
- }
- cout << "Columns: " << a << "\n";
- bubsort(mass, n, compare);
- cout << "\nSorted:\n";
- for (int i = 0; i < n; ++ i) {
- for (int j = 0; j < m; ++ j)
- cout << setw(4) << mass[i][j] << " ";
- cout << "\n";
- }
- for (int i = 0; i < n; ++ i)
- delete [] mass[i];
- delete [] mass;
- delete [] column;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement