Input a set of student data, output according to grades from big to small, the number of students can be modified

#include<stdio.h>
struct student/*define structure*/
{     int num;     char name[20];     float score; }; int main() {     void rank(struct student p[], int n);     void print(struct student p[], int n);     int i;     int n = 3;/*number of students*/     struct student stu[3];/*when the number of students is modified, please modify here accordingly*/     for (i = 0; i <n; i++)         scanf("%d%s%f", &stu[i].num, stu[i].name, &stu[i].score);     printf("the student rank of grade after dispose is:\n");     rank(stu, n);     print(stu, n);     return 0; } void rank(struct student p[], int n)/*processing function*/ {




















    int i, j, k;
    struct student temp;
    for (i = 0; i <n-1; i++)
    {         k = i; /*store the current student serial number*/         for (j = i + 1; j < n; j++)             if (p[j].score> p[k].score)/*Compare the stored student data with the following student data at one time*/                 k = j;/*The serial number of the student with the highest score*/         temp = p[i];         p[i] = p[k];         p[k] = temp;/*Exchange with the first*/     } } void print(struct student p[], int n)/*output Function*/ {     int i;     for (i = 0; i <n; i++)         printf("%6d%8s,%6.2f\n", p[i].num, p[i].name, p[ i].score);     printf("\n"); }















Guess you like

Origin blog.csdn.net/progess_every_day/article/details/104877238