2013-12-17 54 views
2

我最近创建了一个C++程序,找出值的数组的平均中位数和模式。我意识到在课堂上这样做会更好。然而,我的函数生成的意思是不吐出正确的数字,虽然我非常肯定的逻辑是好的。C++:平均中位数,众

另外,我是可以修改的东西从我在网上找到创建生成模式功能的snipbit,或至少1最大出现的数值,它可以找到,我是能够实现。然而,我不能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); 
} 
/////////////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////////////// 
+4

不,它不会在班级中变得更好一点。 – molbdnilo

+0

@molbdnilo你是什么意思? – beckah

+2

@rsheeler结构或类用于聚合状态。你没有状态,所以不需要结构或类。 –

回答

6

不要忘了初始化的变量:

float total = 0.0f; 

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

4

一件事,你有没有一些初始化的变量。在mean(),例如,你应该有这样的:

float total = 0; 

变量不被初始化到默认情况下任何定义的值。

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

+0

优秀!只是这样做,和它的工作就像一个魅力 – beckah

+0

IM还是有点迷茫的模式功能是如何工作的,但是 – beckah

+0

@rsheeler'mode'泄漏内存,这是你一定要解决。 – juanchopanza

1

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

你的功能mode是该操作符的一个实现:它创建其中它存储阵列中的每个元件的频率的新的数组(即多少次的每个元素出现。)。该函数返回具有最高频率的元素,或者在具有相同最高频率的情况下返回大数。

希望它有帮助