用C语言编写,在主函数中定义一个数组,内放10个学生的成绩,写一个函数,用指针做函数参数,当主函数调用此函数后,能求出平均分,最高分和最低分三个数值。
时间: 2024-05-04 20:20:46 浏览: 15
可以使用以下的代码实现:
```c
#include <stdio.h>
void calculate(int *arr, int size, float *avg, int *max, int *min)
{
int sum = 0;
*max = *min = arr[0];
for (int i = 0; i < size; i++) {
sum += arr[i];
if (arr[i] > *max) {
*max = arr[i];
}
if (arr[i] < *min) {
*min = arr[i];
}
}
*avg = (float)sum / size;
}
int main()
{
int scores[10] = {85, 72, 90, 64, 78, 93, 81, 69, 87, 75};
float average;
int max, min;
calculate(scores, 10, &average, &max, &min);
printf("Average score: %.2f\n", average);
printf("Highest score: %d\n", max);
printf("Lowest score: %d\n", min);
return 0;
}
```
输出:
```
Average score: 79.40
Highest score: 93
Lowest score: 64
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_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)