Class Crib Notes for CPSC 110                                Brent Dingle

                                    Wednesday, April 3, 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 one week from today.

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:

Last time we began talking about arrays.

 

Recall we declared and used them as follows:

TYPE

   SmallArray = array[1..5] of real;

VAR

   score : SmallArray;

 

BEGIN

   :

   score[1] := 89.4;

   :

END.

 

Some definitions:

 

 

 

 

Last time we saw some examples of using arrays. Today we will see more.

 

First we will mention some of the downsides of arrays.

 

 

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.

 

Aside:

Arrays are considered structured types NOT simple types.

Simple types cannot be broken apart in value – for example the character ‘C’ can only be the character ‘C’. Arrays can be broken apart based on the index.

 

May throw in talk about memory here too

 

Let’s now look at more examples:

(Semantic context)

 

PROGRAM ColorCount;

 

TYPE

   Color = (red, green, blue, white, black);

   CountArray = array[Color] of integer;

   NameArray = array[Color] of string;

 

VAR

   Count : CountArray;

   cname : NameArray;

   Index : Color;

 

BEGIN

   cname[red] := 'red';

   cname[green] := 'green';

   cname[blue] := 'blue';

   cname[white] := 'white';

   cname[black] := 'black';

 

   For index := red to black do

   Begin

      count[index] := 6;

   End;

 

   For index := red to black do

   Begin

      writeln('There are ', count[index], ' cars of color = ', cname[index]);

   End;

End.

 

Notice we pretty much are accessing all the elements of the array in order – this is called sequential access.

 

We may also access arrays in a random fashion – like jump from element 5 to 9 back to 2.

 

 

Strings are special types of arrays in Turbo Pascal

 

Strings in pascal default their indexing to start at 1.

 

VAR

   name : string[20];      -- this way will NOT allow name to be passed to functions and procedures

     e.g. Procedure la(some_name : string[20]);  -- this will cause errors

 

so better:

TYPE

    string20 = string[20];

VAR

   name : string20;

   n : integer;

 

BEGIN {main }

   name := ‘Bob Smith’;

 

   writeln(name[1]);    -- will output ‘B’

   writeln(name[5]);   -- will output ‘S’

   writeln(name[15]);  -- wil output some random thing

   writeln(name[30]);  -- should cause a compiler error

 

   n := pos(‘Smith’, name);    -- will set n equal to 5   ß NEW Function = Pos(…)

END.

 

There are other string specific functions – may talk about later – you should look them up in Chapter 9 – minimally pos, delete, insert, length, copy, upcase