← The C Language
Lesson 41 of 77 · 10 XP
Arrays of structs
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
Structs can be put into an array — that's how tables of data are stored in C:
struct student {
char name[32];
int score;
};
struct student group[30];
group[0].score = 95;
strcpy(group[0].name, "Kai");
A typical task is to find the item with the best value:
int best = 0;
for (int i = 1; i < n; i++)
if (group[i].score > group[best].score)
best = i;
printf("%s\n", group[best].name);
Note: we remember the index of the best one rather than copying the struct at every step.
Task. Read n (up to 50) and n lines of name score. Print the name and score of the best student. If there are several best ones, print the first of them.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.