🧠 ULTIMATE C EXERCISE: Student Score
System
📌 Problem Description
You are asked to build a student management system.
Each student:
has a name
has an ID
has 3 subjects
each subject has a name and a score
You must:
store students in an array
use pointers to access and modify them
use functions to operate on the data
modify struct data via pointer
🧱 Data Structures (YOU MUST USE THESE)
#define SUBJECTS 3
struct Subject {
char name[30];
float score;
};
struct Student {
int id;
char name[50];
struct Subject sub[SUBJECTS];
};
🛠️Required Functions (MANDATORY)
You must implement ALL of the following:
1️⃣ Input ONE student (by pointer)
void inputStudent(struct Student *s);
Fill all fields of ONE student
Must use fgets for strings
Must remove newline correctly
2️⃣ Input MANY students (array + pointer)
void inputAll(struct Student *arr, int n);
Use pointer arithmetic: (arr + i)
Call inputStudent
3️⃣ Calculate average score of ONE student
float averageScore(const struct Student *s);
Must NOT modify student
Return average of 3 subjects
4️⃣ Increase score of ALL students (pointer modification)
void bonusAll(struct Student *arr, int n, float bonus);
Add bonus to every subject score
Cap score at 10.0
5️⃣ Print ONE student
void printStudent(const struct Student *s);
Output format:
ID: 101
Name: Alice
Math: 8.5
Physics: 7.0
Chemistry: 9.0
Average: 8.17
6️⃣ Print ALL students
void printAll(const struct Student *arr, int n);
🧪 main() REQUIREMENTS
Your main() MUST:
1. Ask for number of students
2. Declare array of struct Student
3. Use a pointer to the array
4. Call:
o inputAll
o printAll
o bonusAll
o printAll again
❌ RULES (IMPORTANT)
❌ No global variables
❌ No dynamic memory (malloc)
❌ No shortcuts (no direct array access inside functions unless required)
✅ Must use ->, [], pointer arithmetic
✅ Must use const where appropriate
1 #include <stdio.h>
2 #include <string.h>
3
4 #define SUBJECTS 3
5
6 struct Subject{
7 char name[30];
8 float score;
9 };
1
0
1 struct Student{
1
1 int id;
2
1 char name[50];
3
1 struct Subject sub[SUBJECTS];
4
1 };
5
1
6
1 void inputStudent(struct Student *s){
7
1 printf("Enter ID: ");
8
1 scanf("%d", &s->id);
9
2 getchar();
0
2 printf("Enter name: ");
1
2 fgets(s->name, sizeof(s->name), stdin);
2
2 s->name[strcspn(s->name, "\n")] = '\0';
3
2 printf("Subjects detail:\n");
4
2 for(int i = 0; i < SUBJECTS; i++){
5
2 printf("Subject: ");
6
2 fgets(s->sub[i].name, sizeof(s->sub[i].name), stdin);
7
2 s->sub[i].name[strcspn(s->sub[i].name, "\n")] = '\0';
8
2 printf("Score: ");
9
3 scanf("%f", &s->sub[i].score);
0
3 getchar();
1
3 }
2
3 }
3
3
4
3 void inputAll(struct Student *arr, int n){
5
3 for(int i = 0; i < n; i++){
6
3 inputStudent(arr + i);
7
3 }
8
3 }
9
4
0
4 float averageScore(const struct Student *s){
1
4 float result = 0;
2
4 for(int i = 0; i < 3; i++){
3
4 result += s->sub[i].score;
4
4 }
5
4 result /= 3;
6
4 return result;
7
4 }
8
4
9
5 void bonusAll(struct Student *arr, int n, float bonus){
0
5 for(int i = 0; i < n; i++){
1
5 for(int j = 0; j < SUBJECTS; j++){
2
5 (arr + i)->sub[j].score += bonus;
3
5 if((arr + i)->sub[j].score > 10) (arr + i)->sub[j].score = 10;
4
5 }
5
5 }
6
5 }
7
5
8
5 void printStudent(const struct Student *s){
9
6 printf("ID: %d\n", s->id);
0
6 printf("Name: %s\n", s->name);
1
6 printf("Math: %.1f\n", s->sub[0].score);
2
6 printf("Physics: %.1f\n", s->sub[1].score);
3
6 printf("Chemistry: %.1f\n", s->sub[2].score);
4
6 }
5
6
6
6 void printAll(const struct Student *arr, int n){
7
6 for(int i = 0; i < n; i++){
8
6 printStudent(arr + i);
9
7 }
0
7 }
1
7
2
7 int main(){
3
7 int n;
4
7 printf("The number of students: ");
5
7 scanf("%d", &n);
6
7 getchar();
7
7 struct Student list[n];
8
7 struct Student *p = list;
9
8 inputAll(list, n);
0
8 printAll(list, n);
1
8 bonusAll(list, n, 0.5);
2
8 printAll(list, n);
3
8 }
4