有一个一维数组,内放10个学生成绩,写一个函数,当主函数调用此函数后,能求出最高分、最低分和平均分
时间: 2023-04-23 14:06:45 浏览: 145
以下是一个可以求出最高分、最低分和平均分的示例函数:
```
def statistics(scores):
max_score = max(scores)
min_score = min(scores)
avg_score = sum(scores) / len(scores)
return (max_score, min_score, avg_score)
```
使用方法:
```
scores = [90, 85, 88, 95, 76, 99, 100, 80, 92, 88]
max_score, min_score, avg_score = statistics(scores)
print("最高分:", max_score)
print("最低分:", min_score)
print("平均分:", avg_score)
```
相关问题
有一个一维数组,内放 10 个学生成绩,写一个函数,当主函数调用此函数后,能求出 平均分、最高分和最低分。
以下是一个可以求出最高分、最低分和平均分的示例函数:
```
def statistics(scores):
max_score = max(scores)
min_score = min(scores)
avg_score = sum(scores) / len(scores)
return (max_score, min_score, avg_score)
```
使用方法:
```
scores = [90, 85, 88, 95, 76, 99, 100, 80, 92, 88]
max_score, min_score, avg_score = statistics(scores)
print("最高分:", max_score)
print("最低分:", min_score)
print("平均分:", avg_score)
```
c语言有一个一维数组,内放10个学生成绩,写一个函数,当主函数调用此函数后,能求出最高分、最低分和平均分的代码
好的,以下是求最高分、最低分和平均分的C语言代码:
```c
#include <stdio.h>
void get_score_info(int score[], int n, int *max, int *min, float *avg) {
int sum = 0;
*max = score[0];
*min = score[0];
for (int i = 0; i < n; i++) {
if (score[i] > *max) {
*max = score[i];
}
if (score[i] < *min) {
*min = score[i];
}
sum += score[i];
}
*avg = (float)sum / n;
}
int main() {
int score[10] = {78, 89, 92, 67, 85, 73, 91, 88, 90, 84};
int max, min;
float avg;
get_score_info(score, 10, &max, &min, &avg);
printf("最高分:%d\n", max);
printf("最低分:%d\n", min);
printf("平均分:%.2f\n", avg);
return 0;
}
```
在这个代码中,我们定义了一个函数 `get_score_info` 来计算最高分、最低分和平均分。它接受一个整数数组 `score`、数组长度 `n`、一个指向最高分变量的指针 `max`、一个指向最低分变量的指针 `min` 和一个指向平均分变量的指针 `avg`。
在函数中,我们使用一个循环来遍历整个数组,并比较每个成绩与当前最高分和最低分。最后,我们计算出成绩的总和并计算出平均分。最高分、最低分和平均分的值都存储在传入函数的指针中。
在 `main` 函数中,我们定义了一个包含10个学生成绩的数组,并将其传递给 `get_score_info` 函数。最后,我们输出最高分、最低分和平均分。