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

时间:2018-10-16 20:48:53

标签: c++ arrays for-loop

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

例如,给定{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

3 个答案:

答案 0 :(得分:0)

我假设您要使用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;
}

答案 1 :(得分:0)

首先,您需要找到索引。

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

您可以使用下面的代码

let link = (~url=?, label) => 
  <a href=?url> {ReasonReact.string(label)} </a>

输出:

  

最大值为索引[7]:10

     

最小值为索引[0]:1

     

总和:27 &&距离:6

     

平均值:4.5


#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; } 的输出:

  

最大索引为[3]:9

     

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

     

总和:6 &&距离:2

     

平均值:3

答案 2 :(得分:0)

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