The following notes supplement the material in Chapter 7.
Arrays:
Definition
We use the term array to refer to a named collection of items. The collection has the following properties:
e.g. Consider the array named prefix, defined to contain 5 numeric items. Let us say these items are prefixes for Illinois telephone numbers. That is, the array contains the items {312, 773, 847, 603, 708}. Each item in the array would be referenced thus:
prefix[1] = 312 prefix[2] = 773 prefix[3] = 847 prefix[4] = 603 prefix[5] = 708Note: We will refer to the first item in an array with the index 1. We will not use 0 to refer to the first item.
Problem
The array numbers is defined to contain n numbers. Develop an algorithm to compute the average of the first 4 numbers in the array. Assume the array contains {10, 20, 2, 4} in the first 4 cells.
Solution
Strategy: Step through the array adding each item to a running total. After adding the 4th item, compute the average. We will be repeatedly adding and so an iteration structure seems appropriate.
total := 0 i := 1 while (i =< 4) do total := total + numbers[i] i := i + 1 endwhile average := total/4The following is an execution trace of this algorithm:
Iteration # ------------------------------------- Initial | 1 | 2 | 3 | 4 ===================================== total: 0 | 10 | 30 | 32 | 36 ------------------------------------- i: 1 | 2 | 3 | 4 | 5 -------------------------------------Hence, at the end of the iteration structure, the variable total has value 36 and so average is 36/4 = 9.
Problem
For the array above, develop an algorithm to find the smallest number and its position in the array.
Solution
Strategy: Step through the array examining each item. If the current item is smaller than the previous smallest item then note the current number and its position. Let us start out by assuming that all numbers in the array are smaller than some very large number (say 9999). An iteration structure again seems appropriate.
min := 9999 minpos := 0 i := 1 while (i =< 4) do if (number[i] < min) then min := number[i] minpos := i endif i := i + 1 endwhileThe following is an execution trace of this algorithm:
Iteration # ------------------------------------- Initial | 1 | 2 | 3 | 4 ===================================== min: 9999 | 10 | 10 | 2 | 2 ------------------------------------- minpos: 0 | 1 | 1 | 3 | 3 ------------------------------------- i: 1 | 2 | 3 | 4 | 5 -------------------------------------Hence, the smallest number is 2 and it is the 3rd item in the array.
Problem
The array word is defined to contain n characters. That is, each cell of the array may contain a character. Develop an algorithm to find the first occurrence of the letter 'a' in the array. Assume that the array contains the string "william.". Notice that the character '.' is used to indicate the end of the word.
Solution
Strategy: Step through the array examining each item. If the current item is the letter 'a' then it has been found so note its position. An iteration structure again seems appropriate.
found := F pos := 0 i := 1 while ((word[i] not= '.') and (found = F)) do if (word[i] = 'a') then found := T pos := i endif i := i + 1 endwhileThe following is an execution trace of this algorithm:
Iteration # --------------------------------------------------- Initial | 1 | 2 | 3 | 4 | 5 | 6 =================================================== found: F | F | F | F | F | F | T --------------------------------------------------- pos: 0 | 0 | 0 | 0 | 0 | 0 | 6 --------------------------------------------------- i: 1 | 2 | 3 | 4 | 5 | 6 | 7 ---------------------------------------------------Hence, the letter 'a' is the 6th item in the array. Notice the use of the boolean variable found in this algorithm.
Linked Lists:
Definition