Я предполагаю, что вам нужна программа на C ++, использующая функции C ++.Ваша текущая программа - это программа на C, которая использует C ++ I / O.Дайте мне знать, если вы хотите программу на C, которая использует функции C.
Программа на C ++ означает, что вы должны использовать std :: vector, но в случае, если для присвоения требуется массив стиля C, вот версия:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
int main() {
int size, *array;
std::cout << "How many elements? ";
std::cin >> size;
// create the array
array = new int[size];
if (array) {
// fill the array from the keyboard (could also use std::generate_n)
std::for_each(array, array+size, [index = 0](int& value) mutable {
std::cout << "Element " << ++index << "? ";
std::cin >> value;
});
// calculate the index of the max and min
auto minmax = std::minmax_element(array, array+size);
std::cout << "\nThe min " << *minmax.first << " is located at index " << std::distance(array, minmax.first);
std::cout << "\nThe max " << *minmax.second << " is located at index " << std::distance(array, minmax.second);
// calculate the average between the indexes
double average = std::accumulate(++minmax.first, minmax.second, 0.0, [count = 0](double average, int value) mutable {
std::cout << "\nAdding " << value << " to average";
return average + (value - average)/++count;
});
// print the result
std::cout << "\nAverage is " << average;
// delete the array
delete[] array;
}
}
И в случае, если я ошибаюсь, и вам разрешено использовать std :: vector, вот версия:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <vector>
int main() {
int size;
std::vector<int> array;
std::cout << "How many elements? ";
std::cin >> size;
// fill the array from the keyboard
std::generate_n(std::back_inserter(array), size, [index = 0, value = 0]() mutable {
std::cout << "Element " << ++index << "? ";
std::cin >> value;
return value;
});
// calculate the index of the max and min
auto minmax = std::minmax_element(array.begin(), array.end());
std::cout << "\nThe min " << *minmax.first << " is located at index " << std::distance(array.begin(), minmax.first);
std::cout << "\nThe max " << *minmax.second << " is located at index " << std::distance(array.begin(), minmax.second);
// calculate the average between the indexes
double average = std::accumulate(++minmax.first, minmax.second, 0.0, [count = 0](double average, int value) mutable {
std::cout << "\nAdding " << value << " to average";
return average + (value - average)/++count;
});
// print the result
std::cout << "\nAverage is " << average;
}