用c语言编码 用函数编程统计平均成绩。要求在主函数中输入学生成绩,输入负数时,结束输入,调用子函数Average()计算平均成绩,并输出结果。 函数原型:int Average(int score[],int n) ****要求输入提示信息为:"Input score: (输入负数结束)\n" (只提示一次) ****输入格式要求为“%d” ****输出格式要求为: "Total students are %d\n" "Average score is %d\n"
时间: 2023-08-04 22:03:30 浏览: 50
好的,这是一个简单的C语言程序,实现你的需求:
```c
#include <stdio.h>
int Average(int score[], int n) {
if (n == 0) {
return 0;
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += score[i];
}
return sum / n;
}
int main() {
printf("Input score: (输入负数结束)\n");
int scores[100];
int n = 0;
while (1) {
int score = 0;
scanf("%d", &score);
if (score < 0) {
break;
}
scores[n] = score;
n++;
}
int average_score = Average(scores, n);
printf("Total students are %d\n", n);
printf("Average score is %d\n", average_score);
return 0;
}
```
你可以将这段代码保存为一个C文件,比如 `average_score.c`,然后在命令行中编译它,运行生成的可执行文件即可。程序会在输入负数后计算平均成绩并输出结果。注意,程序假设输入的成绩都是有效的整数,如果输入非法字符可能会导致程序异常。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)