【问题标题】:Mode Function: How does it work? [duplicate]模式功能:它是如何工作的? [复制]
【发布时间】:2013-12-17 15:29:53
【问题描述】:

我最近创建了一个 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 = 0;
    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);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

【问题讨论】:

  • mode 是一个基本的统计算子;它表示数组中频率最高(出现次数最多)的元素。这正是您的功能所做的。这个运算符的常见问题是如何处理具有相同频率的更多元素的情况,这是最高的 - 在这里你只需取第一个(据我所知)

标签: c++ arrays average bubble-sort


【解决方案1】:

在非常高的层次上,首先它会泄漏内存。

int* ipRepetition = new int[num];

分配一个新数组,之后没有调用delete[]

其次,如果(new_array[i] != new_array[j])(它只检查两次),它通过一次遍历原始数据数组的大小直到它到达i的当前位置来用零填充新数组可以肯定)它递增j

如果它找到匹配项或到达它到目前为止已填充的元素的末尾,它会将一个添加到ipRepetition 数组的位置j。 这试图跟踪索引 new_array 中的数字 i 的使用频率。

下一个for 循环然后遍历这些数字以找到索引i 最大值。

然后它在这个索引处打印原始数组中的值。

如果函数被更改为返回值可能会更有用。由于它是 C++,因此您可以使用向量来避免内存泄漏。

【讨论】:

    【解决方案2】:

    您有两个并行数组:一个用于数字,一个用于计算重复次数。

    重复计数的方式是遍历列表直到当前数字,在第一个匹配处停止并增加其重复计数。假设您有以下数组:

    5 5 2

    在第一次迭代中,您将并行数组的第一个值设置为 0,然后立即跳出内部循环并将其递增,留下:

    1 ? ?

    在并行数组中。在第二次迭代中,循环将再次在第一项上中断,因为new_array[1] == new_array[0] == 5。所以你会留下:

    2 0 ?

    ...当然,在第三次迭代中,第三个值最终将设置为 1。

    如果你仍然难以理解,你可以把它想象成给原始列表中的每个数字一个“点”,然后将这些点向后移动到每个数字的第一个实例。你甚至可以在纸上试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2019-01-16
      相关资源
      最近更新 更多