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

C++: Mean Median and Mode

本文关键字:中位数 C++      更新时间:2023-10-16

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

此外,我能够

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

更好地了解模式函数中发生了什么以及我的平均函数中到底出了什么问题,将不胜感激。

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

#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);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

不要忘记初始化变量:

float total = 0.0f;

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

首先,你还没有初始化一些变量。例如,在mean()中,您应该拥有以下内容:

float total = 0;

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

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

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

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

希望对你有帮助