Last active
August 29, 2015 14:02
-
-
Save morontt/40990c733fc4cfca0b87 to your computer and use it in GitHub Desktop.
salazar 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <stdlib.h> | |
using namespace std; | |
//определяем характеристику строки | |
int haract(int* row, int length) { | |
int i, result = 0; | |
for (i = 0; i < length; i++) { | |
if (row[i] < 0 && row[i] % 2 == 0) { | |
result += row[i]; | |
} | |
} | |
return result; | |
} | |
int main() { | |
int s[100][100]; | |
int h[100], tmp_row[100]; | |
int n, m, i, j, k; | |
int column_null, tmp; | |
bool has_null; | |
//размеры матрицы (кол-во строк и столбцов) | |
cout << "n = "; | |
cin >> n; | |
cout << "m = "; | |
cin >> m; | |
//инициализация генератора случайных чисел | |
//заполнение массива | |
srand(time(NULL)); | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < m; j++) { | |
s[i][j] = rand() % 100 - 49; | |
} | |
h[i] = haract(s[i], n); | |
} | |
//вывод матрицы | |
cout << "array:" << endl; | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < m; j++) { | |
cout << s[i][j] << "\t"; | |
} | |
cout << "| " << h[i]; | |
cout << endl; | |
} | |
cout << endl; | |
//поиск первого столбца с нулём | |
has_null = false; | |
column_null = 0; | |
for (j = 0; j < m; j++) { | |
for (i = 0; i < n; i++) { | |
if (!has_null && s[i][j] == 0) { | |
has_null = true; | |
column_null = j; | |
} | |
} | |
} | |
//пузырьковая сортировка | |
for (i = n - 1; i > 1; i--) { | |
for (j = 0; j < i; j++) { | |
if (h[j] < h[j + 1]) { | |
tmp = h[j]; | |
h[j] = h[j + 1]; | |
h[j + 1] = tmp; | |
for (k = 0; k < m; k++) { | |
tmp_row[k] = s[j][k]; | |
s[j][k] = s[j + 1][k]; | |
s[j + 1][k] = tmp_row[k]; | |
} | |
} | |
} | |
} | |
//вывод отсортированной матрицы | |
cout << "sorted array:" << endl; | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < m; j++) { | |
cout << s[i][j] << "\t"; | |
} | |
cout << "| " << h[i]; | |
cout << endl; | |
} | |
cout << endl; | |
if (has_null) { | |
column_null++; | |
cout << "first column with null: " << column_null << endl; | |
} else { | |
cout << "matrix without null" << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment