Advertisement
Adit_Firdaus

41-45

Feb 12th, 2024
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_STUDENTS 100
  6.  
  7. typedef struct
  8. {
  9.     char id[4];
  10.     char name[50];
  11.     int assignment;
  12.     int midExam;
  13.     int finalExam;
  14. } Student;
  15.  
  16. Student students[MAX_STUDENTS];
  17.  
  18. int studentCount = 0;
  19.  
  20. char *getGrade(float score)
  21. {
  22.     if (score >= 85)
  23.     {
  24.         return "A";
  25.     }
  26.     else if (score >= 80)
  27.     {
  28.         return "A-";
  29.     }
  30.     else if (score >= 75)
  31.     {
  32.         return "B+";
  33.     }
  34.     else if (score >= 70)
  35.     {
  36.         return "B";
  37.     }
  38.     else if (score >= 65)
  39.     {
  40.         return "B-";
  41.     }
  42.     else if (score >= 60)
  43.     {
  44.         return "C+";
  45.     }
  46.     else if (score >= 55)
  47.     {
  48.         return "C";
  49.     }
  50.     else if (score >= 45)
  51.     {
  52.         return "D";
  53.     }
  54.     else
  55.     {
  56.         return "E";
  57.     }
  58. }
  59.  
  60. float getFinalScore(int assignment, int midExam, int finalExam)
  61. {
  62.     return (0.3 * assignment) + (0.3 * midExam) + (0.4 * finalExam);
  63. }
  64.  
  65. void addStudent()
  66. {
  67.     printf("> Add student\n");
  68.  
  69.     if (studentCount >= MAX_STUDENTS)
  70.     {
  71.         printf("Maximum students reached\n");
  72.         return;
  73.     }
  74.  
  75.     printf("ID: ");
  76.     scanf("%s", students[studentCount].id);
  77.     printf("Name: ");
  78.     scanf(" %[^\n]s", students[studentCount].name);
  79.     printf("Assignment: ");
  80.     scanf("%d", &students[studentCount].assignment);
  81.     printf("Mid Exam: ");
  82.     scanf("%d", &students[studentCount].midExam);
  83.     printf("Final Exam: ");
  84.     scanf("%d", &students[studentCount].finalExam);
  85.  
  86.     studentCount++;
  87. }
  88.  
  89. void displayStudentDetails()
  90. {
  91.     printf("> Student details\n");
  92.  
  93.     printf("|%-5s|%-20s|%-10s|%-10s|%-10s|%-15s|%-10s|\n", "ID", "Name", "Assignment", "Mid Exam", "Final Exam", "Final Score", "Final Grade");
  94.  
  95.     for (int i = 0; i < studentCount; i++)
  96.     {
  97.         float finalScore = getFinalScore(students[i].assignment, students[i].midExam, students[i].finalExam);
  98.         char *finalGrade = getGrade(finalScore);
  99.  
  100.         printf("|%-5s|%-20s|%-10d|%-10d|%-10d|%-15.2f|%-10s|\n", students[i].id, students[i].name, students[i].assignment, students[i].midExam, students[i].finalExam, finalScore, finalGrade);
  101.     }
  102. }
  103.  
  104. void displayGradeAverage()
  105. {
  106.     printf("> Grade average \n");
  107.  
  108.     float assignmentTotal = 0;
  109.     float midExamTotal = 0;
  110.     float finalExamTotal = 0;
  111.  
  112.     for (int i = 0; i < studentCount; i++)
  113.     {
  114.         assignmentTotal += students[i].assignment;
  115.         midExamTotal += students[i].midExam;
  116.         finalExamTotal += students[i].finalExam;
  117.     }
  118.  
  119.     float assignmentAverage = assignmentTotal / studentCount;
  120.     float midExamAverage = midExamTotal / studentCount;
  121.     float finalExamAverage = finalExamTotal / studentCount;
  122.     float finalScoreAverage = (0.3 * assignmentAverage) + (0.3 * midExamAverage) + (0.4 * finalExamAverage);
  123.  
  124.     printf("Assignment : %.2f\n", assignmentAverage);
  125.     printf("Mid Exam : %.2f\n", midExamAverage);
  126.     printf("Final Exam : %.2f\n", finalExamAverage);
  127.     printf("Final Score: %.2f\n", finalScoreAverage);
  128. }
  129.  
  130. int main()
  131. {
  132.     int choice;
  133.  
  134.     while (1)
  135.     {
  136.         printf("> Menu\n");
  137.         printf("1. Add student\n");
  138.         printf("2. Display student details\n");
  139.         printf("3. Display grade average\n");
  140.         printf("\n");
  141.         printf("0. Exit\n");
  142.         printf("\n");
  143.         printf("Menu: ");
  144.         scanf("%d", &choice);
  145.  
  146.         switch (choice)
  147.         {
  148.             case 1:
  149.                 addStudent();
  150.                 break;
  151.             case 2:
  152.                 displayStudentDetails();
  153.                 break;
  154.             case 3:
  155.                 displayGradeAverage();
  156.                 break;
  157.             case 0:
  158.                 exit(0);
  159.             default:
  160.                 printf("Invalid choice\n");
  161.         }
  162.     }
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement