|
|
|
|
|
|
|
const int MAX_NUMBER = 500; // Maximum in each list
int firstList[MAX_NUMBER]; // Holds first list |
|
|
|
|
|
|
|
|
Because the first array component has index 0, we must think of our pad of paper as having its lines numbered from 0, not 1. |
|
|
|
|
|
|
|
|
Now we can complete the top-down design and program for our problem. |
|
|
|
|
|
|
|
|
Assumption: The two lists to be compared are of equal length. |
|
|
|
|
|
|
|
|
Data Structures: A one-dimensional int array (firstList) to hold the first list of numbers. See Figure 11-10. |
|
|
|
|
|
|
|
|
Open dataFile (and verify success)
Read first list
Set allOK = TRUE
Compare lists, changing allOK if necessary
IF allOK
Print "The two lists are identical" |
|
|
|
|
|
|
|
|
|
In the module Read First List, the first input value is stored into firstList[0], the second into firstList[1], the third into firstList[2]. This implies that we need a counter to keep track of which number is being read. When the negative number is encountered, the counter tells us how many of the 500 places set aside were actually needed. We can use this value (call it length) to control the reading and comparing loop in the Compare Lists module. This is an example of subarray processing. |
|
|
|
|
|
|
|
|
Read First List (Inout: dataFile; Out: length, firstList) Level 1 |
|
|
|
|
|
|
|
|
Set counter = 0
Read number from dataFile
WHILE number >= 0
Set firstList[counter] = number
Increment counter
Read number from dataFile
Set length = counter |
|
|
|
|
|
|