Statistical Calculation of Student Score ACing and Student Score acing
1. Enter a positive integer n, then score of n students, calculate the average score, and count the number of students who fail.
Int count, I, n; // count is the number of failed records double grade, total; // grade stores the input scores, total stores the sum of the scores printf ("Enter n: "); scanf (" % d ", & n); // enter the number of students n total = 0; count = 0; for (I = 1; I <= n; I ++) {printf ("Enter grade # % d:", I); scanf ("% lf", & grade ); // enter the I score total = total + grade; if (grade <60) {// count the number of failed students + + ;}} printf ("Grade average = %. 2f \ n ", total/n); printf (" Number of failures = % d \ n ", count); return 0;
Test results:
2. Enter the scores of a group of students, use a negative number as the ending indicator, calculate the average score, and count the number of failed students.
Int count, num; // count indicates the number of failed records; num indicates the number of records input; double grade, total; // grade stores the input scores, total Save the sum of scores num = 0; total = 0; count = 0; printf ("Enter grades:"); scanf ("% lf", & grade ); while (grade> = 0) {total = total + grade; // accumulate the result num ++; // count if (grade <60) count ++; scanf ("% lf", & grade); // read a new data and compare it with if (num! = 0) {printf ("Grade average = %. 2f \ n ", total/num); printf (" Number of failures is % d \ n ", count);} else printf (" Grade average is 0 \ n "); return 0;
Test results:
3. Select the highest score from the input n scores and use the for statement.
Int I, mark, max, n; printf ("Enter n:"); scanf ("% d", & n ); // input data count printf ("Enter % d marks:", n); scanf ("% d", & mark); // read the first score max = mark; for (I = 1; I <n; I ++) {scanf ("% d", & mark); if (max <mark) max = mark ;} printf ("Max = % d \ n", max); return 0;
Test results:
Note: If the highest score is selected for the input batch ending with a negative number, you can use the while statement.
4. Enter the score information of n students, calculate and output the average personal score of each student (the structure variable is used as the function parameter ).
Struct student {// student information structure definition int num; // student ID char name [10]; // name int computer, english, math; // three course scores double average; // average personal score}; double count_average (struct student s); // function declaration, calculate the average personal score int main (void) {int I, n; struct student s1; // define the schema variable printf ("Input n:"); scanf ("% d", & n); printf ("Input the student's number, name, course scores \ n "); for (I = 1; I <= n; I ++) {printf (" No. % d: ", I); scanf (" % d % s % d ", & s1.num, & s1.name, & s1.math, & s1.english, & s1.computer ); s1.average = count_average (s1); // function call. The structure variable is used as the function parameter printf ("num: % d, name: % s, average: %. 2lf \ n ", s1.num, s1.name, s1.average);} return 0;} double count_average (struct student s) {return (s. math + s. english + s. computer)/3.0 ;}
Test results:
5. Enter the score information of n (n <50) students and output their information from high to low according to their average personal score.
Struct student {// student information structure definition int num; // student ID char name [10]; // name int computer, english, math; // three course scores double average; // average personal score}; int main (void) {int I, index, j, n; struct student students [50], temp; // define the structure array double count_average (struct student s);/* Input */printf ("Input n:"); scanf ("% d", & n ); for (I = 0; I <n; I ++) {printf ("Input the info of No. % d: \ n ", I + 1); printf (" number: "); scanf (" % d ", & students [I]. num); printf ("name:"); scanf ("% s", & students [I]. name); printf ("math. score: "); scanf (" % s ", & students [I]. math); printf ("english. score: "); scanf (" % s ", & students [I]. english); printf ("computer. score: "); scanf (" % s ", & students [I]. computer); students [I]. average = count_average (students [I]);}/* structure array sorting, select sorting method */for (I = 0; I <n-1; I ++) {index = I; for (j = I + 1; j <n; j ++) if (students [j]. average> students [index]. average) // compare the average score index = j; temp = students [index]; // exchange the array element students [index] = students [I]; students [I] = temp;}/* output the sorted information */printf ("num \ tname \ taverage \ n"); for (I = 0; I <n; I ++) printf ("% d \ t % s \ t %. 2lf \ n ", students [I]. num, students [I]. name, students [I]. average); return 0;} double count_average (struct student s) {return (s. math + s. english + s. computer)/3.0 ;}
Test results:
Note: In this question, the structure array students is defined to store student information. The basic information of n students is input first. The average personal score is calculated by calling the count_average () function, then, sort the selected sorting method based on the average score of the individual from high to low, and finally output the data in the structure array in order.