2013-12-17 33 views
0

我最近創建了一個C++程序來查找值的數組的平均值和模式。模式功能:它是如何工作的?

我能夠從我在網上找到的東西中修改一個snipbit,創建一個函數來生成我能夠實現的模式,或者至少可以找到的第一個最常出現的值。然而,我不能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); 
} 
/////////////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////////////// 
+0

模式是一個基本的統計算子;它表示數組中頻率最高的元素(出現次數最多)。這正是你的功能所做的。這個操作符的常見問題是,如何處理具有最高相同頻率的更多元素的情況 - 在這裏,您只需取第一個元素(據我所知) – Pandrei

回答

3

在一個非常高的水平,首先它泄漏內存。

int* ipRepetition = new int[num]; 

分配一個新的數組,之後沒有什麼叫delete[]。其次,它通過遍歷原始數據數組的大小逐個填充新數組,其中一次只能遍歷一個數據的大小,直到當前的地址爲i,如果(new_array[i] != new_array[j])(它檢查兩次以確保)它會增加j

如果它發現匹配或到目前爲止它已填充的元素的末尾,它會在jipRepetition陣列中添加一個。 這是試圖跟蹤new_array索引i中的數字被使用的頻率。

接下來for循環然後遍歷這些數字找到索引i最大值。

然後在此索引處打印原始數組中的值。

如果函數被更改爲返回值,它可能會更有用。既然它是C++,你可以使用一個向量來避免內存泄漏。

1

你有兩個平行陣列:一個用於數字,一個用於計算重複。

重複計數的方式是遍歷列表直到當前數字,停止在第一次匹配並增加重複次數。說有以下的數組:

在第一次迭代中,用戶設置平行陣列爲0的第一個值,然後結束立即打破內部循環的和遞增它,留下你與:

1? ?

在並行數組中。在第二次迭代中,循環將在第一個項目上再次中斷,因爲new_array[1] == new_array[0] == 5。所以你會留下:

2 0?

...當然在第三次迭代中,第三個值將最終設置爲1。

如果您仍然難以理解,可以將它想象爲給原始列表中的每個數字賦予一個「點」,然後將這些點向後移動到每個數字的第一個實例。你甚至可以在紙上試試這個。