C++:平均中位数和众数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20636914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:15:38  来源:igfitidea点击:

C++: Mean Median and Mode

c++arraysaveragebubble-sort

提问by beckah

I've recently created a C++ program to find the mean median and mode of an array of values. I realize this would be much better to do within a class. However, my function to generate the mean is not spitting out the right number, although I'm pretty certain the logic is fine.

我最近创建了一个 C++ 程序来查找一组值的平均中位数和众数。我意识到在课堂上这样做会好得多。但是,我生成平均值的函数并没有吐出正确的数字,尽管我很确定逻辑很好。

Also, I was able to modify a snipbit from something I found online to create a function that generates the mode, or at least the 1st most occurring values it can find, that I was able to implement. However, I am not 100% sure of how to wrap my head around what is actually happening within the function.

此外,我能够从我在网上找到的东西修改一个 snipbit 来创建一个函数来生成模式,或者至少是它可以找到的第一个最常出现的值,我能够实现。但是,我不是 100% 确定如何理解函数中实际发生的事情。

A better understanding of what is happening in the mode function and what the hell is going wrong in my mean function would be greatly appreciated.

更好地理解 mode 函数中发生的事情以及我的 mean 函数到底出了什么问题将不胜感激。

This is my code so far:

到目前为止,这是我的代码:

#include <iostream>

using namespace std;

void mode(int[], int);
void mean(int[], int);
void sort(int[], int);
void median(int[], int);

int main()
{

   int array[15];
   float total, mode;
   int n = 15;//number of elements in array

    //fill in the value of array
    for(int i=0; i<n; i++){
        cout << "fill in the "<< i+1 << " number. :";
        cin >> array[i];
    }

    sort(array, n);
    return 0;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void mean(int new_array[], int num){
 //GET TOTAL & CALCULATE MEAN
    float total;
    for(int i=0;i<num; i++){
        total += new_array[i];
    }
    cout << "The mean is " << total/num << endl;
    mode(new_array, num);
    }
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void median(int new_array[], int num){
    //CALCULATE THE MEDIAN (middle number)
    if(num % 2 != 0){// is the # of elements odd?
        int temp = ((num+1)/2)-1;
        cout << "The median is " << new_array[temp] << endl;
    }
    else{// then it's even! :)
        cout << "The median is "<< new_array[(num/2)-1] << " and " << new_array[num/2] << endl;
    }
    mean(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void mode(int new_array[], int num) {
    int* ipRepetition = new int[num];
    // alocate a new array in memory of the same size (round about way of defining number of elements by a variable)
    for (int i = 0; i < num; i++) {
        ipRepetition[i] = 0;//initialize each element to 0
        int j = 0;//
        while ((j < i) && (new_array[i] != new_array[j])) {
            if (new_array[i] != new_array[j]) {
                j++;
            }
        }
        (ipRepetition[j])++;
    }
    int iMaxRepeat = 0;
    for (int i = 1; i < num; i++) {
        if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
            iMaxRepeat = i;
        }
    }
    cout<< "The mode is " << new_array[iMaxRepeat] << endl;

}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

void sort(int new_array[], int num){
     //ARRANGE VALUES
    for(int x=0; x<num; x++){
         for(int y=0; y<num-1; y++){
             if(new_array[y]>new_array[y+1]){
                 int temp = new_array[y+1];
                 new_array[y+1] = new_array[y];
                 new_array[y] = temp;
             }
         }
     }
    cout << "List: ";
    for(int i =0; i<num; i++){
        cout << new_array[i] << " ";
    }
    cout << "\n";
    median(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

回答by Joseph Mansfield

Don't forget to initialise your variables:

不要忘记初始化变量:

float total = 0.0f;

In C++, a variable with automatic storage duration can be left uninitialized. Using such a variable will give you undefined behaviour.

在 C++ 中,具有自动存储期的变量可以保持未初始化状态。使用这样的变量会给你带来未定义的行为。

回答by Fred Larson

For one thing, you haven't initialized some of your variables. In mean(), for instance, you should have this:

一方面,您尚未初始化某些变量。mean()例如,在 中,您应该具有以下内容:

float total = 0;

Variables are not initialized to any defined value by default.

默认情况下,变量不会初始化为任何定义的值。

I recommend you increase the warning level on your compiler. If you're using g++, use -Wall. That would detect problems such as using uninitialized variables and unused variables (which you have in main()).

我建议您提高编译器的警告级别。如果您使用的是 g++,请使用-Wall. 这将检测问题,例如使用未初始化的变量和未使用的变量(您在 中拥有main())。

回答by Pandrei

Mode is one of the basic statistical operators; it represent the element with the highest frequency in an array.

众数是基本的统计运算符之一;它代表数组中频率最高的元素。

Your function modeis a implementation of this operator: it creates a new array in which it stores the frequency of each element in the array(i.e. how many times each element appears). The function returns the element with the highest frequency or, in case there are more with the same highest frequency, it returns the larges one.

您的函数mode是此运算符的实现:它创建一个新数组,其中存储数组中每个元素的频率(即每个元素出现的次数)。该函数返回具有最高频率的元素,或者,如果有更多具有相同最高频率的元素,则返回较大的元素。

Hope it helps

希望能帮助到你