编写C程序,.输入10个学生的成绩,求平均分、最高分和最低分,不要用数组
时间: 2024-04-21 10:23:19 浏览: 186
以下是计算平均分、最高分和最低分的 C 程序示例,而不使用数组:
```c
#include <stdio.h>
int main() {
int numStudents = 10;
int score;
int sum = 0;
int maxScore = 0;
int minScore = 100;
for (int i = 1; i <= numStudents; i++) {
printf("请输入第 %d 个学生的成绩:", i);
scanf("%d", &score);
sum += score;
if (score > maxScore) {
maxScore = score;
}
if (score < minScore) {
minScore = score;
}
}
float average = (float)sum / numStudents;
printf("平均分:%.2f\n", average);
printf("最高分:%d\n", maxScore);
printf("最低分:%d\n", minScore);
return 0;
}
```
在上述示例中,我们首先声明变量 `numStudents` 来存储学生人数,变量 `score` 来存储每个学生的成绩,变量 `sum` 来存储所有学生成绩的总和,以及变量 `maxScore` 和 `minScore` 来存储最高分和最低分。
然后,我们使用一个循环来依次输入每个学生的成绩,并更新总和、最高分和最低分。在每次循环中,我们使用 `scanf` 函数从键盘读取学生的成绩,并将其添加到总和中。然后,我们检查该成绩是否是目前的最高分或最低分,并相应地更新 `maxScore` 和 `minScore`。
最后,我们计算平均分,将总和除以学生人数,并将结果存储在变量 `average` 中。
最终,我们使用 `printf` 函数输出平均分、最高分和最低分的结果。
运行程序后,它会要求您输入每个学生的成绩。然后,程序将计算并输出平均分、最高分和最低分。