Store student records in an array of structures and print the student with the highest CGPA.
#include <stdio.h>
struct student {
char name[20];
int roll;
double cgpa;
};
const struct student *topper(const struct student *s, int n) {
const struct student *best = &s[0];
for (int i = 1; i < n; i++)
if (s[i].cgpa > best->cgpa)
best = &s[i];
return best;
}
int main(void) {
struct student cls[] = {{"Asha", 1, 8.7}, {"Ravi", 2, 9.4}, {"Meera", 3, 9.1}};
const struct student *t = topper(cls, 3);
printf("Topper: %s (roll %d), CGPA %.1f\n", t->name, t->roll, t->cgpa);
return 0;
}
Topper: Ravi (roll 2), CGPA 9.4
Returning a pointer to the best record avoids copying the struct. const shows the function only reads the array.