我正在尝试编写程序来查找数字的频率而不要求用户输入。要清楚,我正在寻找一个用户不输入他需要计算的值的想法频率,编译器应默认告诉列表中每个数字的频率而不是用户给出的数字。下面提到的是我的代码。
#include <stdio.h>
#include<conio.h>
float frequency (int theArray [ ], int number, int x)
{
int count = 0;
float mount=0.0;
float tot = (float)number;
int u;
int k;
float q,h;
// printf("%d",number);
for (u = 0; u < number; u++)
{
if ( theArray[u]==x)
count++;
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
for(k = 0; k< number; k++)
{
if(theArray[k]==x)
//count(theArray[k])
{ mount++;
}
}
if(mount>1)
{
q = mount/tot;
return q;
}
else
{
h = mount/tot;
return h;
}
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int theArray[100];
int i=0;
float e;
int num;
int x,k;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",integers[i]);
i++;
}
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf(" %d", &x);/*Stores Number To Search For Frequency*/
e = frequency(integers,i,x);
printf("\n probability of %d is %f",x,e);
getch();
fclose(file);
}
所以我现在的输出是: 1)数字列表 2)出现次数的频率 3)我的号码的可能性 相反,我正在寻找能够给出我的号码的概率和频率而没有任何内容的东西 用户输入。即我正在寻找像
这样的东西输出: 这个数字的频率是----并且它的概率是---------
任何帮助都将受到高度赞赏。
答案 0 :(得分:-2)
这个循环可以解决你的问题
for(i = 0; i < number; i++)
{
if(array[i] == -999) // checking if the number has been counted before or not
continue;
temp = array[i]; // take each number from the array one by one
for(j = 0; j < number; j++) // iterate over the array to check the number of occurences of temp
{
if(array[j] == temp)
{
array[j] = -999; // putting -999 at places where the number has already been counted
count++;
}
}
printf("frequency of %d is %d, and probability is %f", temp, count, count/number);
count = 0;
}