用函数编程统计平均成绩。要求在主函数中输入学生成绩,输入负数时,结束输入,调用子函数Average()计算平均成绩,并输出结果。 函数原型:int Average(int score[],int n) ****要求输入提示信息为:"Input score:" ****输出格式要求为: "Total students are %d\n" "Average score is %d\n" 程序运行示例: Input score:78 Input score:98 Input score:56 Input score:79 Input score:67 Input score:-1 Total students are 5 Average score is 75
时间: 2024-02-29 07:53:25 浏览: 7
很抱歉,您刚才提出的问题我已经回答了。以下是之前回答的代码,希望能够帮到您:
```c++
#include <iostream>
using namespace std;
int Average(int score[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += score[i];
}
return sum / n;
}
int main() {
const int MAXSIZE = 100;
int score[MAXSIZE];
int n = 0;
while (true) {
cout << "Input score:";
int s;
cin >> s;
if (s < 0) {
break;
}
score[n++] = s;
}
int avg = Average(score, n);
cout << "Total students are " << n << endl;
cout << "Average score is " << avg << endl;
return 0;
}
```
输出结果:
```
Input score:78
Input score:98
Input score:56
Input score:79
Input score:67
Input score:-1
Total students are 5
Average score is 75
```
相关推荐
![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)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)