Skip to Content
Course content

67: Practice Exercise: Modeling a Student Record System with Structs

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think about how a registrar's office handles student files. They don't just throw a pile of names in one drawer, a pile of GPAs in another, and a pile of ID numbers in a third. If they did, trying to find the GPA for "John Doe" would be a nightmare—they'd have to hope the 14th name in the first drawer corresponds exactly to the 14th grade in the second. Instead, they use a physical folder. Everything belonging to one student—their name, their ID, their grades—is clipped together in a single unit. When you grab the folder, you have the complete picture.

In C, a struct is that folder. It allows us to stop treating related data as separate, disconnected variables and start treating them as a single object.

Designing the Student Blueprint

When I'm designing a system like this, I usually start by defining exactly what "belongs" in the folder. For a student record, we need a name, an ID number, and a GPA. If we did this with standard variables, we'd end up with student1_name, student1_id, student2_name, and so on. That doesn't scale. Here is how we build the blueprint instead:

typedef struct {
    char name[50];
    int id;
    float gpa;
} Student;

I used a typedef here because I'm lazy—and in a good way. Without it, I'd have to type struct Student myStudent; every single time. With the typedef, I can just use Student as a first-class type, just like int or float. It makes the code much cleaner to read.

Managing a Whole Classroom

One student record is great, but we usually deal with groups. This is where the power of structs really clicks. Since a Student is now a single type, we can create an array of them. It's like having a filing cabinet where every slot is a student folder.

Check out how we can initialize and access this data:

Student classroom[3] = {
    {"Alice Smith", 101, 3.8},
    {"Bob Jones", 102, 3.2},
    {"Charlie Brown", 103, 3.9}
};

// To access Bob's GPA, we just go to index 1 and use the dot operator
printf("Bob's GPA is %.2f", classroom[1].gpa);

The dot operator (.) is your way of reaching into the folder to pull out a specific piece of paper. classroom[1].gpa says: "Go to the second folder in the cabinet, and find the GPA slip."

Processing Records in Bulk

The real magic happens when you combine these structs with loops. I often see students try to write separate print statements for every field. Instead, you should write a function that takes a Student struct as an argument. This keeps your logic decoupled; the function doesn't care which student it's printing, only that it's been handed a valid "folder."

void printStudentCard(Student s) {
    printf("ID: %d | Name: %-15s | GPA: %.2f\n", s.id, s.name, s.gpa);
}

// Then in main:
for(int i = 0; i < 3; i++) {
    printStudentCard(classroom[i]);
}

Notice how printStudentCard makes the main function look incredibly tidy. We're passing the entire struct by value here, which is fine for small records. If our "folder" grew to hold 1,000 pages of data, I'd suggest passing a pointer instead to save memory, but for a simple record system, this is the most intuitive way to start.




📋 Practical Task

Build a Classroom GPA Analyzer

Your task is to create a program that manages a small set of student records and calculates the overall class average. This will test your ability to define structs, manage an array of those structs, and perform calculations across them.

Requirements:

  • Define a Student struct containing a name (string), id (int), and gpa (float).
  • Create an array of 5 Student records and initialize them with sample data.
  • Write a function called calculateClassAverage that takes the array of students and the number of students as arguments, returning the average GPA of the entire group.
  • In your main function, loop through the array to print each student's name and GPA, then print the final calculated class average at the bottom.
  • Bonus: Add a check to find and print the name of the student with the highest GPA.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.