我正在嘗試計算數組中某個值的頻率。我認爲自己目前的參賽作品正處於正確的軌道上,但它似乎算錯了「數」。我會喜歡關於如何正確顯示陣列的頻率的第二意見!下面是我有:C - 計算數組中數字的頻率
#include <stdio.h>
/************************************************************************/
/* Function: frequency */
/* */
/* Purpose: Obtains the frequency of a number in an array */
/* */
/* */
/* */
/* Parameters: theArray-The array in question */
/* n- the size of the array */
/* x- the number to search for frequency within the array */
/* */
/* Returns: The frequency of a given number in an array */
/* */
/************************************************************************/
int frequency (int theArray [ ], int n, int x)
{
int count = 0;
int i;
for (i = 0; i < n; ++i)
{
if (theArray[i]=x)
{
count = count + 1 ;
}
else
{
count = count ;
}
}
printf ("\nThe frequency of %i in your array is %i ",x,count);
}
int main()
{
int i;
int theArray[] = {};
int n;
int x;
printf ("Enter The Amount Of Numbers In Your Array: ");
scanf("%i", &n);/*Stores Amound Of Numbers In The Array*/
for (i = 0; i < n; ++i)
{
printf("\nEnter number for array: ");
scanf ("%i", &theArray[i]);
}
printf ("\nOK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf("%i", &x);/*Stores Number To Search For Frequency*/
frequency(theArray,n,x);
return(0); /* success */
} /* main */
就像一個快速評論。如果你有一個if語句,其做一些事情,和別人不其什麼,只是省略了其他!此外,你已經宣佈你的頻率函數返回一個int值。目前它什麼都不返回。您應該將int更改爲void,或者返回一個值。 – 2013-03-06 01:53:48