5.1 Arrays

If you have several pieces of data, all of the same type then arrays can be a very useful data structure. Suppose you wanted to store the results (out of 100) of five tests that you wrote. You could use five different variables. That is not very convenient to work with. If you have 5000 values to store then using different variables really doesn't work. A better approach is to use arrays. You can visualize an array as follows:

Test #12345
Results8579899577

The test number is the index of the array. The test results are the elements of the array. The elements are the actual values we are storing in the array and the index is how you find a particular element. To declare an array you need to know what the first index value will be, the last index value, and what type of elements you need to store. In the above example, 1 is the first index value, 5 the last index value and we are storing elements of type int. We would call our array results since it is the results of the tests that we are storing. Here is the Turing statement that would declare the array:

var results : array 1 .. 5 of int 

To declare an array you need to start the declaration with the word array. You then specify the first and last values for the index. This is similar to the way for loops are set up. Finally you say what type the array will be.

This only sets aside space in the computer's memory to store the results. No results are stored yet. We need to put the results into the array before we can use the array. There are several ways to accomplish this. If you want to store some initial values in the array when you declare it you can. Here is how.

var results : array 1 .. 5 of int := init(85, 79, 89, 95, 77) 

You specify the initial values in brackets after the keyword init. You need to make sure you specify exactly the right number of initial values. In our example there must be exactly 5 since our array can hold five elements. You will get an error if you specify too many or too few. To access individual elements you use the name of the array followed by an index inside brackets. For example, to refer to the second test you would use results(2). The last element would be results(5). You must be careful to only use valid indices. For instance, results(6) would give an error since there is no sixth element.

The indices of the array can be a range of int, char or boolean (technically there are other possibilities but we will not be looking at those). If you use the boolean type for the index then you can only have 2 elements in the array (false .. true). When using ints for the index you do not have to start at 1 (although this is often most convenient). The only requirement is that the first index must be lower than the last index. Suppose you wanted to store the price of a stamp for the years 1980 to 2003. You could declare the array as follows:

var stampPrice : array 1980 .. 2003 of real 

In this example it makes sense to start the index at a value different from 1. The prices will be stored as amounts in dollars so we need to make the elements of the array real. Suppose you wanted to store the number of students who got different letter grades in a class. Then you could make the indices go from 'A' to 'F'. Here's how we could declare such an array:

var grades : array 'A' .. 'F' of int 

In the next section we'll learn more about using arrays.

Exercise 5.1

  1. A portion of the table of atomic masses of the elements according to atomic number is shown below.

    Number12345
    Atomic Mass1.014.006.949.0110.81

    Suppose that this was stored as an array called atomicMass.

    1. What is the value of atomicMass(3)?
    2. What is the value of atomicMass(5)?
    3. What is the name of the element whose value is 4.00?
    4. What is the name of the element whose value is 1.01?
    5. What are the indices of the array?
    6. Of what type are the indices?
    7. Of what type are the elements?
  2. A substitution cipher is a table that maps each letter of the alphabet onto another letter. Consider a simple substitution cipher table in which each letter maps onto the letter three places further in the alphabet ("wrapping around" at the end of the alphabet) as shown.

    Letter'A''B''C'...'Y''Z'
    Cipher'D''E''F'...'B''C'

    Suppose that this was stored as an array called cipher.

    1. What is the value of cipher('P')?
    2. What is the name of the element whose value is 'E'?
    3. What is the name of the element whose index is 'D'?
    4. What is the value of the element whose index is 'W'?
    5. What is the index of the element whose value is 'C'?
    6. What index gives a cipher of 'L'?
    7. What is the type of the indices?
    8. What is the type of the elements?
  3. How many elements can be stored in each of the following arrays?
    var a : array 1980 .. 1990 of real
    var b : array -10 .. 10 of int
    var c : array 'a' .. 'h' of int
    var d : array -5 .. 5 of boolean
    var e : array false .. true of char 
  4. Write declarations to create arrays for storing the information in the following tables:

    1. Time (h)012...12
      Temperature (oC)111416...-7
    2. Question123...10
      Mark5510...8
    3. Size'A''B''C''D'
      Quantity240185210375
  5. Identify, with reasons, any of the following declarations that are illegal
    const FIRST := 'A'
    const LAST := 'J'
    var a : array -20 .. -15 of int
    var b : array 'p .. 'k' of char
    var c : array FIRST .. 'E' of real
    var d : array 1 .. LAST of boolean
    var e : array FIRST .. LAST - 1 of string
    var f : array 1 .. 1e1 of int
    var g : array 1 .. 5 of int := (1, 2, 3, 4, 5)
    var h : array 5 .. 8 of string := init("one", "two", "three", "four")
    var i : array 2 .. 5 of int := init(1, 2, 3)