Parallel Arrays 


In many problems there are several pieces of information that are related, but have a different type.  

If you had Name and Scores, you might set up a String array for Name and a Single array for Scores.

A particular Score can be associated with a certain Name because they have the same position, and therefore index, in their respective arrays.


Count the frequency of occurrence of grades in an array of scores.

If you were to do this by hand you would probably make a list of the grades that you want to count.  Then you would start scanning the score array score-by-score, and when you encounter one of the grades that you are seeking you would put a hash mark by it in your list.

Such an algorithm can be used directly.  The list of grades whose frequency is to be counted can be read into an array of type String.  

To simulate recording a hash mark, use a second array that is the same size as the one containing the grades.  This parallel array will be of type Integer.  Scan (search) the grade array for a matching grade.  When a match is found return the place in the array where the search item was found, and index can be used as an index into the parallel array to add one to that position.  For example, if the first grade in our list is an "A", then each time an "A" is found the first slot in the Integer array is incremented by one.

For studentPtr = 0 to lastIndexUsed(gradeBook)
    Matched = False
    gradePtr = 0
    While gradePtr <= lastIndexUsed(gradeList) And Not Matched 
          If  ( gradeBook (studentPtr) = gradeList (gradePtr) ) Then
              frequency (gradePtr) = frequency (gradePtr) + 1
              Matched = True
              gradePtr = gradePtr + 1
          End If
    End While
Next studentPtr