(1)输入5个学生3门课的成绩存入结构体数组中;(2)输出学生信息;(3)求每个学生的平均分;(4)按平均分从高到低排列;(5)按平均分求每个学生的名次。要求:用结构体数组存放学生信息,每个功能都用函数实现。
时间: 2023-05-22 15:03:56 浏览: 145
好的,以下是我的回答:
1. 首先我们定义一个结构体来存放学生信息:
```
struct Student {
char name[20];
int score[3];
float avgScore;
int rank;
};
```
2. 然后我们定义一个函数来输入学生信息并存入结构体数组:
```
void inputStudentInfo(Student student[], int n) {
printf("请输入%d个学生的信息:\n", n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", student[i].name);
for (int j = 0; j < 3; j++) {
printf("请输入第%d个学生第%d门课的成绩:", i+1, j+1);
scanf("%d", &student[i].score[j]);
}
}
}
```
3. 然后我们定义一个函数来输出学生信息:
```
void outputStudentInfo(Student student[], int n) {
printf("学生信息如下:\n");
for (int i = 0; i < n; i++) {
printf("姓名:%s,成绩:", student[i].name);
for (int j = 0; j < 3; j++) {
printf("%d ", student[i].score[j]);
}
printf("\n");
}
}
```
4. 接下来我们定义一个函数来计算每个学生的平均分并存入结构体:
```
void calculateAvgScore(Student student[], int n) {
for (int i = 0; i < n; i++) {
float sum = 0;
for (int j = 0; j < 3; j++) {
sum += student[i].score[j];
}
student[i].avgScore = sum / 3;
}
}
```
5. 然后我们定义一个函数来按平均分从高到低排列:
```
void sortByAvgScore(Student student[], int n) {
int i, j;
Student temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (student[i].avgScore < student[j].avgScore) {
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
}
```
6. 最后我们定义一个函数来按平均分求每个学生的名次:
```
void calculateRank(Student student[], int n) {
for (int i = 0; i < n; i++) {
student[i].rank = 1;
for (int j = 0; j < n; j++) {
if (student[j].avgScore > student[i].avgScore) {
student[i].rank++;
}
}
}
}
```
最后我们在主函数中调用这些函数即可:
```
int main() {
const int n = 5;
Student student[n];
// 输入学生信息
inputStudentInfo(student, n);
// 输出学生信息
outputStudentInfo(student, n);
// 计算每个学生的平均分
calculateAvgScore(student, n);
// 按平均分从高到低排列
sortByAvgScore(student, n);
// 按平均分求每个学生的名次
calculateRank(student, n);
// 输出学生信息,包括平均分和名次
printf("按平均分从高到低排列后的学生信息如下:\n");
printf("姓名 成绩 平均分 名次\n");
for (int i = 0; i < n; i++) {
printf("%-7s %-3d %-3d %-3d %.2f %-3d\n", student[i].name, student[i].score[0], student[i].score[1], student[i].score[2], student[i].avgScore, student[i].rank);
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)