Page 170 - Computer
P. 170

Computer Science  Example of loop iteration     Is Kibubu ≤6000?      No
                                                            Start
          control structure
          Consider that you have a “Kibubu”
          and that you wish to save money
        FOR ONLINE READING ONLY
          on it. Write an algorithm that asks
                                                          Yes
                                                                               Kibubu
          the user to input coins in Tanzania
                                                          of coins
          shillings  (Tshs)  repeatedly and             Input amount         reached 6000
          then addsthe sum to the “Kibubu”         Kibubu =Initial amount + Input amount
          until it exceeds or equals 6000                                      End
          shillings. Construct this algorithm   Figure 8.16: Loop to display the amount in Kibubu
          and   draw    a   corresponding
          flowchart.


          Algorithm:                                Exercise 8.6
           (i)  Start a loop with the condition   1.  What is pseudocode?
               that the Kibubu contains less
               than 6000.                      2.  Can you tell someone that presenting a
           (ii)  Within the loop:                 problem in pseudocode is useful? Explain.
               (a)  Prompt the user to input   3.  Write pseudocode to calculate the sum
                   the amount of coins (in        and average of numbers.
                   Tshs).                      4.  Write a pseudocode for converting
               (b)  Add the input amount to       temperature from  Fahrenheit  into
                   Kibubu.                        Celsius. The inputs to the algorithm
               (c)  Check if Kibubu is still      are temperature in Fahrenheit, and the
                   less than or equal to 6000.    expected output is temperature in Celsius.
               (d)  If the condition is false,   5.  Write an algorithm to find the largest of
                   exit the loop.                 three numbers: a, b, and c.
           (iii) End the loop.

           (iv)  Display a message to the
               user that their Kibubu now
               contains 6000 shillings or
               more.
           (v)  End.


          The  flowchart  to  implement  the
          loop is presented as follows in
          Figure 8.16:


                                               162
                                                                  for Secondary Schools
   165   166   167   168   169   170   171   172   173   174   175