2015-09-04 48 views
1

這是第一次我提出結構的數組。基本上我試圖做出一種直方圖。這是分析我提供的字符串,並打印出哪個字母出現次數最多。我也想說我的程序確實有效!我得到了所有正確的結果。但是當程序完成計算並顯示它們時,它會崩潰。而且我留下了一個錯誤,說陣列結構的:不兼容的參數類型

IntelliSense: argument of type "Occurrences **" is incompatible with parameter of type "Occurrences *" 

此行的代碼:

maximum_occurrences(str, ch, num, arr1); 

它位於我main.c。我會盡力提供下面所有必需的代碼。任何關於如何在最後修復崩潰的想法都將非常感激,但程序邏輯再次完美無缺! :)也謝謝我是新手編碼器(C中的1個學期)。

主營:

int main() 
{ 
    // Introduced the original string which as a pointer. 
    // Introduced the original character pointer 'ch'. (Set to an arbitrary Character.) 
    // Introduced the original int pointer as 'num'. (Set to an arbitrary integer.) 
    char *str = {"hey my name is dillon johnson im in comp sci"}, *ch = '\0'; 
    int *num = 0; 

    struct Occurrences *arr1[25]; 

    // Calling the max occurrences function. 
    maximum_occurrences(str, ch, num, arr1); 

    return 0; 
} 

頁眉:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <math.h> 

typedef struct occurrences 
{ 
    int num_occurrences; 
    double frequency; 
}Occurrences; 

void maximum_occurrences (char *str, char *chPtr, int *num, Occurrences *arr1); 

由source.c:

void maximum_occurrences(char *str, char *chPtr, int *num, Occurrences *arr1) 
{ 
    char stringp = *str; 
    int i = 0, j = 0, strLength = 0, temp, max = 0, k = 0; 
    double frequency = 0; 
    char maxch = ' '; 

    // Setting strLength to the length of the alpha string. 
    strLength = strlen(str); 

    for (k = 0; k < 25; k++) 
    { 
     arr1[k].num_occurrences = 0; 
    } 

    // Displaying to the user what sentance will be used for the program. 
    printf ("%s%s", "The given sentence is: ", str); 
    printf ("\n"); 

    // The algorithm to determine which characters occur multiple times. 
    // I am using a for loop to first parse through the array and then when 
    // each letter occurs I subtract 97 from the ascii value. The resulting 
    // Integer will be the corresponding place in the array of struct Occurrences 
    // where that particular variable is held. 

    for (i = 0; i < strLength; i++) 
    { 
     temp = (str[i] - 97); // temp is now set to an integer value that is the correspoinding position in the array of structs. 
     (int)arr1[temp].num_occurrences = (int)arr1[temp].num_occurrences + 1; // Increases the number of occurrences in the specific struct by one to represnt 
                    // a single occurrence of that character. 
    } 

    for (j = 0; j < 26; j++)     // This finds the maximum amount of occurrences and then continues to mach that value 
    {           // to a character by adding 97 to the index of the array to acheive the appropriate ascii value 
     if (max < arr1[j].num_occurrences) 
     { 
      max = arr1[j].num_occurrences; 
      maxch = j + 97; 
     } 
    } 

    frequency = ((double)max/(double)strLength); // Calculating the frequency of the most common character 

    printf ("%s%c%s%d%s%.3lf%s", "The letter '", maxch,"' occurred the most, occurring ", max, " times, with a frequency of: ", frequency,".\n"); 

    return; 
} 
+0

你應該數組的大小傳遞給函數,而不是在寫函數'25'。'main()'函數和'maximum_occurrences()'之間有太多的「耦合」,否則就是這樣。你使用哪種語言,有25個字母的字母表,或當字符串是「快速棕色狐狸跳過懶狗」時會發生什麼?使用97而不是「a」是一個好主意。在進行計算之前,您應該檢查字符是否是小寫字母,並且可能會將大寫字母轉換爲小寫字母,而忽略非字母字符。你的字符串包含空格! –

+0

請注意,您尚未爲要指向的指針數組分配空間。你幾乎可以肯定需要一個簡單的數組結構(大小爲26),而不是指向結構的(未初始化的)指針數組。 –

+0

'struct Occurrences * arr1 [25];'是一個指向struct的指針數組,而不是一個結構數組。刪除'*'。 – Lundin

回答

2

的問題到底是什麼錯誤消息告訴你。學會解讀的編譯器錯誤和生活將是您更容易...

arr1是25個指針紀錄結構數組。

maximum_occurrences預計一個Occurrences*,不是他們的陣列。

因此,你不能傳遞到arr1maximum_occurrences()

基於通過評論的討論:)

在main(你有struct Occurrences* arr1[25]; - 25個指針數組紀錄(紀錄*)。你從不分配任何事件,所以即使它編譯了,也不會起作用。

你想要25次發生(非指針) - struct Occurrences arr1[25]; - 現在你應該可以按原樣使用max_occurrences。請注意,max_occurrences假設長度爲25是不好的做法 - 您應該將長度轉換爲長度。

最後但並非最不重要的,如果你想每個字母出現1次,需要26的陣列(它給你的索引0-25)

+0

感謝您的快速響應。所以我必須將'void maximum_occurrences(char * str,char * chPtr,int * num,Occurrences * arr1)'改成void void maximum_occurrences(char * str,char * chPtr,int * num,Occurrences arr1 [25]) 「或者我剛剛完全不在,我的語法不太好,我很抱歉。 –

+0

不,你的第二次嘗試有'max_occs'獲取25個對象的數組。 'arr1'是一個由25個指向對象的指針組成的數組。你仍然試圖在這個圓孔中插入一個方形釘......你確定'arr1'應該是一個指針數組而不是一個對象數組? – John3136

+0

arr1應該是一個指向25個結構事件數組的指針。至少對我來說至少令人困惑。但是我認爲我的先知得到的是他希望每個字母都有一個結構。如果這是有道理的。 –

0

你需要的功能更改爲

void maximum_occurrences(char *str, char *chPtr, int *num, Occurrences **arr1, int arrsize){ 
//.. 
for (k = 0; k < arrsize; k++) 
    { 
     arr1[k]->num_occurrences = 0; 
    } 
//.. 
for (j = 0; j < arrsize; j++)     // This finds the maximum amount of occurrences and then continues to mach that value 
    {           // to a character by adding 97 to the index of the array to acheive the appropriate ascii value 
     if (max < arr1[j]->num_occurrences) 
     { 
      max = arr1[j]->num_occurrences; 
      maxch = j + 97; 


     } 
     //.. 
} 

然後調用它與

maximum_occurrences(str, ch, num, arr1, 25); 

另外,不要使用調用maximum_occurrences()之前忘了分配的內存arr1 210用於每個數組元素。

+0

分配每個「發生次數」結構的空間在哪裏?目前,它在您的代碼或原始代碼中無處可做。將'main'中的類型改爲'Occurrences arr1 [26];'(允許一個正統的26個字母的字母表)可能會更好。 –

相關問題