COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Repetitions/Loops WhileLoop.java LoopWithCounter.java LoopWithSentinel.java LoopWithFlag.java SimpleInterest.java Factorial.java Fibnonacci.java PrimeNumber.java GCD.java PopulationGrowth.java ForLoop.java DoWhileLoop.java FileIO_Tickets.java FileIO_Salary.java NestedLoop.java



LoopWithCounter Program


Description

There are 10 students, each with a test score.

This program inputs 10 test scores, calculates the average score, and finds the highest score
(assume there is no tie).

Because we know in advance that there are 10 students, so the counter-controlled while loop is used.

Associated Concepts:

Counter-controlled while loop

Algorithm:

Initialize sum variable to zero

Initialize highest variable to a very small number

Use a counter-controlled while loop to input 10 test scores

    If the input score is valid

        Add the score to sum

        Assign the score to highest if the score is larger than the current highest value

    Otherwise

         Print an error message

Once the loop is ended, calculate the average

Print the result

Source Code



Sample Run



Video