Complementary task for topic: 8

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

Dynamic arrays: Struct dynamic arrays (Database)

Dynamic arrays: Struct dynamic arrays (Database)

Create a C program that allows the user to input the details of students and their scores and calculates the average score using dynamic arrays and structs.

like:
Enter the number of students: 3
Enter the details of student 1:
Name: John Doe
Scores: 85 92 78 89
Enter the details of student 2:
Name: Jane Smith
Scores: 78 82 90 88
Enter the details of student 3:
Name: Mike Johnson
Scores: 92 87 75 80
Student Records:
------------------
Name: John Doe
Scores: 85 92 78 89
Average Score: 86.00
Name: Jane Smith
Scores: 78 82 90 88
Average Score: 84.50
Name: Mike Johnson
Scores: 92 87 75 80
Average Score: 83.50

Hint: Define a struct named Student to hold the details of each student, including their name and scores.
Ask the user to input the number of students.
Allocate memory for dynamic arrays of Student to store the details of the students and their scores.
Ask the user to input the name and scores of each student.
Calculate the average score for each student and store it in the Student struct.
Print the details of each student, including their name, scores, and average score.

Solution
#include 
#include 

typedef struct {
    char name[50];
    int numScores;
    int* scores;
    float averageScore;
} Student;

int main() {
    int numStudents;

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

    Student* students = (Student*)malloc(numStudents * sizeof(Student));

    for (int i = 0; i < numStudents; i++) {
        printf("\nEnter the details of student %d:\n", i + 1);

        printf("Name: ");
        scanf(" %[^\n]", students[i].name);

        printf("Number of Scores: ");
        scanf("%d", &students[i].numScores);

        students[i].scores = (int*)malloc(students[i].numScores * sizeof(int));

        printf("Scores: ");
        for (int j = 0; j < students[i].numScores; j++) {
            scanf("%d", &students[i].scores[j]);
        }

        float totalScore = 0;
        for (int j = 0; j < students[i].numScores; j++) {
            totalScore += students[i].scores[j];
        }
        students[i].averageScore = totalScore / students[i].numScores;
    }

    printf("\nStudent Records:\n");
    printf("------------------\n");

    for (int i = 0; i < numStudents; i++) {
        printf("Name: %s\n", students[i].name);
        printf("Scores: ");
        for (int j = 0; j < students[i].numScores; j++) {
            printf("%d ", students[i].scores[j]);
        }
        printf("\nAverage Score: %.2f\n", students[i].averageScore);
        printf("\n");
    }

    // Free memory
    for (int i = 0; i < numStudents; i++) {
        free(students[i].scores);
    }
    free(students);

    return 0;
}



Explanation
    In this task, we define a struct named Student to hold the details of each student, including their name, the number of scores, and an array of scores.

    We ask the user to input the number of students and allocate memory for a dynamic array of Student structs based on the number of students.

    For each student, we ask the user to input their name and the number of scores, and allocate memory for a dynamic array to store the scores.

    We calculate the average score for each student and store it in the averageScore field of the Student struct.

    Finally, we print the details of each student, including their name, scores, and average score. After printing, we free the dynamically allocated memory for each student's scores and the array of Student structs to avoid memory leaks.
< < previous    next > >