编写两个函数,分别求一组学生C语言课程成绩的平均值和最小值。要求:10个学生成绩为整数,用数组存储。
时间: 2024-03-05 16:53:12 浏览: 23
好的,我可以帮你编写这两个函数。以下是示例代码:
```c
#include <stdio.h>
// 求平均值
float average(int scores[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += scores[i];
}
return (float)sum / n;
}
// 求最小值
int min(int scores[], int n) {
int min_score = scores[0];
for (int i = 1; i < n; i++) {
if (scores[i] < min_score) {
min_score = scores[i];
}
}
return min_score;
}
int main() {
int scores[10] = { 90, 85, 92, 78, 80, 86, 88, 84, 89, 87 };
int n = 10;
float avg = average(scores, n);
int min_score = min(scores, n);
printf("平均分数为 %.2f\n", avg);
printf("最低分数为 %d\n", min_score);
return 0;
}
```
在这个示例代码中,我们定义了两个函数:`average` 和 `min`。`average` 函数用于计算给定数组中所有元素的平均值,而 `min` 函数用于找到给定数组中的最小值。在 `main` 函数中,我们定义了一个包含10个整数的数组,并将其传递给我们刚刚定义的函数来计算平均值和最小值。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)