查找数组中第一个最小值和最后一个最大值元素之间的算术平均值



我有以下问题要解决:给定一个数组,我需要找到索引为第一个最小元素和最后一个最大元素的元素之间的算术平均值(索引边界不包括在内(。

例如,给定{1, 5, 1, 9, 2, 7, 1, 3},第一个最小元素和最后一个最大元素分别为 1 和 9,它们的索引是 0 和 3,因此答案将是索引为 1..2 的元素的算术平均值,即 5 和 1 的算术平均值,即 3。

我知道如何找到整个数组的mean,但如何找到数组的第一个最小和最后一个最大元素之间的mean

#include <iostream>
using namespace std;
int main(){
setlocale(LC_ALL,"RUS");
cout << "Enter the array: ";
int k;
double Sum = 0;
double min = 0;
double max = 0;
const int n = 7;
double mass[8] = {0, 0, 0, 0, 0, 0, 0, 0};
for(k = 0; k <= n; k++){
cin >> mass[k];
}
for(int i = 0; i <= 7; i++){
if(mass[i] > max){
max = mass[i];
}
else if (mass[i] < min){
min = mass[i];
}
}

int i;
for(i = 0; i <= n; i++){
Sum = Sum + mass[i];
}
cout << Sum / 8;
return 0;
}

答案应该是3.

我假设你想要一个使用C++功能的C++程序。 当前程序是使用 C++ I/O 的 C 程序。 如果您想要一个使用 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;
}

您需要从迭代器而不是值的角度来考虑。 不要记录第一个最小值,而是创建一个迭代器,将元素指向一个过去。 不要记录最后一个最大值,而是创建一个指向该值的迭代器。 然后,您可以将这些定义的范围传递给迭代器,以便std::accumulate进行求和,然后将其除以相同范围的std::distance以找到平均值。 但请注意,最小值和最大值之间的元素数可能为 0。

首先,您需要查找索引。

然后从第一个最小值到最后一个最大值的索引遍历数组。

您可以使用下面的代码

#include <iostream>
using namespace std;
int main()
{
int array[] = { 1, 5, 2, 10, 2, 7, 1, 10};
int min = array[0];
int max = array[0];
int indexOfMin = 0;
int indexOfMax = 0;
int sum = 0;
float dist = 0;
float mean = 0;
int arrSize = sizeof(array)/sizeof(array[0]);
for (int i = 0; i < arrSize; i++){
if(array[i] >= max ){
max = array[i];
indexOfMax = i;
}
}
cout << "Max is at index [" << indexOfMax << "]  : " << max << endl;
for (int i = 0; i < arrSize; i++){
if(array[i] == min){
continue;
}
if(array[i] < min){
min = array[i];
indexOfMin = i;
}
}
cout << "Min is at index [" << indexOfMin << "]  : " << min << endl;
if(indexOfMin > indexOfMax){
indexOfMax++;
indexOfMin--;
for(int i = indexOfMax; i <= indexOfMin; i++){
sum += array[i];
}
dist = indexOfMin - indexOfMax + 1;
}else if(indexOfMin < indexOfMax){
indexOfMax--;
indexOfMin++;
for(int i = indexOfMin; i <= indexOfMax; i++){
sum += array[i];
}
dist = indexOfMax - indexOfMin + 1;
}
mean = sum/dist;
cout << "Sum: " << sum << " && dist: " << dist << endl;
cout << "Mean: " << mean << endl;
return 0;
}

输出:

最大值位于索引 [7] : 10

最小值位于索引 [0] : 1

总和: 27 &&区: 6

平均值:4.5


int array[] = {1, 5, 1, 9, 2, 7, 1, 3}输出:

最大值位于索引 [3] : 9

最小值位于索引 [0] : 1

总和: 6 &&区: 2

平均值:3

最新更新