Iteration

Case Study


PROBLEM:

Write a program to calculate the average marks for a class of students. The program will initially prompt for the number of students to be entered and then for each student will ask for their marks for each of the 4 exams the student sat. The program will then calculate the average mark for each student and the average mark for the whole class.


SOLUTION:

The solution to this will take the form:-

where the program repeats for each student the code to enter the student's marks and calculate their average.

This could be implemented using a while loop to produce


/*Using While Loop */

#include <stdio.h>

main()
{

        int     num_students, count;
        float   mark1, mark2, mark3, mark4;
        float   student_average, class_total = 0, class_average;

        /* Enter no. of students */
        printf("Please enter the number of students in the class: ");
        scanf("%d",&num_students);

        // set count to zero
        count = 0;

        while (count < num_students)
        {
                /* enter 4 marks for each student */
                printf("Please enter the marks for student %d\n", count); 
                scanf("%f %f %f %f", &mark1, &mark2, &mark3, &mark4);

                /* Calculate average */
                student_average = (mark1 + mark2 + mark3 + mark4) / 4;

                /* Add average to total */
                class_total += student_average;

                /* Display average */
                printf("The average mark for student %d is %f \n ", count,
                     student_average);

                /* Increment count for next student */
                count ++;
        }


        /* Calculate class average */
        class_average = class_total / num_students;

        /* Output Class average */
        printf("The class average is %f \n ", class_average );
}

When this program is run it produces the following output ( with sample figure inserted):-


Please enter the number of students in the class: 4
Please enter the marks for student 0
23 45 63 54
The average mark for student 0 is 46.25

Please enter the marks for student 1
67 87 67 87
The average mark for student 1 is 77

Please enter the marks for student 2
54 65 34 76
The average mark for student 2 is 57.25

Please enter the marks for student 3
34 35 36 25
The average mark for student 3 is 32.5

The class average is 53.25

The program can be drawn schematically to show how it fits in to the flow structure given above. The code used for each part is shown alongside.

The same program could be converted to a for and only the initialisation, loop statement and increment need to be changed. Here is the same program but written with the for loop.


/*Using For Loop */

#include <stdio.h>

main()
{

        int     num_students, count;
        float   mark1, mark2, mark3, mark4;
        float   student_average, class_total = 0, class_average;

        /* Enter no. of students */
        printf("Please enter the number of students in the class: ");
        scanf("%d",&num_students);

	/* for statement includes initialisation, test and increment */
        for (count=0; count < num_students ; count++)
        {
                /* enter 4 marks for each student */
                printf("Please enter the marks for student %d\n", count); 
                scanf("%f %f %f %f", &mark1, &mark2, &mark3, &mark4);

                /* Calculate average */
                student_average = (mark1 + mark2 + mark3 + mark4) / 4;

                /* Add average to total */
                class_total += student_average;

                /* Display average */
                printf("The average mark for student %d is %f \n ", count,
                     student_average);

        }


        /* Calculate class average */

        /* Calculate class average */
        class_average = class_total / num_students;

        /* Output Class average */
        printf("The class average is %f \n ", class_average );
}


Back to Main Page for Lecture 4