What information needs to be saved?

When we save or store information we obviously will be using a variable. The variable(s) will have specific data types which can be a primitive (int, float etc) or a data structure (array, stack etc). Consider the problem below -

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.

The first step is to decide on what the input and out put are -

Input – students test scores
Output – list of students who need to retake

So we need to decide on how the input and output will be stored. Starting with the output we could easily create an array of Strings to hold the list of names. Or we could just simply display them rather than store them (so when decided we just print immediately). In this case we will simply just display them when we come across them.

The input could be stored in many ways. We could use a record which would store -

Record Student
            String name
            int score1
            int score2
            int score3
            int score4
End record

or even

Record Student
            String name
            int scores[4]
End record

Using a record for this (see fixed length records) is the simplest way to structure the data. We could do it in other ways if we wished to.

We also need to store the average score. We will not be outputting it but it is needed during the algorithm. Intermediate steps in a algorithm may also need to save data. To work out the average we need to add up 4 scores and then divide by 4. We may have some code like this -

total = score[0] + score[1] + score[2] + score[3]
average = total / 4

We also need to calculate the percentage so we would need to (assuming the test is out of 10)-

percentage = 10 * total

After analysing the problem we are left with the following storage requirements -

total = will store the sum of all test scores for one student
average = will store the average score for a single student
percentage = will store the percentage of a single student
students = array of records (of type Student) which will store all of the input data