【发布时间】:2019-11-24 22:20:16
【问题描述】:
我需要编写一个程序,打开一个txt 文件并列出有多少个数字(例如下面有 25 个数字),然后列出哪个数字最大(最大)和哪个最小(最小) .然后程序对这些数字进行平均。
到目前为止,我的程序只写了我得到的数字。当我试图将公式发挥到最大时,我只是停下来不能动弹。
你能帮帮我吗?如何通过读取行并评估最大数字来做到这一点?
#include <stdio.h>
FILE *input;
int open()
{
if ((input = fopen("data.txt", "r")) == NULL)
{
printf("Error! opening file");
return 0;
}
}
int rows_reading ()
//Here i dont know what to do
{
input = fopen ("data.txt", "r");
}
void main ()
{
char c;
int linesCount = 0;
float max, min;
float a;
float n;
int count = 0;
input = fopen ("data.txt", "r");
while ((c = fgetc(input)) !=EOF)
{
if(c =='\n')
linesCount++;
} // this works
while ((c = getc(input)) !=EOF)
{
for (a = 0; a < count; a++){
if (a > max)
max = a;
}
} //this not work
printf("In file is %d numerical values. Max value is %d"linesCount, max);
return ;
} ```
【问题讨论】:
-
我看不出有什么计数和 for 循环。无需多次读取值即可记住最大值! (也不是最小值,也不是求平均值)
标签: c