C language programming exercise reference answer chapter 6 (1) Structured Comprehensive Exercises

Source: Internet
Author: User

/* 6.9 10 students, each with three course scores, calculate the average score and the first five */

# Include "stdio. H"
# Include "conio. H"
# Define N 6

Struct student/* define the struct data type */
{
Int num;
Char name [10];
Int score [3];/* float cannot be used */
Float average;
};

Void sort (struct student STU []);/* function prototype declaration, sorting */
Void print (struct student STU []);/* function prototype declaration, output */
Void printtopfive (struct student STU []);/* function prototype declaration, output top 5 */

void main ()
{< br> struct student s [N];/* s is the struct array */
int I;
for (I = 0; I {< br> printf ("Enter the student ID % d. Name Score 1 score 2 score 3 \ n", I + 1 );
scanf ("% d % S % d", & S [I]. num, s [I]. name, & S [I]. score [0],
& S [I]. score [1], & S [I]. score [2]);
S [I]. average = (s [I]. score [0] + s [I]. score [1] + s [I]. score [2])/3.0;
}< br> printf ("Original Score report \ n");
Print (s );
sort (s);
printf ("sorted score report \ n");
Print (s );
printf ("Top Five score reports \ n");
printtopfive (s);
getch ();
}

Void sort (struct student STU [])/* function, select sort */
{
Int I, K, J;
Struct Student t;
For (I = 0; I <N-1; I ++)
{
K = I;
For (j = I + 1; j <n; j ++)
{
If (STU [K]. Average <STU [J]. Average)
K = J;
If (K! = I)
{
T = STU [I]; STU [I] = STU [k]; STU [k] = T;
}
}

}
}

Void print (struct student STU [])/* function, output */
{
Int I;
Printf ("student ID Student name score1 score2 score3 average \ n ");
For (I = 0; I <n; I ++)
Printf ("%-10D %-12 S % 8d % 8d % 8d % 8.1f \ n", STU [I]. Num, STU [I]. Name,
STU [I]. Score [0], STU [I]. Score [1], STU [I]. Score [2], STU [I]. Average );
}

Void printtopfive (struct student STU [])/* function, output the first 5 */
{
Int I;
Printf ("Student name average \ n ");
For (I = 0; I <5; I ++)
Printf ("%-12 S % 8.1f \ n", STU [I]. Name, STU [I]. Average );
}

Related Article

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.