我正在尝试计算文件的大小。我遵循的过程是读取文件并将其存储在数组中并计算其大小。但是,我真的不知道......我尝试了多种方法......我必须将此大小作为属性传递给频率函数。以及数组的名称。
#include <stdio.h>
#include<conio.h>
void frequency (int theArray [ ], int ??????, int x)
{
int count = 0;
int u;
for (u = 0; u < ??????; u++)
{
if ( theArray[u]==x)
{
count = count + 1 ;
/*printf("\n%d",theArray[u]);*/
}
else
{
count = count ;
}
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int i=0;
int r = 0;
int num;
int theArray[100];
int there[100];
int n;
int g;
int x;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",(integers[i]));
there[r] = integers[i];
i++;
}
//printf("%d",there[r]);
//printf("\n%d",file);
//fclose(file);
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf("\n%d", &x);/*Stores Number To Search For Frequency*/
frequency(integers,????????,x);
getch();
fclose(file);
}
??????
是我读取文件并存储它的整数数组的大小。
我找不到计算复制文件的数组大小的方法。我的想法是计算该文件中数字的频率并计算其出现的概率,从而计算熵......请提出建议!
最佳答案
我不知道为什么你要初始化这么多变量,其中一些变量的名称很奇怪,例如 ??????
。
你的主要问题是对function
的调用应该是
frequency(integers, i, x);
删除了尴尬的不相关部分后,您的代码将如下所示
#include <stdio.h>
#include<conio.h>
void frequency (int theArray [ ], int number, int x)
{
int count = 0;
int u;
for (u = 0; u < number; u++)
{
if ( theArray[u]==x)
count++;
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int i=0;
int num;
int x;
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*/
frequency(integers,i,x);
getch();
fclose(file);
}
关于计算数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25956095/