编写程序,将十名同学的成绩存入数组,并统计10名同学的平均成绩,最高成绩和最低成绩
时间: 2024-05-09 07:15:20 浏览: 21
以下是一个示例程序,用于将十名同学的成绩存入数组、计算平均成绩、最高成绩和最低成绩:
```python
# 定义一个存储成绩的数组
scores = []
# 循环10次,依次输入每个同学的成绩,并将其添加到数组中
for i in range(10):
score = float(input("请输入第{}名同学的成绩:".format(i+1)))
scores.append(score)
# 计算平均成绩
average_score = sum(scores) / len(scores)
# 计算最高成绩和最低成绩
max_score = max(scores)
min_score = min(scores)
# 输出结果
print("平均成绩:", average_score)
print("最高成绩:", max_score)
print("最低成绩:", min_score)
```
运行程序后,依次输入每个同学的成绩,程序会输出平均成绩、最高成绩和最低成绩。
相关问题
C语言从键盘输入10名学生的C语言成绩存入一维数组内,编写程序计算10名学生的总成绩、最高分、平均分
好的,让我来解答你的问题。
请看以下C语言代码:
```c
#include <stdio.h>
int main() {
int scores[10];
int sum = 0, max = 0;
double average;
printf("请输入10名学生的成绩:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &scores[i]);
sum += scores[i];
if (scores[i] > max) {
max = scores[i];
}
}
average = sum / 10.0;
printf("十名学生的总成绩为:%d\n", sum);
printf("十名学生的最高分为:%d\n", max);
printf("十名学生的平均分为:%.2f\n", average);
return 0;
}
```
这段代码实现了输入10名学生的成绩,存储到int类型的数组中,然后计算出这些学生的总成绩、最高分、平均分,并输出结果。
简单来说,就是通过for循环逐个输入学生成绩、累加学生成绩、比较得到最高分,最后计算平均分,通过printf函数输出结果。
希望这一段代码能帮到你!
30、编写一个程序,从键盘输入10个学生成绩存入一维数组中,统计最高分、最低分和平均分。
下面是一个 C++ 程序的例子,实现了输入学生成绩并统计最高分、最低分和平均分的功能:
```c++
#include <iostream>
using namespace std;
int main() {
const int num = 10; // 学生数量
int scores[num]; // 存放学生成绩的数组
int max_score, min_score, sum = 0;
double avg_score;
// 输入学生成绩
cout << "请输入 " << num << " 个学生的成绩:" << endl;
for (int i = 0; i < num; i++) {
cin >> scores[i];
sum += scores[i];
if (i == 0) {
max_score = min_score = scores[i];
} else {
if (scores[i] > max_score) max_score = scores[i];
if (scores[i] < min_score) min_score = scores[i];
}
}
// 计算平均分并输出结果
avg_score = (double)sum / num;
cout << "最高分:" << max_score << endl;
cout << "最低分:" << min_score << endl;
cout << "平均分:" << avg_score << endl;
return 0;
}
```
程序中使用了一个 `for` 循环遍历数组输入学生成绩,并在输入时就同时计算最高分、最低分和总分。在循环结束后计算平均分并输出结果。注意要将 `sum` 转换为 `double` 类型再计算平均分。