我的程序假设读取多个输入并显示最小值,最大值,总和和平均值。它应该是这样的:在c中使用scanf和循环输入多个双精度数据,并且没有数组循环
输入:
2.5 5 3.5 4.5 jfkjk
输出:
min is 2.5
max is 5
sum is 15.5
average is 3.875
该方案被认为当它到达一个非数字或一个换行符退出。用户可以输入尽可能多的数字。我不能使用数组,并且必须使用循环。 这是我的计划是什么样子:
void numbers()
{
double digit;
double sum = 0;
double avg = 0;
double max;
double min;
unsigned count = 0;
//int c;
max = 0;
printf("Input:");
do {
scanf("%lf", &digit);
min = digit;
if(max < digit)
digit = max;
if(min < digit)
digit = min;
sum += digit;
count++;
avg = sum/count;
} while(scanf("%lf", &digit)==1)
printf(" min is %lf max is %lf sum is %lf avg is %lf count is %u", min, max, sum, avg, count);
}
打印出:
Input:2.2 2.3 5 3.5 blah
min is 3.500000 max is 0.000000 sum is 0.000000 avg is 0.000000 count is 4
+0
先用正确的输入检查你的程序,然后尝试further.see Liho答案,并使用一个scanf函数与环路和移动scanf函数内部循环来之前。 –