用c编写一个函数,求n个学生的平均成绩,并测试
时间: 2023-05-22 19:04:56 浏览: 70
Sure, I can help you with that. Here is an example code to calculate the average score of n students:
```c
#include <stdio.h>
float calculate_average(int n){
int i;
float score, sum = 0;
for(i = 1; i <= n; i++){
printf("Enter score of student %d: ", i); //ask for score
scanf("%f", &score); //receive score from user
sum += score; //add score to sum
}
return (sum/n); //return the average
}
int main(){
int n;
float avg;
printf("How many students? ");
scanf("%d", &n); //receive number of students from user
avg = calculate_average(n);
printf("\nAverage score is: %.2f", avg); //print the average
return 0;
}
```
You can test this code by compiling and running it.