Class Crib Notes for CPSC 110                               Brent Dingle

                                    Wednesday, April 17, 2002

Reminders:

Quiz 6 is Wed April 24

Don’t forget to write up the CS history report.

Check out the sample programs: parallel.pas and alpha.pas for help on Lab 7.

 

Last time:

Parallel Arrays  (Chapter 10)

CONST

   MAX_STUDS = 100;

TYPE

   NameAry = array[1..MAX_STUDS] of string;

   TestAry = array[1..MAX_STUDS] of real;

 

VAR

   Name : NameAry;

   Test   : TestAry;

 

Which links a student name and his/her test score by the index number into the array.

For example

If Name[3] = Mary and Test[3] = 98

Then we conclude Mary received an 98 on the test.

 

But what if there were multiple tests?

 

Today:

Chapter 10 – array of arrays and multidimensional arrays

Maybe get into a program to alphabetize an array of names (see alpha.pas on the Web)

 

Multi-Dimensional Arrays:   

So again what if each student took 3 tests? Do we want to make 3 arrays of type test? As shown below?

CONST

   MAX_STUDS = 100;

TYPE

   NameAry = array[1..MAX_STUDS] of string;

   TestAry = array[1..MAX_STUDS] of real;

 

VAR

   Name : NameAry;

   Test1   : TestAry;

   Test2   : TestAry;

   Test3   : TestAry;

 

Probably not. Instead we will make a 2 dimensional array. The first index will refer to the student’s associated index. And the second number will be the test number (e.g. Name[3] is Mary, So Test[3][i] is the score of the i-th test Mary took)

 

To set that up we would use the following:

CONST

   MAX_STUDS = 100;

   MAX_TESTS = 3;

TYPE

   NameAry = array[1..MAX_STUDS] of string;

   Test2DAry = array[1..MAX_STUDS, 1..MAX_TESTS] of real;

 

VAR

   Name : NameAry;

   Test    : Test2DAry;

 

So we would have a setup as follows:

index

Name[index]

1

Bob

2

Sally

3

Mary

4

John

 

So student 1 is Bob, student 2 is Sally, student 3 is Mary, student 4 is John.

 

Our Test array would look like this:

 

Test 1

Test 2

Test 3

Student 1

87.5

88.3

90

Student 2

54

60

73

Student 3

98

87

80

Student 4

78

84

82

 

Our indexing is in ROW-MAJOR order so the ROW is the first index, in this case the student number. So

Test[3][2] = 87    which means Student 3 (who is Mary) scored 87 on test #2.

Test[3][1] = 98    which means Student 3 (who is Mary) scored 98 on test #1.

Test[3][3] = 80    which means Student 3 (who is Mary) scored 80 on test #3.

 

Likewise

Test[4][1] = 78    which means Student 3 (who is John) scored 78 on test #1.

Test[4][2] = 84    which means Student 3 (who is John) scored 84 on test #2.

Test[4][3] = 82    which means Student 3 (who is John) scored 82 on test #3.

 

And so on.

 

 

Arrays of Arrays:   

Example 1:

                CONST

                   MAX_STUDS = 100;

TYPE

   NameAry = array[1..MAX_STUDS] of string;

VAR

   Name : NameAry;

 

Recall strings are really ‘fancy’ arrays so the above variable Name is an array of arrays.

For example if Name[5] = ‘Robert Jones’

then we refer to the 3rd character of Name[5] by using the following notation:

Name[5][3]

 

Example 2:

Consider a large company (like Sears) of department stores. Each store has the same departments, for example a shoe depart, a children’s department, a women’s department and a men’s department. You might want to create an array for a single store to hold the income of each department in a given day:

TYPE

   DeptNameType = (Shoes, Children, Women, Men);

   StoreSalesAry = array[DeptNameType] of real;

 

VAR

   StoreSales : StoreSalesAry;

 

So StoreSales[Shoes] will tell you how much the shoe department made.

StoreSales[Children] will tell you how much the children’s department made

and so on.

 

That works fine for an individual store. But you are the company president and want to know the overall status of all your stores (by department). So you might want to know how much store #1’s shoe department made versus store #5’s shoe department.

 

This requires the use of an array of arrays:

 

CONST

   MAX_COMPANIES = 50;

 

TYPE

   DeptNameType = (Shoes, Children, Women, Men);

   StoreSalesAry = array[DeptNameType] of real;

   CompanySalesAry = array[1..MAX_COMPANIES] of StoreSalesAry;

 

VAR

   CompSales : CompanySalesAry;

 

CompSales[1][Shoes] will tell you how much the shoe department of store #1 made.

CompSales[5][Shoes] will tell you how much the shoe department of store #5 made.

Likewise,

CompSales[3][Men] will tell you how much the men’s department of company 3 made.

CompSales[7][Men] will tell you how much the men’s department of company 7 made.