Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_STUDENTS 100
- typedef struct
- {
- char id[4];
- char name[50];
- int assignment;
- int midExam;
- int finalExam;
- } Student;
- Student students[MAX_STUDENTS];
- int studentCount = 0;
- char *getGrade(float score)
- {
- if (score >= 85)
- {
- return "A";
- }
- else if (score >= 80)
- {
- return "A-";
- }
- else if (score >= 75)
- {
- return "B+";
- }
- else if (score >= 70)
- {
- return "B";
- }
- else if (score >= 65)
- {
- return "B-";
- }
- else if (score >= 60)
- {
- return "C+";
- }
- else if (score >= 55)
- {
- return "C";
- }
- else if (score >= 45)
- {
- return "D";
- }
- else
- {
- return "E";
- }
- }
- float getFinalScore(int assignment, int midExam, int finalExam)
- {
- return (0.3 * assignment) + (0.3 * midExam) + (0.4 * finalExam);
- }
- void addStudent()
- {
- printf("> Add student\n");
- if (studentCount >= MAX_STUDENTS)
- {
- printf("Maximum students reached\n");
- return;
- }
- printf("ID: ");
- scanf("%s", students[studentCount].id);
- printf("Name: ");
- scanf(" %[^\n]s", students[studentCount].name);
- printf("Assignment: ");
- scanf("%d", &students[studentCount].assignment);
- printf("Mid Exam: ");
- scanf("%d", &students[studentCount].midExam);
- printf("Final Exam: ");
- scanf("%d", &students[studentCount].finalExam);
- studentCount++;
- }
- void displayStudentDetails()
- {
- printf("> Student details\n");
- printf("|%-5s|%-20s|%-10s|%-10s|%-10s|%-15s|%-10s|\n", "ID", "Name", "Assignment", "Mid Exam", "Final Exam", "Final Score", "Final Grade");
- for (int i = 0; i < studentCount; i++)
- {
- float finalScore = getFinalScore(students[i].assignment, students[i].midExam, students[i].finalExam);
- char *finalGrade = getGrade(finalScore);
- 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);
- }
- }
- void displayGradeAverage()
- {
- printf("> Grade average \n");
- float assignmentTotal = 0;
- float midExamTotal = 0;
- float finalExamTotal = 0;
- for (int i = 0; i < studentCount; i++)
- {
- assignmentTotal += students[i].assignment;
- midExamTotal += students[i].midExam;
- finalExamTotal += students[i].finalExam;
- }
- float assignmentAverage = assignmentTotal / studentCount;
- float midExamAverage = midExamTotal / studentCount;
- float finalExamAverage = finalExamTotal / studentCount;
- float finalScoreAverage = (0.3 * assignmentAverage) + (0.3 * midExamAverage) + (0.4 * finalExamAverage);
- printf("Assignment : %.2f\n", assignmentAverage);
- printf("Mid Exam : %.2f\n", midExamAverage);
- printf("Final Exam : %.2f\n", finalExamAverage);
- printf("Final Score: %.2f\n", finalScoreAverage);
- }
- int main()
- {
- int choice;
- while (1)
- {
- printf("> Menu\n");
- printf("1. Add student\n");
- printf("2. Display student details\n");
- printf("3. Display grade average\n");
- printf("\n");
- printf("0. Exit\n");
- printf("\n");
- printf("Menu: ");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- addStudent();
- break;
- case 2:
- displayStudentDetails();
- break;
- case 3:
- displayGradeAverage();
- break;
- case 0:
- exit(0);
- default:
- printf("Invalid choice\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement