Itteration

A class of 30 students have taken four tests each. The teacher wants to decide on which students need to take a further test based on their results. A student will retake the test if their average score is less than 50%. The results should display all of the students who need to retake.

Deciding on what needs repeating requires you to think through the problem. Consider the input. If it is MORE than one value then it makes sense that we will need to repeat the main part of the algorithm. In this case we have 30 students which need to know if they are doing a retake. So we would need to repeat the following for EACH student.

  1. Calculate the average score
  2. Test to see if it is less than 50
  3. Display students name if it is less than 50

When creating a loop we need to decide on

  1. The start position
  2. The end position
  3. The increment

The start position for this is the first student. As we are using an array this will be 0. The end is the last student (30) and finally the increment will be 1 as we must visit every student. This leads to the following for loop -

FOR count = 0 TO 29
            Calculate the average score
            Test to see if it is less than 50
            Display students name if it is less than 50
NEXT

Do not forget that you MUST have a counter when dealing with a for loop. Here we have created a variable called “count” but it could be called anything. Based on the last section we can fill in some of the blanks here -

FOR count =0 TO 29
            total = students[count].scores[0] +  students[count].scores[1] +  students[count].scores[2]  +                     students[count].scores[3]
            average = total /4
            percentage = 10 * average
            test the percentage and display if less than 50
NEXT

The italic text is still to be worked out. You may be confused about the notation students[count].scores[0] etc. This is due to the syntax of records. Remember we have a record of

Record Student
            String name
            int scores[4]
End record

So if I have a variable “a” of type “Student” I could set it up with the following code -

a.name = “Bert”
a.scores[0] = 5
a.scores[1] = 8
a.scores[2] = 1
a.scores[3] = 8

As we have an array of records of type “Student” which we call “students” (notice the plural) we can access a single record by students[3] or replace this with a variable students[count]. Bringing them together students[1].scores[1] means would access the second students second test. Why not first student and first test? Do not forget arrays start at zero (0)!!!!

The eagle eyed amongst you may of spotted the chance for a second loop. We could use a loop to add up the test scores. This would enable us to take advantage of the array and generalise the code. This term “generalise” is a desirable property. The more general a algorithm is he more values it can handle. By using an array we can allow for the situation where a student may of taken more than 4 exams (or even less!) The code would become -

FOR count =0 TO students.length -1
            total = 0
            FOR j = 0 to students[count].scores.length -1
                        total = total + students[count].scores[j]
            NEXT
            average = total /4
            percentage = 10 * average
            test the percentage and display if less than 50
NEXT

The students.length will state the end size of the array which we subtract 1 from as the array starts at zero. Secondly we use a inner or nested for loop to deal with adding up the test scores. The code above allows us to

  1. Have any number of students
  2. Who each independently can take a different number of exams.