Dealing with errors and more work with decisions and loops - Exercise 2

Assume you have the following data stored somewhere:

Fred,Joan,Brian,Bert,Selie,Sue,Jack,Ng,Jacques,CLASS,Chris,Cheryl,Pam,Allan,CLASS,END

and it represents students in different classes. What might be the sentinel values in the data?

Design a program which:

    1. reads the data and displays the names of the students in the class
    2. counts the number of students in each class
    3. counts the number of classes

This is quite a difficult exercise.  In my sample answer I had to use nested loops, multiway selection and a compound statement. This is because the data we are dealing with contains values with quite different meanings and we have to look out for those meanings. Also with the data we are doing a few different things - displaying names, counting students and counting classes. In the data we have lists of students within a list of classes. The classes are separated by the boundary value CLASS, the students are separated by the boundary value comma. The complete set of data terminates with the boundary value END.

We read a piece of data and have to check it to determine what kind of meaning it has.

There aren't many variables:

There aren't many processes:

There are a few decisions to make:

Since we are counting two things there is the implication that there is two loops:

My first attempt at an algorithm is this:

As you can see, in order to clarify it all for me and you, I had to state it in step-form but now I can refine it. From the step form you can see that it has the shape of a repeat loop, the condition that terminates the whole thing is the last step - remember a repeat loop has the decision at the end.

The first refined sample answer uses a repeat loop nested within a repeat loop.

The second refined sample answer uses a while loop and the while loop is primed. This means that we read before the loop to set the while loop up. If the first read (INPUT student_name) isn't END then we have work to do. The INPUT within the loop is also moved to the end of the loop:

Students_In_Class = 0
Number_Of_Classes = 0
INPUT student_name
IF student_name <> 'END'
 THEN DOWHILE Student_Name <> 'END'
              DOWHILE Student_Name <> 'CLASS'
                IF Student_Name <> ','
                 THEN IF Student_Name <> 'CLASS'
                             THEN IF Student_Name <> 'END'
                                          THEN  DISPLAY Student_Name
                                                      Students_In_Class = Students_In_Class + 1
                ENDIF
                INPUT Student_Name
              END DOWHILE
              Number_Of_Classes = Number_Of_Classes + 1
              DISPLAY Students_In_Class
             Students_In_Class = 0
            END DOWHILE
            DISPLAY Number_Of_Classes
 

Return to the lesson


This publication is copyright David Beech and Learning Systems 1997-2002
and may not be reproduced by any means without the written permission of
David Beech.
9 Wyndella Street, Tasmania, Australia


db@codelearn.com