Untitled
unknown
c_cpp
3 years ago
5.8 kB
7
Indexable
#include <stdio.h> #include <string.h> typedef struct Student{ char name[30]; char dep[20]; int yearos; float cgpa; }stu; void insertStudent(int n, stu *s); void sortStudent(int n, stu *s); void getNameByYear(int n, stu *s, int year); void getNameByDep(int n, stu *s, const char * dep); void getHighestCGPA(int n, stu *s); void getDetailByIndex(stu *s, int index); void getDetailByDep(int n, stu *s, const char *dep); void getDetailByName(int n, stu *s, const char *name); void getAll(int n, stu *s); void updateCGPA(int n, stu *s, const char *name); void insertStudent(int n, stu *s){ for(int i=0; i<n; i++){ printf("Enter the details of student %d\n", i+1); printf("Enter name\n"); scanf("%s", (s+i)->name); printf("Enter department\n"); scanf("%s", (s+i)->dep); printf("Enter year of study\n"); scanf("%d", &(s+i)->yearos); printf("Enter cgpa\n"); scanf("%f", &(s+i)->cgpa); } } void sortStudent(int n, stu *s){ for(int i=0; i<n-1; i++){ for(int j=i; j<n; j++){ if(strcmp((s+i)->name, (s+j)->name) > 0){ stu temp = *(s+i); *(s+i) = *(s+j); *(s+j) = temp; } } } } void getNameByYear(int n, stu *s, int year){ int count=0; //printf("Students from Year %d\n", year); for(int i=0; i<n; i++){ if((s+i)->yearos == year){ count++; if(count == 1) printf("Students from Year %d\n", year); printf("%s\n", (s+i)->name); } } if(!count) printf("No student found in %d year.\n", year); } void getNameByDep(int n, stu *s, const char * dep){ int count=0; //printf("Students from Department %s\n", dep); for(int i=0; i<n; i++){ if(strcmp((s+i)->dep, dep) == 0){ count++; if(count == 1) printf("Students from Department %s\n", dep); printf("%s\n", (s+i)->name); } } if(!count) printf("No student found in %s department.\n", dep); } void getHighestCGPA(int n, stu *s){ float highest = 0.0, index; for(int i=0; i<n; i++){ if((s+i)->cgpa > highest){ highest = (s+i)->cgpa; index = i; } } printf("Overall Topper Details\n"); getDetailByIndex(s, index); } void getDetailByIndex(stu *s, int index){ printf("Name : %s\n", (s+index)->name); printf("Department : %s\n", (s+index)->dep); printf("Year of study : %d\n", (s+index)->yearos); printf("CGPA : %.2f\n", (s+index)->cgpa); } void getDetailByDep(int n, stu *s, const char *dep){ float highest = 0.0, index=-1; for(int i=0; i<n; i++){ if(strcmp((s+i)->dep, dep) == 0){ if((s+i)->cgpa > highest){ highest = (s+i)->cgpa; index = i; } } } if(index == -1) printf("%s department not found.\n", dep); else { printf("%s Department Topper Details\n", dep); getDetailByIndex(s, index); } } void getDetailByName(int n, stu *s, const char *name){ int count=0; for(int i=0; i<n; i++){ if(strcmp((s+i)->name, name) == 0){ count++; getDetailByIndex(s, i); } } if(!count) printf("Student by name %s not found\n", name); } void getAll(int n, stu *s){ for(int i=0; i<n; i++){ getDetailByIndex(s, i); } } void updateCGPA(int n, stu *s, const char *name){ int found = 0; for(int i=0; i<n; i++){ if(strcmp((s+i)->name, name) == 0){ found = 1; (s+i)->cgpa += 0.5; } } if(!found) printf("%s not found\n", name); } int main(){ int n; printf("Enter the number of students\n"); scanf("%d", &n); stu s[n]; insertStudent(n, s); sortStudent(n, s); int exit = 0, choice; do{ printf("Menu\n"); printf("1)Display names of students from a particular year\n"); printf("2)Display names of students from a particular department\n"); printf("3)Display all details of the student having the highest CGPA\n"); printf("4)Display all details of the student from a particular department having the highest CGPA\n"); printf("5)Display all details of a particular student (Search by name)\n"); printf("6)Display all details of all students\n"); printf("7)Update CGPA of a particular student\n"); printf("8)Exit\n"); printf("Enter your choice\n"); scanf("%d", &choice); int year; char buf[10]; switch(choice){ case 1: printf("Enter the year\n"); scanf("%d", &year); getNameByYear(n, s, year); break; case 2: printf("Enter the department\n"); scanf("%s", buf); getNameByDep(n, s, buf); break; case 3: getHighestCGPA(n, s); break; case 4: printf("Enter the department\n"); scanf("%s", buf); getDetailByDep(n, s, buf); break; case 5: printf("Enter the name\n"); scanf("%s", buf); getDetailByName(n, s, buf); break; case 6: getAll(n, s); break; case 7: printf("Enter the name\n"); scanf("%s", buf); updateCGPA(n, s, buf); break; case 8: exit = 1; break; default: break; } } while(!exit); return 0; }
Editor is loading...