這是第一次我提出結構的數組。基本上我試圖做出一種直方圖。這是分析我提供的字符串,並打印出哪個字母出現次數最多。我也想說我的程序確實有效!我得到了所有正確的結果。但是當程序完成計算並顯示它們時,它會崩潰。而且我留下了一個錯誤,說陣列結構的:不兼容的參數類型
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;
}
你應該數組的大小傳遞給函數,而不是在寫函數'25'。'main()'函數和'maximum_occurrences()'之間有太多的「耦合」,否則就是這樣。你使用哪種語言,有25個字母的字母表,或當字符串是「快速棕色狐狸跳過懶狗」時會發生什麼?使用97而不是「a」是一個好主意。在進行計算之前,您應該檢查字符是否是小寫字母,並且可能會將大寫字母轉換爲小寫字母,而忽略非字母字符。你的字符串包含空格! –
請注意,您尚未爲要指向的指針數組分配空間。你幾乎可以肯定需要一個簡單的數組結構(大小爲26),而不是指向結構的(未初始化的)指針數組。 –
'struct Occurrences * arr1 [25];'是一個指向struct的指針數組,而不是一個結構數組。刪除'*'。 – Lundin