计算数组的大小

标签 c arrays file-io

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

#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/

相关文章:

matlab - 如何在 matlab 中根据文本文件中的索引和值重新创建矩阵

c++ - __cyg_profile_func_enter 和 g++ 2.95.4

javascript - 一般使用javascript map函数是什么意思?

java - 在方法参数中设置数组项

C - 像二进制数一样遍历 0 和 1 的数组

c# - 使用管理员用户登录到某个域并使用 C# 复制文件客户端机器

c - 为什么数字字符的 ASCII 代码会写入文件?

c - 'struct x' 在参数列表中声明

c - FUSE开放系统调用机制

c++ - 如何在 C++ 中自动打开输入文件?