c语言从键盘输入10个成绩,然后求出最高成绩、最低成绩和它们在数组中的位置
时间: 2023-12-06 12:39:32 浏览: 109
可以使用一个数组来存储这10个成绩,然后遍历数组求出最高成绩和最低成绩,并记录它们在数组中的位置。代码如下:
```c
#include <stdio.h>
int main() {
int scores[10];
int max_idx = 0, min_idx = 0;
// 从键盘输入10个成绩
printf("请输入10个成绩:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &scores[i]);
}
// 求最高成绩和最低成绩及它们在数组中的位置
for (int i = 1; i < 10; i++) {
if (scores[i] > scores[max_idx]) {
max_idx = i;
}
if (scores[i] < scores[min_idx]) {
min_idx = i;
}
}
// 输出结果
printf("最高成绩:%d,位置:%d\n", scores[max_idx], max_idx);
printf("最低成绩:%d,位置:%d\n", scores[min_idx], min_idx);
return 0;
}
```
运行程序后,输入10个成绩,程序会输出最高成绩、最低成绩及它们在数组中的位置。
相关问题
c语言定义一维数组,从键盘输入10个成绩存入一维数组中,然后求出最高成绩,最低成绩和它们在数组中的位置(即数组元素的下标+1)
好的,以下是C语言的代码实现:
```c
#include <stdio.h>
int main() {
int scores[10];
int i, max_score, min_score, max_index, min_index;
for (i = 0; i < 10; i++) {
printf("请输入第%d个成绩:", i+1);
scanf("%d", &scores[i]);
}
max_score = scores[0];
min_score = scores[0];
max_index = 1;
min_index = 1;
for (i = 1; i < 10; i++) {
if (scores[i] > max_score) {
max_score = scores[i];
max_index = i + 1;
}
if (scores[i] < min_score) {
min_score = scores[i];
min_index = i + 1;
}
}
printf("最高成绩为%d,位置为%d;最低成绩为%d,位置为%d。\n", max_score, max_index, min_score, min_index);
return 0;
}
```
代码中先定义一个长度为10的整型数组`scores`,然后通过for循环从键盘输入10个成绩存入数组中。接着,使用for循环遍历数组,求出最高成绩、最低成绩以及它们在数组中的位置。注意,在计算位置时,由于C语言中的数组下标也是从0开始的,因此需要将位置加1。最后输出结果即可。
从键盘输入10个学生的c语言成绩到数组中,求平均分、最高分和最低分
题意:从键盘输入10个学生的C语言成绩,求平均分、最高分和最低分。
首先需要将输入的成绩存入一个数组中,然后可以通过遍历数组求出总成绩,从而计算平均分。最高分和最低分可以通过遍历数组比较每个成绩来得到。具体代码可以:
```python
scores = [] # 创建一个数组,用于存储成绩
# 通过for循环依次输入10位学生的成绩
for i in range(10):
score = float(input('请输入第{}个学生的成绩:'.format(i+1)))
scores.append(score) # 将输入的成绩添加到数组中
total_score = sum(scores) # 计算总成绩
average_score = total_score / len(scores) # 计算平均分
max_score = max(scores) # 获取最高分
min_score = min(scores) # 获取最低分
print('平均分为:{:.2f}'.format(average_score))
print('最高分为:{}'.format(max_score))
print('最低分为:{}'.format(min_score))
```