Use the following structure definition to answer question
struct subject {
char subjectID[10];
float marks;
};
struct student {
char name[20];
char ID[10];
struct subject *enrolled;
};
Write a C program to fulfil the following Menu requirements
a. Add Student - Your program should allow creation of any number of students. Each student must enrol in at least one subject and maximum of 4 subjects.
b. Display Students – Display the details of all the students along with their subject details
c. Rank Students – Display students based on the Average marks. Student with the highest average marks to be displayed first
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_SUBJECTS 4 #define MAX_MARKS 100 typedef struct subject { char subjectID[10]; float marks; }subject_db_t; typedef struct student { char name[20]; char ID[10]; struct subject *enrolled; }student_db_t; void allocate_student_memory(student_db_t *stud, int index, int *stud_sub) { int sub_index = 0; char tmp_subj_name[20] = {0}; float tmp_marks = 0; // Allocate memory for each student's subject stud[index].enrolled = (subject_db_t *)malloc(stud_sub[index] * sizeof(subject_db_t)); for (sub_index = 0; sub_index < stud_sub[index]; sub_index++) { printf("Student[%s] Subject[%d] Name\t", stud[index].ID, sub_index + 1); scanf("%s", tmp_subj_name); strncpy(stud[index].enrolled[sub_index].subjectID, tmp_subj_name, strlen(tmp_subj_name)); printf("\n"); printf("Student[%s] Subject[%d] Mark \t", stud[index].ID, sub_index + 1); scanf("%f", &tmp_marks); stud[index].enrolled[sub_index].marks = tmp_marks; printf("\n"); } } void Free_student(student_db_t *stud, int no_stud) { int index = 0; for (index = 0; index < no_stud; index++) free(stud[index].enrolled); } void Display_sorted_student(student_db_t *stud, int no_stud, int *average_mark, int *stud_sub) { int in = 0, index = 0, sub_index = 0; printf("Index - Stud ID - Name - Sub1 - Mark - Sub2 - Mark - Sub3 - Mark -" " Sub4 - Mark - Avg_Mark\n"); printf("-------------------------------------------------------------------------" "-----------------------------------\n"); for (index = 0; index < no_stud; index++) { printf("%5d %7s ", index + 1, stud[index].ID); printf("%8s ", stud[index].name); for (sub_index = 0; sub_index < stud_sub[index]; sub_index++) { printf("%8.4s ", stud[index].enrolled[sub_index].subjectID); printf("%6.2f ", stud[index].enrolled[sub_index].marks); } // Not enrolled subject details will be displayed as "-" if (stud_sub[index] < MAX_SUBJECTS) { for (in = stud_sub[index]; in < MAX_SUBJECTS; in++) { printf(" - - "); } } printf(" %d\n", average_mark[index]); } printf("\n"); } void Display_student(student_db_t *stud, int no_stud, int *average_mark, int *stud_sub) { int in = 0, index = 0, sub_index = 0; int total_marks = 0; printf("Index - Stud ID - Name - Sub1 - Mark - Sub2 - Mark - Sub3 - Mark -" " Sub4 - Mark - Avg_Mark\n"); printf("-------------------------------------------------------------------------" "-----------------------------------\n"); for (index = 0; index < no_stud; index++) { printf("%5d %7s ", index + 1, stud[index].ID); printf("%8s ", stud[index].name); total_marks = 0; for (sub_index = 0; sub_index < stud_sub[index]; sub_index++) { printf("%8.4s ", stud[index].enrolled[sub_index].subjectID); printf("%6.2f ", stud[index].enrolled[sub_index].marks); total_marks += stud[index].enrolled[sub_index].marks; } // Not enrolled subject details will be displayed as "-" if (stud_sub[index] < MAX_SUBJECTS) { for (in = stud_sub[index]; in < MAX_SUBJECTS; in++) { printf(" - - "); } } average_mark[index] = total_marks / stud_sub[index]; printf(" %d\n", average_mark[index]); } printf("\n"); } void swap(int *left, int * right) { int tmp = *right; *right = *left; *left = tmp; return; } void swap_student(student_db_t *left_stud, student_db_t *right_stud) { student_db_t tmp_stud = *right_stud; *right_stud = *left_stud; *left_stud = tmp_stud; return; } int partition(int *avg_mark, int *stud_sub, student_db_t *stud, int low, int high) { int pivot = avg_mark[high]; int index = 0; int start = low - 1; for (index = low; index < high; index++) { // Keep all elements greater than pivot at its left if (avg_mark[index] >= pivot) { start++; swap(&avg_mark[start], &avg_mark[index]); swap(&stud_sub[start], &stud_sub[index]); swap_student(&stud[start], &stud[index]); } } // Place pivot at its proper place swap(&avg_mark[start + 1], &avg_mark[high]); swap(&stud_sub[start + 1], &stud_sub[high]); swap_student(&stud[start + 1], &stud[high]); return (start + 1); } void sort_student_on_rank(int *avg_mark, int *stud_sub, student_db_t *stud, int low, int high) { int index = 0; if (low < high) { index = partition(avg_mark, stud_sub, stud, low, high); // After partition sort the pivot left and right elements sort_student_on_rank(avg_mark, stud_sub, stud, low, index -1); sort_student_on_rank(avg_mark, stud_sub, stud, index + 1, high); } return; } void Rank_student(int *avg_mark, int *stud_sub, student_db_t *stud, int low, int high) { int index; sort_student_on_rank(avg_mark, stud_sub, stud, low, high); return; } int main() { int no_stud = 0; int in = 0, index = 0, sub_index = 0; char tmp_subj_name[20] = {0}; float tmp_marks = 0; int total_marks = 0; printf("Enter number of students\t"); scanf("%d", &no_stud); student_db_t stud[no_stud]; int stud_sub[no_stud]; int average_mark[no_stud]; // Add Student for (; index < no_stud; index++) { printf("Enter student %d Name\t", index + 1); scanf("%s", stud[index].name); printf("\n"); printf("Enter student %d Roll Number\t", index + 1); scanf("%s", stud[index].ID); printf("\n"); printf("Enter the number of subjects\t"); scanf("%d", &stud_sub[index]); if (stud_sub[index] > MAX_SUBJECTS) { printf("You've entered more than maximum. Subjects can be enrolled" "upto maximum %d\n", MAX_SUBJECTS); printf("Re-enter number of subjects\t"); scanf("%d", &stud_sub[index]); } // Allocate Memory based on the subjects enrolled for allocate_student_memory(stud, index, stud_sub); } // Display Students with their subject details Display_student(stud, no_stud, average_mark, stud_sub); // Rank Students Rank_student(average_mark, stud_sub, stud, 0, no_stud - 1); // Display Students based on rank Display_sorted_student(stud, no_stud, average_mark, stud_sub); // Free the Students Free_student(stud, no_stud); return 0; }
Output:
Enter number of students 4
Enter student 1 Name Shirley
Enter student 1 Roll Number 07IT073
Enter the number of subjects 3
Student[07IT073] Subject[1] Name Tamil
Student[07IT073] Subject[1] Mark 45
Student[07IT073] Subject[2] Name English
Student[07IT073] Subject[2] Mark 67
Student[07IT073] Subject[3] Name Mathematics
Student[07IT073] Subject[3] Mark 55
Enter student 2 Name Vicky
Enter student 2 Roll Number 06IT089
Enter the number of subjects 4
Student[06IT089] Subject[1] Name Hindhi
Student[06IT089] Subject[1] Mark 79
Student[06IT089] Subject[2] Name English
Student[06IT089] Subject[2] Mark 85
Student[06IT089] Subject[3] Name ComputerScience
Student[06IT089] Subject[3] Mark 90
Student[06IT089] Subject[4] Name Physics
Student[06IT089] Subject[4] Mark 77
Enter student 3 Name Sivy
Enter student 3 Roll Number 06IT090
Enter the number of subjects 2
Student[06IT090] Subject[1] Name Chemistry
Student[06IT090] Subject[1] Mark 80
Student[06IT090] Subject[2] Name English
Student[06IT090] Subject[2] Mark 57
Enter student 4 Name Sweety
Enter student 4 Roll Number 07IT090
Enter the number of subjects 1
Student[07IT090] Subject[1] Name Chemistry
Student[07IT090] Subject[1] Mark 65
Index - Stud ID - Name - Sub1 - Mark - Sub2 - Mark - Sub3 - Mark - Sub4 - Mark - Avg_Mark
------------------------------
Index - Stud ID - Name - Sub1 - Mark - Sub2 - Mark - Sub3 - Mark - Sub4 - Mark - Avg_Mark
------------------------------
|
Comments
Post a Comment