定义函数处理结构体数组,按成绩从高到低降序排列。 函数接口定义: struct stu { int num; char name[20]; int score; }; void fun ( struct stu *p, int n ); p是结构体数组的起始地址,n是结构体单元个数。 结构体成员:num是学号,name是姓名,score是成绩。 裁判测试程序样例: 在这里给出函数被调用进行测试的例子。例如: #include <stdio.h> struct stu{ int num; char name[20]; int score; }; void fun( struct stu *p, int n ); int main() { struct stu a[10]; int n, i; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%d%s%d", &a[i].num, a[i].name, &a[i].score); } fun(a,n); for(i = 0; i < n; i++) printf("%d %s %d\n", a[i].num, a[i].name, a[i].score); return 0; } /* 请在这里填写答案 */ 输入样例: 在这里给出一组输入。例如: 6 1001 anny 85 1002 boy 75 1003 car 65 1004 dan 55 1005 emm 95 1006 pa 35 输出样例: 在这里给出相应的输出。例如: 1005 emm 95 1001 anny 85 1002 boy 75 1003 car 65 1004 dan 55 1006 pa 35
时间: 2024-04-28 20:23:48 浏览: 12
void fun(struct stu *p, int n) {
int i, j;
struct stu temp;
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(p[j].score < p[j + 1].score) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
相关问题
struct student { int num ; char name[20]; int score;}; typedef struct student stu; 有上面面学生记录类型定义, 定义函数在一组学生记录中找成绩最低的学生信息, 最低分唯一
### 回答1:
可以定义一个函数,接收一个stu类型的数组作为参数,遍历数组,记录最低分和对应的学生信息,最后返回最低分对应的学生信息即可。
函数定义如下:
stu findLowestScore(stu arr[], int len) {
int minScore = arr[].score;
int minIndex = ;
for (int i = 1; i < len; i++) {
if (arr[i].score < minScore) {
minScore = arr[i].score;
minIndex = i;
}
}
return arr[minIndex];
}
其中,arr为stu类型的数组,len为数组长度。函数首先将第一个学生的成绩作为最低分,然后遍历数组,如果找到比最低分更低的成绩,就更新最低分和对应的学生信息。最后返回最低分对应的学生信息。
### 回答2:
要查找一组学生记录中最低分数的学生信息,我们需要遍历整个学生记录,依次比较每个学生的分数,记录下分数最低的学生信息即可。
下面是一个具体的函数实现:
```
stu find_lowest_score(stu* students, int num_students) {
int min_score = students[0].score;
int min_index = 0;
for (int i = 1; i < num_students; i++) {
if (students[i].score < min_score) {
min_score = students[i].score;
min_index = i;
}
}
return students[min_index];
}
```
在这个函数中,我们首先定义了两个变量,`min_score`表示当前最低的分数,`min_index`表示当前最低分数的学生在数组中的下标。然后从第一个学生开始遍历数组,如果发现当前学生的分数比`min_score`更低,则将`min_score`更新为当前学生的分数,同时记录下当前学生的下标。最后返回数组中`min_index`对应的学生信息即可。
调用这个函数的方法很简单,只需要将学生记录的数组和数组长度传递给这个函数即可:
```
int main() {
stu students[3] = {
{1, "Alice", 90},
{2, "Bob", 80},
{3, "Charlie", 70}
};
stu lowest = find_lowest_score(students, 3);
printf("Lowest score student information: %d %s %d\n", lowest.num, lowest.name, lowest.score);
return 0;
}
```
在这个示例程序中,我们定义了一个包含三个学生记录的数组,其中Alice的成绩最高,Charlie的成绩最低,因此函数的输出应该是:
```
Lowest score student information: 3 Charlie 70
```
这就完成了我们的目标,可以通过这个函数找出任何一组学生记录中最低分数的学生信息。
### 回答3:
首先,题目给出的学生记录类型定义很简单,包括学生的学号、姓名、成绩等三个属性。题目要求我们编写一个函数,可以在一组学生记录中找到成绩最低的学生信息,并且最低分是唯一的。
根据题目要求,我们可以写一个函数如下:
stu find_min_score(stu* list, int len){
int i, min_index = 0;
for(i = 1; i < len; i++){
if(list[i].score < list[min_index].score){
min_index = i;
}
}
return list[min_index];
}
参数列表中,我们需要传入一个包含多个学生记录的数组指针list,以及数组长度len。在函数体内部,我们使用循环遍历list数组中的每一个学生记录,同时记录下成绩最低的学生在数组中的下标min_index。循环结束后,我们返回list[min_index]即可。
需要注意的是,如果有多个学生的成绩都是最低的,我们应该保证返回其中任意一个即可,因为题目中要求最低分是唯一的,可以理解成只有一名学生的成绩是最低的。
完整的代码实现如下:
#include<stdio.h>
#include<string.h>
struct student{
int num;
char name[20];
int score;
};
typedef struct student stu;
stu find_min_score(stu* list, int len){
int i, min_index = 0;
for(i = 1; i < len; i++){
if(list[i].score < list[min_index].score){
min_index = i;
}
}
return list[min_index];
}
int main(){
stu list[3] = {{1001, "John", 90}, {1002, "Lucy", 80}, {1003, "Tom", 70}};
stu min_stu = find_min_score(list, 3);
printf("The student who got the lowest score is: \n");
printf("Number: %d\n", min_stu.num);
printf("Name: %s\n", min_stu.name);
printf("Score: %d\n", min_stu.score);
return 0;
}
运行结果为:
The student who got the lowest score is:
Number: 1003
Name: Tom
Score: 70
这说明我们编写的函数可以正常地找到成绩最低的学生信息。
定义一个 student 结构体如下: struct student { char name[20]; /*姓名*/ float score[4]; /*四门功课的成绩*/ float average; /*四门功课的平均成绩*/ }; 其中,name 中存放同学的姓名;score[4]数组中存放同学的英语、数学、物理、化学成绩;average中存放上述四门功课的平均成绩。 2) 编写下面5个函数。函数的具体要求: void inputFromKeyboard(struct student stu[], int num) 从键盘输入学生信息的函数。stu 为结构体数组,num 为数组中元素的个数。 void outputToScreen(struct student stu[], int num) 将学生信息输出到屏幕的函数。stu 为结构体数组,num 为数组中元素的个数。 void averageScore(struct student stu[], int num) 计算学生平均成绩的函数。stu 为结构体数组,num 为数组中元素的个数。 int compare(struct student stu1, struct student stu2) 对两个学生的平均成绩进行比较。若学生1的平均成绩高于学生2,返回值为1,否则返回值为0。stu1 和stu2为两个结构体变量,分别代表学生1和学生2。 void sortByAverageScore(struct student stu[], int num) 根据学生的平均成绩对学生信息整体排序的函数,其中两学生成绩比较要求调用函数compare。stu 为结构体数组,num 为数组中元素的个数。
1. 输入学生信息的函数 inputFromKeyboard:
```
void inputFromKeyboard(struct student stu[], int num) {
for (int i = 0; i < num; i++) {
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", stu[i].name);
printf("请输入第%d个学生的英语、数学、物理、化学成绩:", i+1);
for (int j = 0; j < 4; j++) {
scanf("%f", &stu[i].score[j]);
}
}
}
```
2. 输出学生信息的函数 outputToScreen:
```
void outputToScreen(struct student stu[], int num) {
printf("姓名\t英语\t数学\t物理\t化学\t平均成绩\n");
for (int i = 0; i < num; i++) {
printf("%s\t", stu[i].name);
for (int j = 0; j < 4; j++) {
printf("%.2f\t", stu[i].score[j]);
}
printf("%.2f\n", stu[i].average);
}
}
```
3. 计算学生平均成绩的函数 averageScore:
```
void averageScore(struct student stu[], int num) {
for (int i = 0; i < num; i++) {
float sum = 0;
for (int j = 0; j < 4; j++) {
sum += stu[i].score[j];
}
stu[i].average = sum / 4;
}
}
```
4. 对两个学生的平均成绩进行比较的函数 compare:
```
int compare(struct student stu1, struct student stu2) {
if (stu1.average > stu2.average) {
return 1;
} else {
return 0;
}
}
```
5. 根据学生的平均成绩对学生信息整体排序的函数 sortByAverageScore:
```
void sortByAverageScore(struct student stu[], int num) {
for (int i = 0; i < num - 1; i++) {
for (int j = 0; j < num - i - 1; j++) {
if (compare(stu[j], stu[j+1])) {
struct student temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
```