0% found this document useful (0 votes)
4 views7 pages

Student Database Management System

The document presents a C program that implements a student database using an array of structures. It allows users to add, display, search, sort, and modify student records, including attributes such as roll number, name, program, course, and marks. The program features a menu-driven interface for user interaction and handles operations like calculating total and average marks for students.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Student Database Management System

The document presents a C program that implements a student database using an array of structures. It allows users to add, display, search, sort, and modify student records, including attributes such as roll number, name, program, course, and marks. The program features a menu-driven interface for user interaction and handles operations like calculating total and average marks for students.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical -1 Code : Implement a student database with attributes using an array of structures.

#include <stdio.h>
#include <string.h>

#define MAX 100


#define SUBJECTS 5

typedef struct {
int rollNo;
char name[50];
char program[30];
char course[30];
int marks[SUBJECTS];
int total;
float average;
} Student;

Student students[MAX];
int count = 0;

// Function Prototypes
void addStudent();
void displayAll();
void searchStudent();
void sortStudents();
void modifyStudent();

int main() {
int choice;

do {
printf("\n=== Student Result Management System ===\n");
printf("1. Add Student\n");
printf("2. Display All Students\n");
printf("3. Search by Roll Number\n");
printf("4. Sort by Total Marks\n");
printf("5. Modify Student Record\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline from buffer

switch (choice) {
case 1: addStudent(); break;
case 2: displayAll(); break;
case 3: searchStudent(); break;
case 4: sortStudents(); break;
case 5: modifyStudent(); break;
case 6: printf("Exiting...\n"); break;
default: printf("Invalid choice.\n");
}
} while (choice != 6);

return 0;
}
void addStudent() {
if (count >= MAX) {
printf("Database full.\n");
return;
}

Student s;
[Link] = 0;

printf("Enter Roll Number: ");


scanf("%d", &[Link]);
getchar(); // clear newline

printf("Enter Name: ");


fgets([Link], sizeof([Link]), stdin);
[Link][strcspn([Link], "\n")] = 0;

printf("Enter Program: ");


fgets([Link], sizeof([Link]), stdin);
[Link][strcspn([Link], "\n")] = 0;

printf("Enter Course: ");


fgets([Link], sizeof([Link]), stdin);
[Link][strcspn([Link], "\n")] = 0;

printf("Enter marks for %d subjects:\n", SUBJECTS);


for (int i = 0; i < SUBJECTS; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &[Link][i]);
[Link] += [Link][i];
}

[Link] = [Link] / (float)SUBJECTS;


students[count++] = s;
printf("Student added successfully!\n");
}

void displayAll() {
if (count == 0) {
printf("No student records available.\n");
return;
}

printf("\n%-5s %-20s %-10s %-10s %-15s %-10s\n", "Roll", "Name", "Program", "Course", "Total Marks",
"Average");
for (int i = 0; i < count; i++) {
printf("%-5d %-20s %-10s %-10s %-15d %-10.2f\n",
students[i].rollNo, students[i].name, students[i].program,
students[i].course, students[i].total, students[i].average);
}
}

void searchStudent() {
int roll;
printf("Enter Roll Number to search: ");
scanf("%d", &roll);
for (int i = 0; i < count; i++) {
if (students[i].rollNo == roll) {
printf("Student found:\n");
printf("Name: %s\nProgram: %s\nCourse: %s\n",
students[i].name, students[i].program, students[i].course);
printf("Marks: ");
for (int j = 0; j < SUBJECTS; j++)
printf("%d ", students[i].marks[j]);
printf("\nTotal: %d\nAverage: %.2f\n", students[i].total, students[i].average);
return;
}
}
printf("Student with roll number %d not found.\n", roll);
}

void sortStudents() {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (students[j].total < students[j + 1].total) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("Students sorted by total marks (descending).\n");
displayAll();
}

void modifyStudent() {
int roll;
printf("Enter Roll Number to modify: ");
scanf("%d", &roll);

for (int i = 0; i < count; i++) {


if (students[i].rollNo == roll) {
printf("Enter new name: ");
getchar();
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = 0;

printf("Enter new program: ");


fgets(students[i].program, sizeof(students[i].program), stdin);
students[i].program[strcspn(students[i].program, "\n")] = 0;

printf("Enter new course: ");


fgets(students[i].course, sizeof(students[i].course), stdin);
students[i].course[strcspn(students[i].course, "\n")] = 0;

students[i].total = 0;
printf("Enter new marks for %d subjects:\n", SUBJECTS);
for (int j = 0; j < SUBJECTS; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
students[i].total += students[i].marks[j];
}
students[i].average = students[i].total / (float)SUBJECTS;
printf("Student record modified successfully.\n");
return;
}
}
printf("Student with roll number %d not found.\n", roll);
}

Output:

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 1
Enter Roll Number: 1
Enter Name: John
Enter Program: Engineering
Enter Course: E&TC
Enter marks for 5 subjects:
Subject 1: 20
Subject 2: 30
Subject 3: 40
Subject 4: 50
Subject 5: 60
Student added successfully!

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 1
Enter Roll Number: 2
Enter Name: Alica
Enter Program: Engineering
Enter Course: Computer
Enter marks for 5 subjects:
Subject 1: 40
Subject 2: 60
Subject 3: 50
Subject 4: 30
Subject 5: 80
Student added successfully!

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 1
Enter Roll Number: 3
Enter Name: David
Enter Program: Engineering
Enter Course: E&TC
Enter marks for 5 subjects:
Subject 1: 70
Subject 2: 0
Subject 3: 90
Subject 4: 60
Subject 5: 50
Student added successfully!

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice:
2

Roll Name Program Course Total Marks Average


1 John Engineering E&TC 200 40.00
2 Alica Engineering Computer 260 52.00
3 David Engineering E&TC 270 54.00

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 3
Enter Roll Number to search: 1
Student found:
Name: John
Program: Engineering
Course: E&TC
Marks: 20 30 40 50 60
Total: 200
Average: 40.00

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 4
Students sorted by total marks (descending).

Roll Name Program Course Total Marks Average


3 David Engineering E&TC 270 54.00
2 Alica Engineering Computer 260 52.00
1 John Engineering E&TC 200 40.00

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 5
Enter Roll Number to modify: 1
Enter new name: John
Enter new program: Engineering
Enter new course: E&TC
Enter new marks for 5 subjects:
Subject 1: 60
Subject 2: 90
Subject 3: 0
Subject 4: 0
Subject 5: : 90
Student record modified successfully.

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: Enter Roll Number to modify: Student with roll number 90 not found.

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice: 3
Enter Roll Number to search: 1
Student found:
Name: John
Program: Engineering
Course: E&TC
Marks: 60 90 0 0 60
Total: 210
Average: 42.00

=== Student Result Management System ===


1. Add Student
2. Display All Students
3. Search by Roll Number
4. Sort by Total Marks
5. Modify Student Record
6. Exit
Enter your choice:6
Exiting...

=== Code Execution Successful ===

You might also like