Class Crib Notes for CPSC 110                                Brent Dingle

                                    Monday, April 1, 2002

Reminders:

Assignment 6 – do NOT use arrays (even though we talk about them today), begin working on it as soon as possible – it will take a while to do.

Quiz next Wednesday

TA’s should return go over the quiz in lab this week – mean average was near 84%.

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

 

Review:

Recall last time we were talking about Abstract data types and keyword TYPE.

Recall what ordinal and enumerated data types were:

ORDINAL TYPES:

Definition:

Types whose values can be specified by a list are ordinal types.

(also called scalar types and types with a finite number of elements)

 

Examples:

integer

char

PRIME_COLORS = (red, green, blue)

 

ENUMERATED TYPES:

Definition:

Enumerated types are user (programmer) defined ordinal types. 

So PRIME_COLORS above would be an enumerated type.

 

Enumerated Types have TWO properties:

1.        The order of their members

2.        The names of their members

 

Check out examples on the web: rainbow.pas and rainbow2.pas, maybe others.

 

For Today:  Arrays (Chapter 9)

 

Motivation:   Consider the following program:

PROGRAM Grades1;

VAR

   grade1, grade2 : real;

   grade3, grade4 : real;

   avg : real;

 

PROCEDURE GetGrade(var grade : real; grade_num : integer);

BEGIN

   Write(‘Enter grade ’, grade_num, ‘ --> ‘);

   Readln(grade);

END;

BEGIN { main }

   GetGrade(grade1, 1);

   GetGrade(grade2, 2);

   GetGrade(grade3, 3);

   GetGrade(grade4, 4);

 

   avg := grade1+grade2+grade3+grade4;

   avg := avg / 4;

 

   Writeln(‘Average = ‘, avg);

END.

That’s all good until you want to deal with 20 grades – then you would need to alter the program to have 20 variables: grade1, grade2, … grade19, grade20.

The problems with that: Too much typing and too hard to alter the program.

 

There is of course a better way.

 

As you might suspect each variable we have seen so far takes up a specified amount of memory, for example an integer takes up 2 bytes.

 

Think of the memory it occupies as a box and the box is referenced by the variables name.

 

 

 

Or perhaps a shelf diagram is better:

 

 

Now what if we had a lot of different things of the same type in boxes – for example shoes. Would we want to store them all in boxes scattered about everywhere?

No.

 

We would likely put them together – perhaps stacking their boxes one on top of each other (or use a rack or set of shelves to organize them). Then instead of looking at a bunch of different shoeboxes we simply can look at the rack of shoes.

 

 

 

 

With this in mind let’s consider an what arrays are.

 

Arrays are collections of values all of the same type

.

 

Properties of arrays in terms of shelves:

 

The best way to declare an array in Pascal is to create a Type for the desired array:

TYPE

   ARY_0_TO_4_REAL = array [0..4] of real;

VAR

   x : ARY_0_TO_4_REAL;

So in the above we would have an array named x of type real containing 5 elements where the first element is at index = 0.

Notice usually you will pick a better TYPE name, but for illustrative purposes ARY_0_TO_4_REAL was used.

 

Here we may talk about how the memory storage works like boxes and how the indexing works with memory – see someone who was in class for good notes.

 

 

To access the values stored in an array

You use the array name and the index of the desired element, where the index is placed in square brackets.

So if we wanted to display the element at index 3 we would say:    Writeln(x[3]);

 

Let’s now go back to our Grade program, but this time we will use an array named grade of type real containing 4 elements where the first element is at index = 1.

 

 

PROGRAM Grades2;

TYPE

   GRADE_ARRAY = array[1..4] of real;

VAR

   grade : GRADE_ARRAY;

   avg : real;

   index : integer;

 

 

PROCEDURE GetGrade(var grade : real; grade_num : integer);

BEGIN

   Write(‘Enter grade ’, grade_num, ‘ --> ‘);

   Readln(grade);

END;

 

 

 

BEGIN { main }

   avg := 0;

   FOR index := 1 to 4 DO

   BEGIN

      GetGrade(grade[index], index);

      avg := avg + grade[index];

   END;

   avg := avg / 4;

 

   Writeln(‘Average = ‘, avg);

END.

 

 

 

Downsides to Arrays

x[0] would be out of bounds

x[34] would be out of bounds

If index = 5 then

x[index] would be valid but

x[index + 1] would NOT be valid

Sometimes the compiler catches these errors (as in the first 2 cases above), sometimes it does not (as in the x[index + 1] case. Bad things may result.

 

.