Algorithmic I Ch 4 : Iterative structure – 10) Exercise 3 2023/2024
Exercise 3 : (Course)
Write an algorithm that reads n real values between 0 and 20 (which represent the averages of
n students with 0<n≤100) and displays :
• The percentage of students with an average < 10
• The percentage of students with an average ≥ 10 and < 15
• The percentage of students with an average ≥ 15
The algorithm must take into account incorrect entries (values < 0 and > 20). In this case, the
user is asked to re-enter the value until it is correct.
Solution :
Algorithm Statistics;
Variable
nb1, nb2, nb3, i, n : integer; x : real;
Begin
/*(1) Controlled reading of the number of averages n between 1 and 100*/
Repeat
Print(“Give the number of averages n>0 and <=100“);
Read(n);
Until (n>0 and n<=100);
/*(2) Initialize the three counters to 0*/
nb1 0; nb20; nb30;
/*(3) Repeat the treatment for traitement for each of the n averages*/
For i 1 to n step 1 do
/*(3.1) Controlled reading of an average*/
Repeat
Print(“Give the average n° “,i ,“ between 0 and 20 : “); Read(x);
Until (x>=0 and x<=20);
/*(3.2) Updating the three counters*/
If(x<10) then nb1nb1+1;
Else If (x>=15) then nb3nb3+1;
Else /*The average is between 10 and 15*/
nb2nb2+1;
EndIf;
EndIf;
EndFor;
/*(4) Calculate and display the three percentages*/
Print(“The percentage of students with an average < 10 is : “, nb1*100/n, “%“);
Print(“The percentage of students with an average ≥ 10 and < 15 is : “, nb2*100/n,
“%“);
Print(“The percentage of students with an average ≥ 15 is : “, nb3*100/n, “%“);
End
Page 1 sur 1