【发布时间】:2020-11-07 19:25:48
【问题描述】:
我需要让用户输入 7 到 12 ([7;12]) 之间的 N(数字)。然后,我需要让用户输入数字的数量,应该等于N。然后,我需要输出这些数字的总和,找到平均值,最小值和最大值。 它工作得很好,除了最低限度。当数字从 1 开始计数时,我的程序显示最小值为 0。谢谢你...
#include <stdio.h>
int main(void) {
int i = 0;
int N;
float numbers;
float sum = 0;
float average, min, max;
printf("Choose a number between 7 and 12: ");
scanf("%d", &N);
if (N >= 7 && N <= 12) {
printf("You have chosen: %d\n", N);
printf("Now choose the amount of N numbers you have chosen: ");
for (i = 0; i < N; i++) {
numbers = N; // the numbers the user enters must be
// strictly == N
scanf("%f", &numbers);
sum = sum + numbers;
average = sum / numbers;
if (numbers < min) {
min = numbers;
}
if (numbers > max) {
max = numbers;
}
}
printf("The sum of your numbers is: %f\n The average of "
"numbers: %f\n The max: %f\n The min: %f\n",
sum, average, max, min);
}
}
输出是:
Choose a number between 7 and 12: 7
You have chosen: 7
Now choose the amount of N numbers you have chosen: 1
2
3
4
5
6
7
The sum of your numbers is: 28.000000
The average of numbers: 4.000000
The max: 7.000000
The min: 0.000000
【问题讨论】:
-
将 min 初始化为较大的值,将 max 初始化为较小的值。或者当输入数同时为最小值和最大值时,在第一次迭代时设置初始值。
-
有可能,写一段代码
标签: c