Условие - Найти максимальный элемент матрицы. Строку, содержащую
максимальный элемент, поменять с последней строкой матрицы.
Нумерация в матрице начинается с 0.
С++ на Code Blocks 16
Объяснение:
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int n = 8; // можно ввести любую размерность квадратной матрицы
int a[n][n];
int Nmax, Nind, i, j = 0 ;
int d;
// Заполним матрицу случайными числами в диапазоне [0 ,100)
// и сразу её выведем
cout << " ---- Array in start ---- " << endl;
srand(time(0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = rand()%111;
cout <<a[i][j] ;
cout<< " ";
}
cout << " " << endl;
}
// Поиск максимального элемента матрицы. Для оптимизации, можно было это произвести на этапе заполнения матрицы
// но для наглядности, напишем отдельно
Nmax = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
if (Nmax < a[i][j]) {
Nmax = a[i][j];
Nind = i;
}
}
}
cout<< "Max ["<< Nind<<"] = "<< Nmax << endl;
// Меняем строки местами
for (int j = 0; j < n; j++) {
d = a[n-1][j];
a[n-1][j]=a[Nind][j];
a[Nind][j] = d;
}
cout << " ---- Array after modify ---- " << endl;
// Выводим полученную матрицу
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
cout << a[i][j] ;
cout<< " ";
}
cout << " " << endl;
}
return 0;
}
Объяснение:
C++Выделить код
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void main()
{
setlocale(0, "rus");
int mas[5][5];
srand(time(NULL));
cout << "Первоначальный массив" << endl;
for (int i = 0; i<5; i++){
cout << endl;
for (int j = 0; j<5; j++){
mas[i][j] = rand() % 10;
cout << mas[i][j] << " ";
}
cout << endl;
}
cout << "Полученный массив" << endl;
for (int i = 0; i<5; i++){
int tmp = mas[i][i];
mas[i][i] = mas[i][5 - i - 1];
mas[i][5 - i - 1] = tmp;
}
cout << endl;
for (int i = 0; i<5; i++){
cout << endl;
for (int j = 0; j<5; j++){
cout << mas[i][j] << " ";
}
cout << endl;
}
}
0