用于查找最小值,最大值,平均值,中值和模式的程序

时间:2015-09-27 00:16:48

标签: c++ max min mode median

背景

编写一个C ++程序,该程序接受用户输入的一组数字,并输出最大值,最小值,平均值,中位数和模式。我们知道模式可以是多个数字,因此请编写代码,以便在必要时输出多种模式。

示例输入

输入整数并在完成后输入一个字母。

-2 3 3 5 6 4 4 8 9 9 9 1 -2 10 10 10

样本输出

Your numbers are: -2 -2 1 3 3 4 4 5 6 8 9 9 9 10 10 10
Max: 10  
Min: -2  
Average: 5.4375  
Median: 5.5  
Mode: 9 10 (3) 

我的代码

#include <iostream>
using namespace std;

void main()
{
    double value, sum;
    double average, minimum, maximum;
    int count;
    // initialise
    sum = 0.0;
    count = 0;
    cout << "Enter a value: ";
    cin >> value;
    minimum = value;
    maximum = value;
    while (value >= 0.0)
    {
        // process value
        sum += value;
        count++;
        if (value > maximum)
            maximum = value;
        else if (value < minimum)
            minimum = value;
        // get next value
        cout << "Enter a value: ";
        cin >> value;
    }
    if (count == 0)
        cout << "No data entry" << endl;
    else
    {
        average = sum / count;
        cout << "There were " << count << " numbers" << endl;
        cout << "Average was " << average << endl;
        cout << "Minimum was " << minimum << endl;
        cout << "Maximum was " << maximum << endl;
    }
}

3 个答案:

答案 0 :(得分:0)

如果你想使用STL,你可以写这样的东西(使用c ++ 11):

One = 1

答案 1 :(得分:0)

以下代码是您的代码的完整版本的编辑版本:

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    double value, sum;
    double average, minimum=(int)INFINITY, maximum=-(int)INFINITY, median;
    map<int,int>modes;
    vector<int>data;
    int count,mxmode=0;
    // initialise
    sum = 0.0;
    count = 0;
    cout << "Enter a value: ";

    while (cin >> value)
    {
        // process value
        sum += value;
        count++;
        if (value > maximum)
            maximum = value;
        else if (value < minimum)
            minimum = value;

        modes[ value ]++;
        mxmode = max(mxmode, modes[value]);
        data.push_back(value);

        // get next value
        cout << "Enter a value: ";
    }
    if (count == 0)
        cout << "No data entry" << endl;
    else
    {
        average = sum / count;

        sort(data.begin(), data.end());

        if(data.size()%2==0) median = ( data[ data.size()/2 - 1 ] + data[ data.size()/2 ] )/2.0;
        else median = data[ data.size()/2 ];

        cout << "There were " << count << " numbers" << endl;
        cout << "Average was " << average << endl;
        cout << "Minimum was " << minimum << endl;
        cout << "Maximum was " << maximum << endl;
        cout << "Median was " << median << endl;
        cout << "Median was";

        for (auto &x: modes)
            if(x.second == mxmode) cout<<" "<<x.first;

        cout<<" ("<<mxmode<<")" << endl;
    }
}

记住:

  • 在使用C ++ 11的功能时,不要忘记使用最新的-std=c++11标志进行编译。
  • 输入由EOF终止,或者在控制台中您可以通过按 Ctrl + z 然后输入来终止它(使用相应的如果你的操作系统与Windows不同,请使用密钥。

答案 2 :(得分:0)

平均:

首先,您需要对数字进行排序。您可以将它们存储在一个数组中,然后使用排序算法,例如插入排序,选择排序等。一旦对数组进行排序,中位数就是中间数。下面是插入排序实现的代码,假设您已将数字存储在arr[]中,length是数组的长度。

    void insertion_sort(double arr[], int length) {
      int i, j;
      double tmp; 
      for (i = 1; i < length; i++) {
        j = i;
        while (j > 0 && arr[j - 1] > arr[j]) {
          tmp = arr[j];
          arr[j] = arr[j - 1];
          arr[j - 1] = tmp;
          j--;
        }//end of while loop
        print_array(arr,5);
      }//end of for loop
    }//end of insertion_sort.

模式:

您可以使用计数器跟踪阵列中的每个数字。最常见的是模式。