/* 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 );
}