编写程序实现下述功能:有10位学生的成绩:17、34、90、88、55、74、95、82、43、90、编写程序找出其中的最高分,并将最高分与第一个成绩交换位置。
时间: 2023-08-08 20:08:16 浏览: 98
好的,这是一个简单的程序,可以用C语言实现:
```c
#include <stdio.h>
int main() {
int scores[10] = {17, 34, 90, 88, 55, 74, 95, 82, 43, 90};
int max = scores[0]; // 初始化最高分为第一个成绩
int max_index = 0; // 记录最高分的下标
for (int i = 1; i < 10; i++) {
if (scores[i] > max) {
max = scores[i];
max_index = i; // 更新最高分和下标
}
}
scores[max_index] = scores[0]; // 将第一个成绩与最高分交换
scores[0] = max;
printf("最高分为:%d\n", max);
return 0;
}
```
程序首先定义了一个包含10个成绩的数组,并初始化了它的每一个元素。接着使用一个for循环遍历数组中的每一个元素,找到最高分并记录它的下标。然后将第一个成绩与最高分交换,并输出最高分即可。
相关问题
C语言编写程序实现下述功能:有10位学生的成绩:17、34、90、88、55、74、 95、82、43、90、编写程序找出其中的最高分,并将最高分与第一个成绩交换位置
以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int scores[] = {17, 34, 90, 88, 55, 74, 95, 82, 43, 90};
int max_score = scores[0];
int max_index = 0;
for (int i = 1; i < 10; i++) {
if (scores[i] > max_score) {
max_score = scores[i];
max_index = i;
}
}
printf("最高分是:%d,位于第%d个位置\n", max_score, max_index + 1);
int temp = scores[0];
scores[0] = max_score;
scores[max_index] = temp;
printf("将最高分与第一个成绩交换位置后的成绩为:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", scores[i]);
}
printf("\n");
return 0;
}
```
运行结果:
```
最高分是:95,位于第7个位置
将最高分与第一个成绩交换位置后的成绩为:
95 34 90 88 55 74 17 82 43 90
```
C语言编写程序实现下述功能:有10位学生的成绩:20、34、90、88、55、74、 95、82、17、90、编写程序找出其中的最低分,及其在这批数中的位置。
以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int scores[] = {20, 34, 90, 88, 55, 74, 95, 82, 17, 90};
int min_score = scores[0];
int min_index = 0;
for (int i = 1; i < 10; i++) {
if (scores[i] < min_score) {
min_score = scores[i];
min_index = i;
}
}
printf("最低分是:%d,位于第%d个位置\n", min_score, min_index + 1);
return 0;
}
```
运行结果:
```
最低分是:17,位于第9个位置
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)