5.2 Using Arrays

There are many uses for arrays. In this section we'll take a look at some examples. We'll look at how to work with arrays.

Suppose a school wanted to keep track of how many students had each mark. Here is a program that would create an array for the total number of students with each mark. It will initialize all the values to 0. Since this array is relatively large we will not be using the init approach to initialize the array.

var marks : array 0 .. 100 of int

for i : 0 .. 100
   marks(i) := 0
end for 

You have to be careful distinguishing between indices and elements of arrays, especially if the types are the same. Here are some statements for working with the marks array, and what they do:

Consider the following program:

var results : array 1 .. 6 of int := init (0, 0, 0, 0, 0, 0)
var testNo : int

loop
   put "Which test do you want to enter a result for? Enter a negative number to end."
   get testNo
   exit when testNo < 0
   if testNo > 6 then
      put "That is not a valid test, try again."
   else
      put "What did you get on test #", testNo, "?"
      get results(testNo)
   end if
end loop

put ""
put "Here are your current test results:"
put "Test    1   2   3   4   5   6"
put "------------------------------"
put "     " ..
for i : 1 .. 6
    put results(i):4 ..
end for 

This program keeps asking for test numbers and results until the user enters a negative number. It also needs to make sure that the test numbers the user requests are in the valid range (1-6). If the program tries to access an element of results that has an index less than 1 or greater than 6 it will crash because the array index is out of bounds. Here is a sample output from the program:

Which test do you want to enter a result for? Enter a negative number to end.
2
What did you get on test #2?
86
Which test do you want to enter a result for? Enter a negative number to end.
4
What did you get on test #4?
91
Which test do you want to enter a result for? Enter a negative number to end.
5
What did you get on test #5?
95
Which test do you want to enter a result for? Enter a negative number to end.
8
That is not a valid test, try again.
Which test do you want to enter a result for? Enter a negative number to end.
1
What did you get on test #1?
79
Which test do you want to enter a result for? Enter a negative number to end.
-1

Here are your current test results:
Test    1   2   3   4   5   6
------------------------------
       79  86   0  91  95   0

Exercise 5.2

  1. State the effect of each program fragment assuming that the following array declarations have been made and MAX is some constant that's been defined.
    var score, copy : array 1 .. MAX of int 
    1. for i : 1 .. MAX
         score(i) := 0
      end for 
    2. total := 0
      for i : 1 .. MAX
         total := total + score(i)
      end for
    3. for i : 1 .. MAX
         copy(i) := score(i)
      end for 
    4. for i : 1 .. MAX
         copy (i) := score(MAX - i + 1)
      end for
  2. For each program fragment, give any declaration(s) that would be needed and state the purpose of the fragment.
    1. for x : -5 .. 5
         value(x) := x**2
      end for
    2. for category : 'A' .. 'E'
         get wage(category)
         put "Category: ", category, " Hourly Wage: $", wage(category):5:2
      end for
    3. for i : 0 .. 5
         switch(i) := false
      end for 
    4. for i : 1 .. LIST_SIZE div 2
         temp := list(i)
         list(i) := list(LIST_SIZE - i + 1)
         list(LIST_SIZE - i + 1) := temp
      end for 
    5. for i : 1 .. LIST_SIZE
         temp := list(i)
         list(i) := list(LIST_SIZE - i + 1)
         list(LIST_SIZE - i + 1) := temp
      end for 
  3. Suppose that an array sample has been created using the following declaration:
    var sample : array 1 .. SAMPLE_SIZE of int 
    Write one or more statements to perform each operation.
    1. Initialize all elements to zero.
    2. Replace the third element by the value 28.
    3. Copy the value in the first location into the eighth location.
    4. Store the sum of the values in the first and second location into the ninth location.
    5. Replace each element of the array by twice its current value.
    6. Change any negative values to positive values (of equal magnitude).
    7. Print the contents of the odd numbered locations.
    8. Swap the values in the first and second locations.
  4. For each program, state what it does and simplify it.
    1. var count : int := 0
      var value : array 1 .. 100 of int
      
      loop
         exit when count > 100
         get value(count + 1)
         count := count + 1
      end loop 
    2. var size : int := 1
      var inventory : array 1 .. 4 of int := init (1 ,2, 3, 4)
      
      loop
         put inventory(size), " of size ", size
         size := size + 1
         exit when size = 5
      end loop 
    1. Complete the following program.
      % This program reads a set of responses to a survey in which there
      % are five possible answers: A - E. The program then writes a summary
      % giving the number of times that each response was made.  The program
      % assumes that all data are valid and that a dollar sign will be the
      % last value entered.
      
      const FIRST_CHOICE := 'A'
      const LAST_CHOICE := 'E'
      var total : array FIRST_CHOICE .. LAST_CHOICE of int
      var response : char
      
      % Initialize array for counting responses.
      .
      .
      .
      % Read responses and increment corresponding array location to count the
      % frequency of each response.
      .
      .
      .
      % Write summary of responses.
      .
      .
      . 
    2. Show the output that would be produced by your program given the following input:

      ECCBBBEAAEDCACBBEAEDBBC$

    1. Write a program that will read a set of ten real values into an array and then find the maximum.
    2. Modify your program so that it also finds the minimum value, without making an extra pass through the array.
    3. Rewrite the program of part b) without using an array.
  5. The mode of a group of values is the value that occurs most often in the group. A group may have more than one mode. This is the case if more than one value occurs with the maximum frequency. Such a group is said to be multi-modal. (If there are exactly two modes, the values are said to be bi-modal.) Write a program that will read an unknown number of marks (out of 100) and determine the mode(s) of those marks.