Unit 5.5 -- 2-D Array of Counters

PROG

Input: A file containing pairs:
precinctno, crimeno

        Each crime is associated with one such pair 
Output: List indicating how many times each crime occurred in each preccinct

Crime types: numbered 1 to 5
Precinct Types: numbered 1 to 7

PED

To learn about counting pairs with 2-d arrays

CONCEPT

To count pairs of x,y where x is from m to n and y is p to q

CountArray:array[m..n,p..q] of integer;

loop to zero out CountArray

for each x,y pair
CountArray[x,y]:=CountArray[x,y]+1;



Unit 5.5 -- 2-D Array of Counters

Unit 5.3 discussed a situation where we counted the number of crimes in each precinct. However, a raw count of crimes isn't of much meaning. We need to know which type of crime. A precinct with a lot of murders has a more serious crime problem than one with a lot of petty larcenies.

We need a printout that tells us which crimes are happening in which precincts.

We will again number our precincts from 1 to 7. We will also number our crimes from 1 to 5. (The user will know that code 1 means murder and code 5 means rape and code 3 means petty larceny.)

Our data might look something like this

7 1
7 4
5 1
7 4
-1

This means that a crime of type 1 happened in precinct 7.
Then we had a crime of type 4 in precinct 7.
Then in precinct 5, we had a crime of type 1.
Then, in precinct 7, we had a crime of type 4.

The -1 at the end is a sentinel.

In this case, we will report that we had two instances of crime type 4 in precinct 7. There were one each of crime type 1 in precincts 5 and 7. The others would be zero. Our program as written would report all zero values. In some cases, we might want to only print those crimes and precinct combinations that occurred at least once. How would we modify the above program?

At the end, we might also want to print out such results, as the most frequent crime over all the precincts, the precinct with the most crime of any type, the total number of crimes committed, etc.

Let's look at our program in detail.

Line 2 sets up an array to contain the counts of each pair. The first subscript is 1 to 7, as this corresponds to precincts. We defined the problem as saying there are seven precincts. Our problem definition also includes five crime-types numbered 1 to 5. Thus, the second subscript is declared 1..5.

Lines 10 to 14 is a simple nestedfor loops to zero out the entire array. Initially, before any data is read, there are no crimes of any type in any precinct.

Lines 15 to 20 do the actual work of the program. PrecinctNo at line 20 contains the number of the precinct for the current pair. If that is equal to -1, we leave the loop--note the code is set up so we don't attempt to read the second number of the pair when there is a -1.

In line 17, it is now OK to read in the second number of the pair. Line 18 increments the appropriate box in the two-diemnsional array. And lastly, in line 23, the program reads in the next number.

Lines 21 to 28, go through the two-dimensional array and print out each of the counts labled with the corresponding Precinct Number and Crime Number.