Задача: Напишите программу, которая находит минимальный и максимальный элементы двумерного массива.
В программировании не силён. Выдаёт ошибку. Помогите пожалуйста её исправить.
Сам код:
#include
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
int N,M;
cout << "Enter arr size N x M : ";
cin >> N >> M;
int Arr[N][M];
for(int i =0; i < N; i++){
for(int j = 0; j < M; j++){
Arr[i][j]=rand()%10;
cout << Arr[i][j] << " ";}
cout << endl;}
int Min=Arr[0][0];
int Max=Arr[0][0];
for(int i =0; i < N; i++)
for(int j = 0; j < M; j++){
if(Arr[i][j]>Max)
Max=Arr[i][j];
if(Arr[i][j]<Min)
Min=Arr[i][j];}
cout << Max << endl << Min;
}
C/C++
Помогите исправить программу на С++
#include
using namespace std;
int main()
{
int N, M,i;
cout << "Enter arr size N x M : ";
cin >> N >> M;
int **Arr = new int*[N];
for (i = 0; i < N; i++)
Arr[i] = new int[M];
for (i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
Arr[i][j] = rand() % 10;
cout << Arr[i][j] << " ";
}
cout << endl;
}
int Min = Arr[0][0];
int Max = Arr[0][0];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
if (Arr[i][j] > Max)
Max = Arr[i][j];
if (Arr[i][j] < Min)
Min = Arr[i][j];
}
cout << Max << endl << Min;
}
так как то
using namespace std;
int main()
{
int N, M,i;
cout << "Enter arr size N x M : ";
cin >> N >> M;
int **Arr = new int*[N];
for (i = 0; i < N; i++)
Arr[i] = new int[M];
for (i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
Arr[i][j] = rand() % 10;
cout << Arr[i][j] << " ";
}
cout << endl;
}
int Min = Arr[0][0];
int Max = Arr[0][0];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
if (Arr[i][j] > Max)
Max = Arr[i][j];
if (Arr[i][j] < Min)
Min = Arr[i][j];
}
cout << Max << endl << Min;
}
так как то
#include <algorithm>
#include <iostream>
#include <random>
#include <iomanip>
using namespace std;
unsigned input(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
return value;
}
int main() {
const auto a = 10, b = 99;
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int x) { cout << setw(4) << x; };
auto n = input("n: ");
auto m = input("m: ");
puts("");
auto matrix = new int* [n];
for (auto i = 0U; i < n; ++i) matrix[i] = new(nothrow) int[m];
for (auto i = 0U; i < n; ++i) generate(matrix[i], matrix[i] + m, rand);
for (auto i = 0U; i < n; ++i) {
for_each(matrix[i], matrix[i] + m, show);
puts("");
}
puts("");
auto min = b, max = a;
for (auto i = 0U; i < n; ++i) {
auto [mn, mx] = minmax_element(matrix[i], matrix[i] + m);
if (*mn < min) min = *mn;
if (*mx > max) max = *mx;
}
cout << "Min: " << min << '\n' << "Max: " << max << '\n';
for (auto i = 0U; i < n; ++i) delete[] matrix[i];
delete[] matrix;
system("pause > nul");
}
#include <iostream>
#include <random>
#include <iomanip>
using namespace std;
unsigned input(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
return value;
}
int main() {
const auto a = 10, b = 99;
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int x) { cout << setw(4) << x; };
auto n = input("n: ");
auto m = input("m: ");
puts("");
auto matrix = new int* [n];
for (auto i = 0U; i < n; ++i) matrix[i] = new(nothrow) int[m];
for (auto i = 0U; i < n; ++i) generate(matrix[i], matrix[i] + m, rand);
for (auto i = 0U; i < n; ++i) {
for_each(matrix[i], matrix[i] + m, show);
puts("");
}
puts("");
auto min = b, max = a;
for (auto i = 0U; i < n; ++i) {
auto [mn, mx] = minmax_element(matrix[i], matrix[i] + m);
if (*mn < min) min = *mn;
if (*mx > max) max = *mx;
}
cout << "Min: " << min << '\n' << "Max: " << max << '\n';
for (auto i = 0U; i < n; ++i) delete[] matrix[i];
delete[] matrix;
system("pause > nul");
}
Похожие вопросы
- Помогите исправить программу на языке СИ, работает коряво.
- Помогите исправить и дописать программу с массивами на языке C++. Буду благодарен, т. к. самому уже не понятно.
- Ошибка в программе, помогите исправить.
- Помогите написать программу на С
- Помогите написать программу на C++
- Помогите написать программу на с++
- Помогите написать программу на с++
- Помогите написать программу
- Помогите с программой на C++
- Помогите написать программу на С++