C语言输入5位同学语文、数学成绩,编写函数求出总分最好的成绩值。 要求:定义一个求两数之和的函数,通过多次调用函数得出结果。
时间: 2024-02-11 13:08:16 浏览: 32
好的,以下是 C 语言的代码实现:
```c
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int max_score = 0;
int chinese_score, math_score, total_score;
for(int i = 0; i < 5; i++) {
printf("请输入第%d位同学的语文成绩:", i+1);
scanf("%d", &chinese_score);
printf("请输入第%d位同学的数学成绩:", i+1);
scanf("%d", &math_score);
total_score = sum(chinese_score, math_score);
if(total_score > max_score) {
max_score = total_score;
}
}
printf("总分最好的成绩值为:%d\n", max_score);
return 0;
}
```
这段代码定义了一个求两数之和的函数 `sum()`,并通过循环调用该函数计算出每个同学的总成绩,最后找出总分最好的成绩值。注意,为了能够在函数内部修改 `max_score` 变量的值,我们需要在 `main()` 函数中定义 `max_score` 变量,并将其传递给 `sum()` 函数进行比较。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)