计算数组的大小(Calculating Size of the Array)

编程入门 行业动态 更新时间:2024-10-28 12:15:53
计算数组的大小(Calculating Size of the Array)

我正在尝试计算文件的大小。 我遵循的过程是读取文件并将其存储在数组中并计算其大小。 但是,我真的不知道......我尝试了很多方法..我已经将这个大小作为属性传递给频率function.along与数组的名称。

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

?????? 是我读取文件并存储它的整数数组的大小。

我找不到计算我复制文件的数组大小的方法。 我的想法是计算该文件中数字的频率并计算它发生的概率,从而计算熵。请提出建议!

I'm trying to calculate the size of the file . The process I've followed is to read the file and store it in an array and calculate its size. However,I really don't know ... I tried n number of ways..I've to pass this size as an attribute to the frequency function.along with the name of the array.

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

?????? is the size of the integer array from where i read the file and stored it.

I could not find a way to calculate the size of the array into which i copied my file. My idea is to calculate the frequency of a number in that file and calculate the probability of it's occurrence and thereby calculating entropy..Suggestions please!

最满意答案

我不知道你为什么要初始化这么多的变量,其中一些变量有些尴尬的名字,比如?????? 。

你的主要问题是对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); }

I don't know why you are initializing so many variables and some of them with awkward names like ??????.

Your main problem is that the call to function should be

frequency(integers, i, x);

Your code with the awkward irrelevant parts removed will look like

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

更多推荐

本文发布于:2023-08-06 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小   Calculating   Size   Array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!