C Student Data Management: Code Analysis & Implementation
This document presents a C program designed for managing student information. It includes header files, source code, and a main function demonstrating the program’s functionality.
studentinfo.h
#ifndef STUDENTINFO_H
#define STUDENTINFO_H
#define MAX_SIZE 10
typedef struct student{
char name[30];
char student_Ids[50];
int student_grades[10];
int gradeavg;
}student;
void print_students (student[], int);
student return_student(student[], int, char []);
#endif
studentinfo.c
#include "studentinfo.h"
#include <stdio.h>
#include <string.h>
void print_students (student student_list [], int size){
int i;
int studentavg;
for (i=0;i<size;i++){
student_list[i].gradeavg = (student_list[i].student_grades[0] + student_list[i].student_grades[1] + student_list[i].student_grades[2]) /3;
}
for (i=0; i < size; i++){
printf("%s %s %d\n", student_list[i].name, student_list[i].student_Ids, student_list[i].gradeavg);
}
}
student return_student(student student_list[], int size, char user_provided_code[] ){
int i;
for (i=0; i<size; i++){
if (strcmp(student_list[i].student_Ids, user_provided_code)==0){
return student_list[i];
}
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "studentinfo.h"
int main(int argc, char** argv) {
int size = 3;
student student_list[MAX_SIZE];
srand(2);
strcpy (student_list[0].name, "Abraham McLovin");
strcpy (student_list [0].student_Ids , "abc123");
student_list [0].student_grades[0] = 98;
student_list [0].student_grades[1] = 91;
student_list [0].student_grades[2] = 97;
strcpy (student_list[1].name, "Jorge John");
strcpy (student_list [1].student_Ids , "def456");
student_list [1].student_grades[0] = 26;
student_list [1].student_grades[1] = 48;
student_list [1].student_grades[2] = 51;
strcpy (student_list[2].name, "Jose Juan");
strcpy (student_list [2].student_Ids , "ghi789");
student_list [2].student_grades[0] = 78;
student_list [2].student_grades[1] = 70;
student_list [2].student_grades[2] = 75;
char user_provided_code[7];
student results;
int user_opt = 4;
while(user_opt > 0){
printf("please provide the operation you are looking for: \n");
printf("0 : exit\n");
printf("1 : print all students\n");
printf("2 : print SPECIFIC student information:\n");
scanf("%d",&user_opt);
switch(user_opt){
case 0:
return 0;
break;
case 1:
print_students(student_list,size);
printf("\n");
break;
case 2:
printf("please provide the student ID you are looking for:\n");
scanf("%s",user_provided_code);
results = return_student(student_list, size,user_provided_code);
printf("%s %s %d\n",results.name, results.student_Ids,results.student_grades[10] );
break;
default :
printf("please provide a valid input");
break;
}
}
return 0;
}
Key Features:
- Data Structure: Uses a
struct
namedstudent
to store student details. - Functions: Includes functions to print all students and return a specific student by ID.
- User Interaction: Provides a menu-driven interface for user interaction.
Potential Improvements:
- Error Handling: Add input validation and error handling.
- Grade Array Access: Fix the out-of-bounds access in the
main
function when printing a specific student’s grade. - Dynamic Memory: Consider using dynamic memory allocation for scalability.