我正在尝试计算数组中值的频率。我认为我目前的条目是正确的,但它似乎误算了“计数”。我想就如何正确显示数组的频率提出第二意见!这是我所拥有的:
#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 */