2015-04-20 114 views
1

我試圖計算兩個值之間的差異,我從一個結構數組上的每個條目得到某個參數('年齡')。計算色散C

我有一個輔助.txt文件,並且這個整點是要經過的每個.txt文件的單獨的線(每行是一個字符串)。它的更好,如果我狀態的例子,所以,在這裏我們去:

matrix[n][n]: 
1 2 3 4 
1 2 3 
1 2 

因此,結構看起來有點像這樣:

struct person { 
    int id; //where im comparing between the matrix and the structure; 
    int age; //what I need to calculate the dispersion 
} 

我要比較的每一行的每個值.txt,如果它與結構中的任何id相匹配,我就得到它的年齡。現在來了棘手的部分。

要計算我需要得到以下工作爲我分散:

讓我們爲例.txt文件的第一行:分散將是:

讓我們說,「年齡'= id(n)的年齡;

//in this case nGroups = 6 (number of age(n)-age(m) operations) 
dispersion(first_row)= [ [|age(1)-age(2)|] + [|age(1)-age(3)|] + [|age(1)-age(4)|] + [|age(2)-age(3)|] + [|age(2)-age(4)|] + [|age(3)-age(4)|] ]/nGroups 

所以我必須爲矩陣的每一行做這個。我試過了,並且管理了下面的代碼,但是在'數學'部分,我的大腦凍結了一下。

// going through each line of the matrix 
for(i=0; i<nconjuntos; i++) { 
    // going through each column of the matrix 
    for(j=0; j<strlen(aux[i]); j++) { 
    // going through all the structures in the structure array 
    for(k=0; k<TOTAL; k++) { 
     // comparing each number with it's id equivalent in 
     // each structure.id parameter in the array 
     if(aux[i][j] - 48 == pessoas[k].id) { 

     } 
    } 
    } 
} 

任何幫助我可以在我的代碼中前進的幫助將非常感激!

+0

呵呵。我不知道[散佈](http://en.wikipedia.org/wiki/Statistical_dispersion)是一個統計術語。 –

回答

0

你這裏有兩個問題需要解決:

  • 比賽與在結構中的ID矩陣中的ID並獲得青睞的列表給定行
  • 計算分散

如果你分開這兩個任務,而不是在同一個嵌套的for循環中執行它們,這可能是最簡單的。你可以根據你給這樣的公式寫一個函數來計算分散:

double calculateDispersion(double* values, int count) 
{ 
    if(count < 2) 
     return 0.0; // need at least 2 values 
    double total = 0.0; 
    // iterate through each combination of values and sum the absolute 
    // differences of each combination: 
    int i, j; 
    for(i = 0; i < (count - 1); i++) 
    { 
     for(j = i + 1; j < count; j++) 
     { 
      double difference = values[i] - values[j]; 
      if(difference < 0.0) // find absolute value 
       difference = 0.0 - difference; 
      total += difference; // add to the total 
     } 
    } 
    int groups = (count * (count - 1))/2; 
    return total/(double)groups; 
} 

內主循環建立在尺寸上最大的行等於雙打在您的矩陣陣列,存儲年齡爲當前行(或浮動,但使用整數會給你舍入錯誤)。通過與包含id和年齡的結構交叉引用填充它。然後調用calculateDispersion()來計算該行的離差。

喜歡的東西大致是這樣的:

double rowArray[MAX_SIZE]; // or initialize to maximum matrix row length dynamically 

// going through each line of the matrix 
for(i=0; i<nconjuntos; i++) { 
    int count = 0; 
    // going through each column of the matrix 
    for(j=0; j<strlen(aux[i]); j++) { 
    // going through all the structures in the structure array 
    for(k=0; k<TOTAL; k++) { 
     // comparing each number with it's id equivalent in 
     // each structure.id parameter in the array 
     if(aux[i][j] - 48 == pessoas[k].id) { 
      // set array value: 
      rowArray[count++] = (double)pessoas[k].age; 
      break; // break out of the loop 
     } 
    } 
    } 
    // compute the dispersion for the row: 
    doubleDispersion = calculateDispersion(rowArray, count); 
} 
+0

非常感謝!工作完美無缺,有一些疑問,但希望你能澄清:b 像,rowArray [count ++]操作到底做了什麼?此外,計數變量將是該行中的數字的數量,對嗎? – Zetsuno

+0

是的,count是行中的數字個數。 rowArray [count ++] = age將存儲在count中的索引處的rowArray元素設置爲年齡,然後將計數值遞增1(++是後增量運算符)。 – samgak