A Brief Introduction to Arrays

Last week, you learned that selection of one of several actions can be accomplished by a "cascaded conditional" statement, and saw some examples. Here is another example: determine a student's final class letter grade from the percentage grade. In rather compact pseudocode:

If (numericGrade >= 90) then letterGrade = 'A';
    Else if (numericGrade >= 80) then letterGrade = 'B';
    Else if (numericGrade >= 70) then letterGrade = 'C';
    Else if (numericGrade >= 60) then letterGrade = 'D';
    Else letterGrade = 'F';
End If

That's fine for one student.  But there are 32 students in this class.  If I have variables numericGrade1 and letterGrade1 for student 1, numericGrade2 and letterGrade2 for student 2, etc., then to compute all the grades I would have to write

If (numericGrade1 >= 90) then letterGrade1 = 'A';
    Else if (numericGrade1 >= 80) then letterGrade1 = 'B';
    Else if (numericGrade1 >= 70) then letterGrade1 = 'C';
    Else if (numericGrade1 >= 60) then letterGrade2 = 'D';
    Else letterGrade1 = 'F';
End If

If (numericGrade2 >= 90) then letterGrade2 = 'A';
    Else if (numericGrade2 >= 80) then letterGrade2 = 'B';
    Else if (numericGrade2 >= 70) then letterGrade2 = 'C';
    Else if (numericGrade2 >= 60) then letterGrade2 = 'D';
    Else letterGrade2 = 'F';
End If

Etc., etc., ...

If (numericGrade32 >= 90) then letterGrade32 = 'A';
    Else if (numericGrade32 >= 80) then letterGrade32 = 'B';
    Else if (numericGrade32 >= 70) then letterGrade32 = 'C';
    Else if (numericGrade32 >= 60) then letterGrade32 = 'D';
    Else letterGrade = 'F';
End If

That's 140 lines of code.  There has got to be a better way!

But suppose that instead of numericGrade1 and letterGrade1, I could write numericGrade[1] and letterGrade[1], numericGrade[2] and letterGrade[2],etc.  Then I can do the whole above job with a counter-controlled loop:
 

For (count = 1; count <= 32; count = count + 1) 
   If (numericGrade[count] >= 90) then letterGrade[count] = 'A';
      Else if (numericGrade[count] >= 80) then letterGrade[count] = 'B';
      Else if (numericGrade[count] >= 70) then letterGrade[count] = 'C';
      Else if (numericGrade[count] >= 60) then letterGrade[count] = 'D';
      Else letterGrade[count] = 'F';
   End If
End For

What we have here is two arrays.  An array is a group of several variables of the same type, of some specific size. An array is given a variable name, like numericGrade, and the specific item in the group is referred to by the array name, followed by an integer value in square brackets.

Of course, arrays have to be declared, just like any other variable. When you declare an array, you have to give it an name and a data type (just like an ordinary variable), and you also have to specify the size of the array.  The declarations appropriate to the above situation are

    Declare numericGrade[50] as Integer
    Declare letterGrade [50] as Character

You have to choose an array size big enough to hold as much data as you expect.  But there is no harm in choosing a size bigger than you think you need.  There are (or were) 32 students in this class, but if I am writing something that I might want to use in the future, I can plan for larger class sizes now.  So I chose 50.

Important point: Contrary to what you might expect, in almost all modern programming languages (Including C and Java), the elements of an array are numbered starting at 0. So if I have an array Xarray of 50 elements, the elements are  they Xarray[0] through Xarray[49]. We will follow this convention in this class.

Click here for pseudocode for a complete program that uses the arrays and loops described above.
 

In our example above, you saw two arrays.  Take a look at the kinds of data you deal with in your workplace.  Find some arrays! They will be lurking everywhere...