从在C文件中查找最大和最小的浮点数 [英] Finding Max and Min floating numbers from a file in C
本文介绍了从在C文件中查找最大和最小的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写使用C只是为了找到一个包含约500个浮点数,如54.54我的输入文件的最大值和最小值数的程序。我可以让程序运行,但输出的说,我是最小0和最大我是54.88是从文件中的第一个号码。
这里是我有这么远。</ P>
的#include&LT;&stdio.h中GT;INT主(INT ARGC,为const char * argv的[])
{ FILE * FP; FP = FOPEN(file.txt的,R);
如果(FP == NULL)
{
的printf(无法打开);
} 浮动我;
浮分钟;
浮动最大; {
的fscanf(FP,%F,&安培; I) 如果(I&LT;分)
分= I;
如果(ⅰ&GT;最大)
最大= I;
}
的printf(数据范围为:%F%F \\ N,最小值,最大值);
返回0;
}
解决方案
这个程序将符合要求。
的#include&LT;&stdio.h中GT;诠释的main()
{
浮NUM;
浮分钟= 999.99;数/ *最大值您的文件不会超过* /
FLOAT MAX = 0;
INT I = 0; FILE * FP;
FP = FOPEN(file.txt的,R); 而(的fscanf(FP,%F,试验#)== 1)
{
如果(NUM&LT;分)
分= NUM;
如果(NUM&GT;最大)
最大= NUM;
} FCLOSE(FP); 的printf(数据范围为:%F%F \\ N,最小值,最大值); 返回0;
}
I am writing a program using C just to find the max and min numbers from my input file that contains about 500 floating numbers such as 54.54. I can get the program to run but the output says my min is 0 and my max is 54.88 which is the very first number from the file. Here is what i have so far.
#include <stdio.h>
int main(int argc, const char * argv[])
{
FILE * fp;
fp=fopen("file.txt","r");
if (fp==NULL)
{
printf("Failed to open");
}
float i;
float min ;
float max ;
{
fscanf( fp, "%f", &i);
if (i < min)
min = i;
if (i > max)
max = i;
}
printf("Data range is: %f %f \n", min, max);
return 0;
}
解决方案
This program will fit the bill.
#include <stdio.h>
int main()
{
float num;
float min = 999.99; /*Max value of number your file will not exceed*/
float max = 0;
int i = 0;
FILE *fp;
fp = fopen("file.txt", "r");
while(fscanf(fp,"%f",&num) == 1)
{
if (num < min)
min = num;
if (num > max)
max = num;
}
fclose(fp);
printf("Data range is: %f %f \n", min, max);
return 0;
}
这篇关于从在C文件中查找最大和最小的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文