C/C++

Cоставление и отладка программ работы с двумерными массивами C++

Создать квадратный массив целых чисел из диапазона [1,15]. Найти максимальный элемент на побочной диагонали
#include <algorithm>
#include <iostream>
#include <random>
#include <iomanip>
using namespace std;
int main() {
const auto n = 8U;
int matrix[n][n];
const auto a = 1, b = 15;
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto print = [](int x) { cout << setw(5) << x; };
for (auto& row : matrix) {
generate(begin(row), end(row), rand);
}
for (const auto& row : matrix) {
for_each(begin(row), end(row), print);
puts("\n");
}
puts("");
double max = 0;
unsigned r = 0U, c = n - 1U;
for (auto i = 0U; i < n; ++i) {
auto j = n - 1 - i;
if (matrix[i][j] > max) {
max = matrix[i][j];
r = i;
c = j;
}
}
cout << " max: matrix[" << r << "][" << c << "] = " << max << '\n';
system("pause > nul");
}
Boris Bobylkov
Boris Bobylkov
58 241
Лучший ответ
Составление и отладка. Это что то сложное. Проще просто программу написать и мозг себе не делать)))
TT
Tl Traveller
10 755
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

int main(){
int n, sum = 0, max;
cout<<"Vvedite razmer massiva -> ";
cin>>n;

int A[n][n];
//zapolnenie massiva A ot -10 do 10
for(int i=0;i<n;i++){
for(int j=0;j= 0 && A[i][j] <= 9){
cout<<" "<<A[i][j]<<" ";
}
else {
cout<<A[i][j]<<" ";
}
}
cout<<endl;
}

cout<<endl<<"Pobochnaya diagonal' -> ";
int j = n - 1;
max = A[0][n];
for(int i=0;i max){
max = A[i][j];
}
cout<<A[i][j]<<" ";
j--;
}
cout<<"\nMax value = "<<max;
getch();
}