Complementary task for topic: 8

M Nemeth · 2023-08-29 15:21:04.629218'

Dynamic arrays: 2D dynamic array

Dynamic arrays: 2D dynamic array

Simple Grade Calculator:
Create a C program to calculate the final grades of students using dynamic arrays for storing their scores.
Requirements:
Ask the user to input the number of students and the number of subjects.
Allocate memory for a 2D array to store the scores of each student in each subject.
Ask the user to input the scores for each student in each subject.
Calculate the average score for each student and their final grades based on the following grading scale:
90 - 100: A
80 - 89: B
70 - 79: C
60 - 69: D
Below 60: F
Print the final grades of each student.

Enter the number of students: 3
Enter the number of subjects: 4
Enter scores for student 1:
Subject 1: 85
Subject 2: 75
Subject 3: 90
Subject 4: 78
Enter scores for student 2:
Subject 1: 92
Subject 2: 88
Subject 3: 70
Subject 4: 65
Enter scores for student 3:
Subject 1: 78
Subject 2: 80
Subject 3: 85
Subject 4: 93
Final Grades:
Student 1: A
Student 2: B
Student 3: B
Example outfit:
Enter the number of students: 3
Enter the number of subjects: 4
Enter scores for student 1:
Subject 1: 85
Subject 2: 75
Subject 3: 90
Subject 4: 78
Enter scores for student 2:
Subject 1: 92
Subject 2: 88
Subject 3: 70
Subject 4: 65
Enter scores for student 3:
Subject 1: 78
Subject 2: 80
Subject 3: 85
Subject 4: 93
Final Grades:
Student 1: A
Student 2: B
Student 3: B
Example outfit:
Enter the number of students: 3
Enter the number of subjects: 4
Enter scores for student 1:
Subject 1: 85
Subject 2: 75
Subject 3: 90
Subject 4: 78
Enter scores for student 2:
Subject 1: 92
Subject 2: 88
Subject 3: 70
Subject 4: 65
Enter scores for student 3:
Subject 1: 78
Subject 2: 80
Subject 3: 85
Subject 4: 93
Final Grades:
Student 1: A
Student 2: B
Student 3: B

Hint:

Solution
#include 
#include 

char calculateGrade(int averageScore) {
    if (averageScore >= 90 && averageScore <= 100)
        return 'A';
    else if (averageScore >= 80 && averageScore <= 89)
        return 'B';
    else if (averageScore >= 70 && averageScore <= 79)
        return 'C';
    else if (averageScore >= 60 && averageScore <= 69)
        return 'D';
    else
        return 'F';
}

int main() {
    int numStudents, numSubjects;
    printf("Enter the number of students: ");
    scanf("%d", &numStudents);

    printf("Enter the number of subjects: ");
    scanf("%d", &numSubjects);

    int** scores = (int**)malloc(numStudents * sizeof(int*));
    for (int i = 0; i < numStudents; i++) {
        scores[i] = (int*)malloc(numSubjects * sizeof(int));
    }

    for (int i = 0; i < numStudents; i++) {
        printf("\nEnter scores for student %d:\n", i + 1);
        for (int j = 0; j < numSubjects; j++) {
            printf("Subject %d: ", j + 1);
            scanf("%d", &scores[i][j]);
        }
    }

    printf("\nFinal Grades:\n");
    for (int i = 0; i < numStudents; i++) {
        int sum = 0;
        for (int j = 0; j < numSubjects; j++) {
            sum += scores[i][j];
        }
        int average = sum / numSubjects;
        char grade = calculateGrade(average);
        printf("Student %d: %c\n", i + 1, grade);
    }

    for (int i = 0; i < numStudents; i++) {
        free(scores[i]);
    }
    free(scores);

    return 0;
}



Explanation
    In this task, we ask the user to input the number of students and the number of subjects, and allocate memory for a 2D array called scores to store the scores of each student in each subject.

    We use nested for loops to ask the user to input the scores for each student in each subject and store them in the dynamic array.

    We calculate the average score for each student by summing up their scores in each subject and dividing by the number of subjects.

    We then call the calculateGrade function to determine the final grade for each student based on their average score.

    Finally, we print the final grades of each student using printf and free the dynamically allocated memory for the 2D array using free at the end of the program to avoid memory leaks.
< < previous    next > >