Student Report
Completed
Read students name m1 m2 m3. Print a report sorted by average descending (ties by name) as name avg grade (avg with 1 decimal; A ≥ 85, B ≥ 70, C ≥ 50, else F), then the class average.
Input
n, then n lines.
Output
n + 1 lines.
Example
Input
4 cy 90 80 100 ann 70 75 65 bob 40 55 50 dan 90 90 90
Output
cy 90.0 A dan 90.0 A ann 70.0 B bob 48.3 F class 74.6
Editor
C
Hints
Sort the struct array with insertion sort and a two-level comparison.
Solution explanation
Structs, sorting, helper functions and formatted output together make a complete report.
Show solution
#include <stdio.h>
#include <string.h>
typedef struct {
char name[30];
double avg;
} Student;
char grade(double avg) {
if (avg >= 85) return 'A';
if (avg >= 70) return 'B';
if (avg >= 50) return 'C';
return 'F';
}
int main() {
int n, a, b, c;
Student s[50];
double total = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s %d %d %d", s[i].name, &a, &b, &c);
s[i].avg = (a + b + c) / 3.0;
total += s[i].avg;
}
for (int i = 1; i < n; i++) {
Student key = s[i];
int j = i - 1;
while (j >= 0 && (s[j].avg < key.avg || (s[j].avg == key.avg && strcmp(s[j].name, key.name) > 0))) {
s[j + 1] = s[j];
j--;
}
s[j + 1] = key;
}
for (int i = 0; i < n; i++) printf("%s %.1f %c\n", s[i].name, s[i].avg, grade(s[i].avg));
printf("class %.1f\n", total / n);
return 0;
}