写一个函数,当主函数调用此函数能求出10个学生的平均成绩、最高成绩和最低成绩
时间: 2024-06-14 15:03:45 浏览: 9
```python
def calculate_scores(scores):
average_score = sum(scores) / len(scores)
max_score = max(scores)
min_score = min(scores)
return average_score, max_score, min_score
# 调用函数
student_scores = [85, 90, 78, 92, 88, 76, 85, 80, 94, 87]
average, highest, lowest = calculate_scores(student_scores)
print("Average score:", average)
print("Highest score:", highest)
print("Lowest score:", lowest)
```
相关问题
有一个一维数组,内放 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语言写一个在一维数组内放十个学生成绩写一个函数当主函数调用此函数后能求出学生成绩最高分和对应学号并对学生成绩从高到低进行排序
好的,我可以帮您写这个程序。以下是代码实现:
```c
#include <stdio.h>
// 定义学生类型
typedef struct {
int id; // 学号
int score; // 成绩
} Student;
// 定义函数获取最高分和对应学号
void get_max_score(Student students[], int size, int *max_score, int *max_id) {
*max_score = students[0].score; // 假设第一个学生的成绩最高
*max_id = students[0].id; // 假设第一个学生的学号对应最高分
for (int i = 1; i < size; i++) {
if (students[i].score > *max_score) {
*max_score = students[i].score;
*max_id = students[i].id;
}
}
}
// 定义函数按成绩从高到低排序
void sort_by_score(Student students[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (students[i].score < students[j].score) {
// 交换学生信息
int temp_id = students[i].id;
students[i].id = students[j].id;
students[j].id = temp_id;
int temp_score = students[i].score;
students[i].score = students[j].score;
students[j].score = temp_score;
}
}
}
}
int main() {
Student students[10] = {
{1, 90},
{2, 85},
{3, 92},
{4, 78},
{5, 88},
{6, 95},
{7, 81},
{8, 87},
{9, 83},
{10, 79}
};
int max_score, max_id;
get_max_score(students, 10, &max_score, &max_id);
sort_by_score(students, 10);
printf("学生成绩最高分:%d,对应学号:%d\n", max_score, max_id);
printf("学生成绩从高到低排序:\n");
for (int i = 0; i < 10; i++) {
printf("学号:%d,成绩:%d\n", students[i].id, students[i].score);
}
return 0;
}
```
运行结果如下:
```
学生成绩最高分:95,对应学号:6
学生成绩从高到低排序:
学号:6,成绩:95
学号:3,成绩:92
学号:1,成绩:90
学号:5,成绩:88
学号:8,成绩:87
学号:2,成绩:85
学号:9,成绩:83
学号:7,成绩:81
学号:10,成绩:79
学号:4,成绩:78
```
相关推荐
![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)