Introduction
In this write-up, we’ll delve into a complete instance of a student management method implemented in the C programming language. This plan enables customers to handle a list of students, record their grades, search for students, and show relevant data. It serves as an outstanding illustration of struct usage, file organization, and the implementation of a menu-driven console application.
System Structure
Header and Definitions
The plan starts with which includes the important header files and defining constants. The MAX_STUDENTS and MAX_SUBJECTS constants identify the maximum quantity of students and subjects, respectively. The structure Student is defined to retailer student data, which includes their name, roll quantity, and grades.
Principal Function
The major function initializes an array of Student structures, students, and tracks the quantity of students applying the numStudents variable. It enters a loop exactly where the user is presented with a menu of selections, and the selected choice is executed by means of a switch statement.
Menu-Driven Choices
Add New Student: This choice prompts the user to enter information for a new student, which includes their name, roll quantity, and initializes their grades. The data is stored in the students array.
Record Grades: Customers can record grades for a particular student by getting into the student’s roll quantity. The plan then prompts the user to enter grades for every topic.
Show Grades: Customers can view the grades of a particular student by delivering the student’s roll quantity. The plan displays the grades for every topic along with the student’s name.
Search Student: Customers can search for a student either by roll quantity or name. The plan displays relevant data if the student is identified.
Modify Grades: Customers can modify the grades of a particular student by getting into the student’s roll quantity. The plan prompts the user to enter new grades for every topic.
Show Students: This choice displays a list of all students along with their names, roll numbers, grades for every topic, typical, and GPA (Grade Point Typical).
Exit: The plan exits the loop and terminates.
Complete supply code in C Programming
#include things like
// Define the maximum quantity of students
#define MAX_STUDENTS 50
// Define the maximum quantity of subjects
#define MAX_SUBJECTS 5
// Define the structure for a student
struct Student {
char name[50]
int rollNumber
int grades[MAX_SUBJECTS]
}
// Function to add a new student
void addNewStudent(struct Student students[], int *numStudents) {
if (*numStudents < MAX_STUDENTS) {
printf("Enter the details for the new student:n")
// Increment the number of students
(*numStudents)++
// Get the details from the user
printf("Name: ")
scanf("%s", students[*numStudents - 1].name)
printf("Roll Number: ")
scanf("%d", &students[*numStudents - 1].rollNumber)
// Initialize grades to -1 (indicating not yet recorded)
for (int i = 0 i < MAX_SUBJECTS i++) {
students[*numStudents - 1].grades[i] = -1
}
printf("Student added successfully!n")
} else {
printf("Maximum number of students reached!n")
}
}
// Function to record grades for a student
void recordGrades(struct Student students[], int numStudents) {
int rollNumber, subject
printf("Enter the roll number of the student: ")
scanf("%d", &rollNumber)
// Search for the student
int studentIndex = -1
for (int i = 0 i < numStudents i++) {
if (students[i].rollNumber == rollNumber) {
studentIndex = i
break
}
}
if (studentIndex != -1) {
printf("Enter grades for the student (subject-wise):n")
for (int i = 0 i < MAX_SUBJECTS i++) {
printf("Subject %d: ", i + 1)
scanf("%d", &students[studentIndex].grades[i])
}
printf("Grades recorded successfully for student %s!n", students[studentIndex].name)
} else {
printf("Student not found!n")
}
}
// Function to display grades for a student
void displayGrades(struct Student students[], int numStudents) {
int rollNumber
printf("Enter the roll number of the student: ")
scanf("%d", &rollNumber)
// Search for the student
int studentIndex = -1
for (int i = 0 i < numStudents i++) {
if (students[i].rollNumber == rollNumber) {
studentIndex = i
break
}
}
if (studentIndex != -1) {
printf("nGrades for student %s (Roll Number: %d):n", students[studentIndex].name, students[studentIndex].rollNumber)
for (int i = 0 i < MAX_SUBJECTS i++) {
printf("Subject %d: %dn", i + 1, students[studentIndex].grades[i])
}
printf("n")
} else {
printf("Student not found!n")
}
}
// Function to search for a student by roll number or name
void searchStudent(struct Student students[], int numStudents) {
int choice
printf("Search student by:n")
printf("1. Roll Numbern")
printf("2. Namen")
printf("Enter your choice: ")
scanf("%d", &choice)
if (choice == 1) {
int rollNumber
printf("Enter the roll number of the student: ")
scanf("%d", &rollNumber)
// Search for the student
int studentIndex = -1
for (int i = 0 i < numStudents i++) {
if (students[i].rollNumber == rollNumber) {
studentIndex = i
break
}
}
if (studentIndex != -1) {
printf("Student found!n")
printf("Name: %s, Roll Number: %dn", students[studentIndex].name, students[studentIndex].rollNumber)
} else {
printf("Student not found!n")
}
} else if (choice == 2) {
char name[50]
printf("Enter the name of the student: ")
scanf("%s", name)
// Search for the student
int studentIndex = -1
for (int i = 0 i < numStudents i++) {
if (strcmp(students[i].name, name) == 0) {
studentIndex = i
break
}
}
if (studentIndex != -1) {
printf("Student found!n")
printf("Name: %s, Roll Number: %dn", students[studentIndex].name, students[studentIndex].rollNumber)
} else {
printf("Student not found!n")
}
} else {
printf("Invalid choice. Please enter a valid option.n")
}
}
// Function to modify grades for a student
void modifyGrades(struct Student students[], int numStudents) {
int rollNumber
printf("Enter the roll number of the student: ")
scanf("%d", &rollNumber)
// Search for the student
int studentIndex = -1
for (int i = 0 i < numStudents i++) {
if (students[i].rollNumber == rollNumber) {
studentIndex = i
break
}
}
if (studentIndex != -1) {
printf("Enter the new grades for the student (subject-wise):n")
for (int i = 0 i < MAX_SUBJECTS i++) {
printf("Subject %d: ", i + 1)
scanf("%d", &students[studentIndex].grades[i])
}
printf("Grades modified successfully for student %s!n", students[studentIndex].name)
} else {
printf("Student not found!n")
}
}
// Function to display the list of students
void displayStudents(struct Student students[], int numStudents) {
printf("nList of Students:n")
for (int i = 0 i < numStudents i++) {
printf("Name: %s, Roll Number: %dn", students[i].name, students[i].rollNumber)
// Display grades and average
printf("Grades:n")
for (int j = 0 j < MAX_SUBJECTS; j++) {
float grade = students[i].grades[j];
if (grade > 0) {
printf(“Subject %d: %.2fn”, j + 1, grade)
} else {
printf(“Subject %d: %sn”, j + 1, “N/A”)
}
}
// Calculate and display average
int sum = 0
for (int j = 0 j < MAX_SUBJECTS; j++) {
sum += students[i].grades[j];
}
float average = (float)sum / MAX_SUBJECTS;
if (average > 0) {
printf(“Average: %.2fn”, average)
} else {
printf(“Average: %.2fn”, “N/A”)
}
// Calculate and display GPA
float gpa = 0.0
for (int j = 0 j < MAX_SUBJECTS; j++) {
float grade = students[i].grades[j];
if (grade >= 80) {
gpa += 4.0
} else if (grade>= 70) {
gpa += 3.
} else if (grade >= 70) {
gpa += 2.
} else if (grade >= 60) {
gpa += 1.
}
}
gpa /= MAX_SUBJECTS
printf(“GPA: %.2fn”, gpa)
printf(“n”)
}
}
int major() {
struct Student students[MAX_STUDENTS]
int numStudents =
int decision
do {
// Show menu
printf(“Menu:n”)
printf(“1. Add New Studentn”)
printf(“2. Record Gradesn”)
printf(“3. Show Gradesn”)
printf(“4. Search Studentn”)
printf(“5. Modify Gradesn”)
printf(“6. Show Studentsn”)
printf(“7. Exitn”)
printf(“Enter your decision: “)
scanf(“%d”, &decision)
switch (decision) {
case 1:
addNewStudent(students, &numStudents)
break
case 2:
recordGrades(students, numStudents)
break
case 3:
displayGrades(students, numStudents)
break
case 4:
searchStudent(students, numStudents)
break
case 5:
modifyGrades(students, numStudents)
break
case 6:
displayStudents(students, numStudents)
break
case 7:
printf(“Exiting plan.n”)
break
default:
printf(“Invalid decision. Please enter a valid choice.n”)
}
} even though (decision != 7)
return
}
Verify the beneath hyperlinks to get the complete write-up