我正在尝试计算文件的大小。我所遵循的过程是读取文件并将其存储在数组中并计算其大小。但是,我真的不知道。。。我尝试了多种方法。我必须将此大小作为频率函数的属性以及数组名进行传递。

#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);
}

08-04 06:08