📦 Comprehensive Practical Guide to C Language Structures: Student Grade Management System
Author: IoT Smart Classroom
0. Before Writing Code, Imagine a Real-Life Scenario
Imagine this scenario:
- The class teacher has a pile of student grades: paper forms, Excel sheets, screenshots from DingTalk…
- Wants to know: Who has the highest score? Who has the lowest score? What is the average score? What is the score of a specific student?
- Flipping through them every time is quite troublesome.
Now, let’s write a simple and user-friendly system in C language:
- 📝 Input student information
- 👀 Want to see it all? One-click “Show All”
- 🏆 Want to see the top students? One-click “Sort by Score”
- 🔍 Want to check a specific student ID? Just input the ID and it will find it for you
- 📊 Want to see statistics? Highest, lowest, average scores at the click of a button
This is the prototype of an “Information Management System.” In the future, when you do “Sensor Management” or “Device Status Monitoring”, the pattern will be exactly the same.
1. The System We Are Going to Build Looks Like This
The program will run and interact like this:
===== Student Grade Management System =====
1. Input Student Information
2. Show All Student Information
3. Sort by Score (High to Low)
4. Query Student by ID
5. Statistics: Highest, Lowest, Average Score
0. Exit System
===========================
Please enter your choice: 1
Please enter the number of students to input: 3
Please enter the 1st student's ID Name Score: 1001 Zhang 89
Please enter the 2nd student's ID Name Score: 1002 Li 95
Please enter the 3rd student's ID Name Score: 1003 Wang 76
Input complete, total number of students: 3
Choosing 2 will display all, choosing 3 will sort, choosing 4 will query… This is a “small menu system.”
Now, let’s break it down from 0 to 1 and explain it clearly.
2. Step One: Teach the Computer “What is a Student” 👨🎓
Previously, we did it like this:
int id;
char name[20];
float score;
A student consists of 3 variables, which is barely manageable. What about 10 students? That would require 30 variables, which is overwhelming.
👉 Therefore, we use a structure to package it:
typedef struct {
int id; // Student ID
char name[20]; // Name
float score; // Score
} Student;
You can explain to the students like this:
“I invented a new type called
<span>Student</span>, just like int is an integer, float is a decimal,<span>Student</span>is—‘a whole person’ with an ID, name, and score.”
What if I have a bunch of students?
#define MAX_STU 100
Student stu[MAX_STU]; // Up to 100 students
int count = 0; // Current number of students
Remember two key points:
<span>stu[i]</span>: The i-th student (as a whole)<span>stu[i].score</span>: The score of the i-th student
The “dot” is a key symbol: “Find this person, then get some information about them”.
3. Step Two: Create a “Receptionist” that Never Closes (Menu Loop)
We want the system to behave like this:
- It should not exit immediately after running
- Instead, it should keep asking you: “What would you like to do?”
👉 Therefore, use a <span>while(1)</span> loop + <span>switch</span>:
int main() {
int choice;
while (1) { // Infinite loop until you choose to exit
printf("\n===== Student Grade Management System =====\n");
printf("1. Input Student Information\n");
printf("2. Show All Student Information\n");
printf("3. Sort by Score (High to Low)\n");
printf("4. Query Student by ID\n");
printf("5. Statistics: Highest, Lowest, Average Score\n");
printf("0. Exit System\n");
printf("===========================\n");
printf("Please enter your choice:");
scanf("%d", &choice);
switch (choice) {
case 1: addStudents(); break;
case 2: showStudents(); break;
case 3: sortByScoreDesc(); break;
case 4: searchById(); break;
case 5: statistics(); break;
case 0:
printf("Exited the system, goodbye!\n");
return 0;
default:
printf("Invalid option, please try again!\n");
}
}
}
Key points to explain (you can explain this to the students):
<span>while(1)</span>: Means “the program keeps running unless you manually exit”<span>switch(choice)</span>: This is the receptionist asking you “Which service do you choose?”- Each
<span>case</span>calls a function: clean code, clear logic
4. Step Three: Explain Each Function One by One
Below is the complete code and detailed explanation.
Function 1: Input Student Information (addStudents)
Requirement: 📥 The teacher says “I want to input 3 students,” so we let them input one by one.
void addStudents() {
int n;
printf("Please enter the number of students to input:");
scanf("%d", &n);
if (n <= 0) {
printf("The number must be greater than 0.\n");
return;
}
if (count + n > MAX_STU) {
printf("Exceeds maximum capacity, currently can input %d more people.\n", MAX_STU - count);
return;
}
for (int i = 0; i < n; i++) {
printf("Please enter the %d-th student's ID Name Score:", count + 1);
scanf("%d %s %f",
&stu[count].id,
stu[count].name,
&stu[count].score);
count++;
}
printf("✅ Student information input completed, currently there are %d students.\n", count);
}
Key points to explain to the students:
<span>stu[count]</span>: Where the total number ends, the new student stands there- After inputting one,
<span>count++</span>: The length of the queue increases by 1 - Note that
<span>name</span>does not add<span>&</span>because it is the array name (address)
Function 2: Show All Students (showStudents)
void showStudents() {
if (count == 0) {
printf("Currently, there is no student information, please input first.\n");
return;
}
printf("\nNo. ID Name Score\n");
for (int i = 0; i < count; i++) {
printf("%d %d %s %.2f\n",
i + 1, stu[i].id, stu[i].name, stu[i].score);
}
}
Explanation:
<span>for</span>runs from 0 to<span>count-1</span>- Each line represents one student
- This function is the most intuitive, allowing students to see “the structure array is really working”
Function 3: Sort by Score from High to Low (sortByScoreDesc)
This part is the intersection of “algorithm + structure,” focusing on explaining “the whole student moves together.”
void sortByScoreDesc() {
if (count == 0) {
printf("No student information, cannot sort.\n");
return;
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
printf("✅ Sorted by score from high to low.\n");
showStudents();
}
You can explain it like this:
- It’s bubble sort, but instead of swapping two ints, it swaps “two whole students”
- Cannot just swap scores, otherwise the scores will get mixed up, and no one will know whose score belongs to whom
On the blackboard/projector, you can draw a simple illustration: “People and scores must move together”
Function 4: Query by Student ID (searchById)
void searchById() {
if (count == 0) {
printf("Currently, there is no student data, cannot query.\n");
return;
}
int id;
printf("Please enter the student ID to query:");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (stu[i].id == id) {
printf("Found student: ID=%d Name=%s Score=%.2f\n",
stu[i].id, stu[i].name, stu[i].score);
return;
}
}
printf("Student information for this ID not found.\n");
}
Key points for explanation:
- This is called sequential search (checking one by one)
- It’s sufficient and easy to understand
- For advanced learning, it can be changed to “first sort by ID, then use binary search”
Function 5: Statistics (statistics)
void statistics() {
if (count == 0) {
printf("Currently, there is no student data, cannot perform statistics.\n");
return;
}
float max = stu[0].score;
float min = stu[0].score;
float sum = 0.0f;
for (int i = 0; i < count; i++) {
if (stu[i].score > max) max = stu[i].score;
if (stu[i].score < min) min = stu[i].score;
sum += stu[i].score;
}
printf("Total number of students: %d\n", count);
printf("Highest score: %.2f\n", max);
printf("Lowest score: %.2f\n", min);
printf("Average score: %.2f\n", sum / count);
}
This can be likened to:
“This function helps the teacher automatically calculate: ‘What is the highest score in the class? What about the lowest? How about the average?’”
It also serves as a review of maximum, minimum, and average along with loops.
5. Common “Pitfalls” to Warn Students About 😂
You can add this section at the end, both humorous and helpful to avoid pitfalls:
-
Missing a dot
stu[i]id = 1001; // ❌ stu[i].id = 1001; // ✅ -
Using an array as a variable
stu.id = 1001; // ❌ stu is an array stu[0].id = 1001; // ✅ -
Improper string assignment
stu[0].name = "Zhang"; // ❌ // Use scanf or strcpy -
Sorting only by swapping scores
// Only swapping scores, not swapping IDs and names // Everything gets mixed up, no one knows whose score belongs to whom