谢谢您的回答 可是还有一道跟它相关的题#include #include struct student{\x05int id;\x05int score;};void sort(struct student* students,int n){/*Sort the n students based on their score*/ /* Remember,each student must be matched with th

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/18 10:22:29
谢谢您的回答可是还有一道跟它相关的题#include#includestructstudent{\x05intid;\x05intscore;};voidsort(structstudent*stud

谢谢您的回答 可是还有一道跟它相关的题#include #include struct student{\x05int id;\x05int score;};void sort(struct student* students,int n){/*Sort the n students based on their score*/ /* Remember,each student must be matched with th
谢谢您的回答 可是还有一道跟它相关的题
#include
#include
struct student{
\x05int id;
\x05int score;
};
void sort(struct student* students,int n){
/*Sort the n students based on their score*/
/* Remember,each student must be matched with their original score after sorting */
}
int main(){
/*Declare an integer n and assign it a value.*/
/*Allocate memory for n students using malloc.*/
/*Generate random IDs and scores for the n students,using rand().*/
/*Print the contents of the array of n students.*/
/*Pass this array along with n to the sort() function*/
/*Print the contents of the array of n students.*/
return 0;
}

谢谢您的回答 可是还有一道跟它相关的题#include #include struct student{\x05int id;\x05int score;};void sort(struct student* students,int n){/*Sort the n students based on their score*/ /* Remember,each student must be matched with th

#include <stdio.h>
#include <stdlib.h>
struct student{
int id;
int score;
};
void sort(struct student* number, int n){
     int i,j;
     struct student t;
     for(i=0; i<n; i++)
         for(j=i+1; j<n; j++)
             if (number[j].score>number[i].score)
             {
                 t = number[j];
                 number[j] = number[i];
                 number[i] = t;
             }
}
 
int main(){
    int n = 20;
    int i;
     
    struct student* number= (int *)malloc(n * sizeof(struct student));

    for(i=0; i<n; i++) {
        number[i].id = rand() % (n+1);
        number[i].score = rand() % 100;
    }

    for(i=0; i<n; i++) {
        printf("number[%d].id=%d\n", i, number[i].id);
        printf("number[%d].score=%d\n", i, number[i].score);
    }
 
    sort(number, n);
     
    for(i=0; i<n; i++) {
        printf("number[%d].id=%d\n", i, number[i].id);
        printf("number[%d].score=%d\n", i, number[i].score);
    }
     
    free(number);
    return 0;
}