C/C++

Программирование C++, нужна помощь)

Дано три одномерных массива: первый размерностью n, второй – k, третий – m. Для каждого массива определить разницу между минимальным и максимальным элементами массива.
#include "stdafx.h"
#include
using namespace std;

void minmax (int n){
int *a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int min = 999999;
int max = -9999999;
for (int i = 0; i < n; i++) {
if (a[i] < min){
min = a[i];
}
if (a[i] > max) {
max = a[i];
}
}
max = max - min;
cout << "Result \n";
cout << max << "\n";

}

int main()
{
int n = 3;
int k = 4;
int m = 5;
cout << "For first array \n";
minmax(n);
cout << "For second array \n";
minmax(k);
cout << "For third array \n";
minmax(m);
cin >> n;
return 0;
}
Гриша Вагайцев
Гриша Вагайцев
659
Лучший ответ
Гриша Вагайцев Ну и собственно если надо менять размерности, то после инициализации n,k,m вводите свои значения.
#include <algorithm>
#include <string>
#include <random>
#include <iostream>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int value) { cout << value << ' '; };
string msg[] = { "n: ", "k: ", "m: " };
for (auto i = 0U; i < size(msg); ++i) {
cout << msg[i];
size_t length;
cin >> length;
auto box = new int[length];
generate(box, box + length, rand);
for_each(box, box + length, show);
puts("");
auto [min, max] = minmax_element(box, box + length);
cout << *max << " - " << *min << " = " << *max - *min << "\n\n";
delete[] box;
}
system("pause > nul");
}
Роман Тарлавин
Роман Тарлавин
79 568
#include "iostream"
#include "ctime"
#include "cstdlib"
#include "algorithm"
using namespace std;
void gen(int *a, int n){
generate(a,a+n,[n]()mutable{return cout<<(n=rand()%10-5)<<' ',n;}); cout<<'\n';}
void out(int *a, int n){
cout<<"max-min="<< *max_element(a,a+n)-*min_element(a,a+n)<<"\n\n";}
int main(){
int na,nb,nc; cout<<"n k m: "; cin>>na>>nb>>nc; srand(time(NULL));
int *a=new int[na], *b=new int[nb], *c=new int[nc];
gen(a,na); out(a,na); gen(b,nb); out(b,nb); gen(c,nc); out(c,nc);
delete []a; delete []b; delete []c;}
Роман Харченко
Роман Харченко
65 493