2016-02-19 52 views
0

對於學校我必須創建一個函數,打印排序數組的模式。它必須考慮到存在多種模式並且沒有模式。因爲即使有數組中的功能始終打印「無模式」C函數查找模式總是返回「無模式」

void findMode(double *mode, double a[], unsigned int size) 
{ 
    double number = a[0]; //Used to to compare values in the array to see if they're similar 
    int count = 1; //Keeps track of number of occurences for each number 
    int max = 1; //Keeps track of max number of occurences found for each number 
    int uniqueNum = 1; //Keeps track of how many unique values in the array 
    int maxCount = 1; //Counts how many set's of numbers occur the max ammount of times 
    int elementNum = 0; //Keeps track of element number in mode array 

    for (unsigned i = 1; i < size; ++i)//loop to determine how many modes and unique numbers there are 
    { 
     if (number == a[i]) 
     { 
      ++count; //if the numbers the two numbers compared are the same, count is increased by 1 
     } 
     else 
     { 
      if (count == max) 
      { 
       ++maxCount; //Keeps track of how many modes are in the array 
      } 
      if (count > max) 
      { 
       //If the two numbers compared are not the same and the count of the previous "set" of similar numbers is higher than the current max count, max is equal to the new count 
       max = count; 
       maxCount = 1; //Reset the max count if a new max is found 
      } 
      //Count is set back to 1 as we are counting a different number 
      count = 1; 
      number = a[i]; 
      ++uniqueNum; //Unique number variable gets incremented 
     } 
    } 

    printf("\n%d, %d, %d", max, maxCount, uniqueNum); 
    count = 1; //sets count back to 1 for next loop 

    if ((double)size/max == uniqueNum) 
    { 
     mode = malloc(1); 
     mode[0] = (a[size - 1] + 1); //Returns a value that can't exist in the array there is no mode 
    } 
    else 
    { 
     mode = malloc(sizeof(double) * maxCount); //makes the mode array the right size to store all the modes 
     for (unsigned i = 1; i < size; ++i)//loop to determine what the modes are 
     { 
      if (number == a[i]) 
      { 
       ++count; //if the numbers the two numbers compared are the same, count is increased by 1 
      } 
      else 
      { 
       if (count == max) 
       { 
        mode[elementNum] = a[i]; 
        ++elementNum; 
       } 
       //Count is set back to 1 as we are counting a different number 
       count = 1; 
      } 
     } 

     if (mode[0] = (a[size - 1] + 1)) 
     { 
      printf("\nMode: no mode"); 
     } 
     else 
     { 
      printf("\nMode: {"); 
      for (int i = 0; i <= (sizeof(mode)/sizeof(mode[0])); ++i) 
      { 
       printf(" %.3lf ", mode[i]); 
      } 
      printf("}"); 
     } 
    } 

} 

我不知道我多麼接近或遠從我在這個正確的邏輯,所以如果我是一個模式某種原因一路順風,需要從頭開始讓我知道。

謝謝!

+0

我想你需要從頭開始學習如何使用調試器。 –

+1

從1開始的數組索引循環不會讓我充滿信心:( –

+0

@MartinJames我看到了,但他首先讀取索引0,但是**模式**是什麼? –

回答

2

你的問題是你比較if (mode[0] = (a[size - 1] + 1))

=是做任務不等價測試。您將mode[0]分配給值a[size -1] + 1

c中的賦值運算符返回左操作數的值,您可以在the answer to this question中看到該值。在你的情況下,它會返回一個正數,它將被轉換爲布爾值並且每次都被評估爲真。

+0

是的,我意識到它與此有關,並將其改爲與布爾值一起工作,因爲這樣更簡單(並且我假設更合適)。現在我確實在有模式時得到了一個數字輸出,但是它有些奇怪{-674285437328543.124543543}或類似的東西。我在想這是一個指針/內存分配問題。我會繼續玩弄它,看看我能不能修復它 – Frankjoww