2013-12-17 55 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是該操作符的一個實現:它創建其中它存儲陣列中的每個元件的頻率的新的數組(即多少次的每個元素出現。)。該函數返回具有最高頻率的元素,或者在具有相同最高頻率的情況下返回大數。

希望它有幫助