Creating a C Program to Calculate the Average Marks of a Class

Creating a C Program to Calculate the Average Marks of a Class

Developing a C program to calculate the average marks of a class of 15 students, where marks range from 1 to 100, involves several key steps. This guide will walk you through the process, including how to declare variables, handle input, perform calculations, and output the results.

Key Steps

The following steps outline the essential elements of this program:

Declare Variables: Variables to store the marks of each student, the total marks, and the average. Input Marks: Use a loop to input marks for each student and validate the input. Calculate Total and Average: Sum the marks and then calculate the average. Print the Result: Output the average.

Sample C Program Code

Here is a sample C program that accomplishes this:

#include stdio.h
int main() {
    int marks[15];     // Array to hold the marks of 15 students
    int total  0;     // Variable to hold the total marks
    float average;      // Variable to hold the average
    // Input marks for 15 students
    for (int i  0; i  15; i  ) {
        printf(Enter the marks for student %d: , i   1);
        scanf(%d, marks[i]);
        // Validate input
        while (marks[i]  1 || marks[i]  100) {
            printf(Marks must be between 1 and 100. Please re-enter: );
            scanf(%d, marks[i]);
        }
        total   marks[i];
    }
    // Calculate average
    average  total / 15.0;  // Using 15.0 to ensure floating-point division
    // Print the average
    printf(The average marks of the class are: %.2f
, average);
    return 0;
}

Explanation of the Code

Array Declaration:

int marks[15] Declares an array to store the marks of 15 students.

Input Loop:

for (int i 0; i 15; i ) { printf(Enter the marks for student %d: , i 1); scanf(%d, marks[i]); while (marks[i] 1 || marks[i] 100) { printf(Marks must be between 1 and 100. Please re-enter: ); scanf(%d, marks[i]); } total marks[i]; }

This loop iterates 15 times to take input for each student. It checks if the entered marks are within the valid range (1 to 100) and ensures that the user inputs valid marks. Total Calculation:

total marks[i] Each mark is added to the total variable to accumulate the total marks.

Average Calculation:

average total / 15.0 The average is calculated by dividing the total marks by 15.0 to ensure that the result is a floating-point number.

Output:

printf(The average marks of the class are: %.2f , average); Finally, the average is printed with two decimal places using .2f.

Running the Program

You can compile and run this program using any standard C compiler. When you run it, it will prompt you to enter the marks for each student and then it will display the average marks.

Next Steps

I would start by deciding what my input will look like. Possibly a name followed by marks as numbers. I'd decide if I'm going to use a delimiter. Vertical bar is my favorite.

Next, I'd decide what my output looks like. In this case, it's probably no more than a name followed by an average, unless I want to show the individual marks too.

Once I have those, it's not too hard to parse the strings, sum the marks, and compute the average.

C doesn't help much with parsing strings, so I might also make some helper functions for that. This could involve using standard library functions or implementing custom logic to handle different input formats.