Write the program and enter the student ID, name, and score information, sorted from high to low

Source: Internet
Author: User

Requirement: write a program to enter the student's student ID, name, and score information, sorted from high to low according to the student's score.

# Include <stdio. h>
# Include <string. h>
Typedef struct student_score
{
Int ID;
Char name [20];
Int score;

} Stu; // defines the struct.

Int main (INT argc, const char * argv [])
{
Int I, J;
Int num;
Stu TMP, STU [100];

Printf ("how many students? Input the num :");
Scanf ("% d", & num); // enter the number of students

for (I = 0; I {< br> printf ("plz input the [num: % d] Student's ID: \ n ", I + 1);
scanf (" % d ", & STU [I]. ID);
puts ("plz input the name:");
scanf ("% s", STU [I]. name);
puts ("plz input the score:");
scanf ("% d", & STU [I]. score);
}< br>/*
to prevent incorrect input, use:
int ret;
ret = scanf ("% d % S % d", & STU [I]. ID, STU [I]. name, STU [I]. score);
If (Ret! = 3)
printf ("input error");
*/

printf ("=================================\ N ");
printf ("| ID name Score \ n");
for (I = 0; I {< br> printf ("| % d % S % d \ n", STU [I]. ID, STU [I]. name, STU [I]. score);
}< br> for (I = 0; I for (j = 0; j {// if the number of students is 3, the comparison is only twice. So the cycle condition: j

If (STU [J]. score <STU [J + 1]. Score)
{
TMP = STU [J];
STU [J] = STU [J + 1];
STU [J + 1] = TMP;
}
}
Printf ("=================================\ N ");
For (I = 0; I <num; I ++)
{
Printf ("| % d % S % d \ n", STU [I]. ID, STU [I]. Name, STU [I]. Score );
}
Return 0;
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.