任务要求:定义3个浮点型数组用来存放学生的成绩,要求用数组名作为函数参数,求这三组学生的平均成绩。提示:定义一个求平均成绩的函数,一次定义,三次调用。
时间: 2024-02-01 22:13:45 浏览: 50
以下是实现该任务的代码:
```c
#include <stdio.h>
float average(float arr[3]) {
float sum = 0;
for(int i=0; i<3; i++) {
sum += arr[i];
}
return sum / 3;
}
int main() {
float scores1[3] = {80.5, 90.0, 75.5};
float scores2[3] = {70.0, 85.5, 92.0};
float scores3[3] = {88.0, 76.5, 82.5};
float avg1 = average(scores1);
float avg2 = average(scores2);
float avg3 = average(scores3);
printf("Average score of student 1: %.2f\n", avg1);
printf("Average score of student 2: %.2f\n", avg2);
printf("Average score of student 3: %.2f\n", avg3);
return 0;
}
```
运行结果如下:
```
Average score of student 1: 82.00
Average score of student 2: 82.50
Average score of student 3: 82.33
```