Class Crib Notes for CPSC 110                                Brent Dingle

                                    Monday, April 22, 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 and MultiDim Arrays  (Chapter 10)

 

Parallel arrays are nice. Recall we could use two (or more) arrays to track student information. For example say we wanted to keep track of 3 things about you as a student:

  1. Your Name
  2. Your Student ID
  3. and your test scores (and you only took 3 tests) (this will be a 2D array).

We could use the following 3 parallel arrays:

 

CONST

   NUM_STUDS = 100;

   MAX_TESTS = 3;

TYPE

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

   IDAry = array[1..NUM_STUDS] of string;

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

VAR

   Name : NameAry;

   ID : IDAry;

   Test : Test2DAry;

 

So if we wanted to print the name of student 5 we would say:

writeln(Name[5]);

 

And to print the ID of student 5 we would say

writeln(ID[5]);

 

And if we wanted to print all 3 tests scores for student 5 we would say:

for i := 1 to MAX_TEST do

Begin

   writeln(‘Score for test number ’,  i, ‘ is ’, Test[5][i]);

End

 

 

 

 

 

For today:

Records (Chapter 11)

 

Records allow us to create a structure to hold a variety of related information. In English you might ask to see your student record which would really mean you want to see your grades, your vaccinations, your teacher reports, etc.

 

For now let’s pretend the only thing in your student record is:

  1. Your Name
  2. Your Student ID
  3. and your test scores (and you only took 3 tests).

 

To model this in Pascal using a RECORD TYPE we would declare the following:

 

CONST

   MAX_TESTS = 3;

 

TYPE

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

 

   STUD_REC = RECORD

                             name : string;

                             id      : string;

                             test    : TestAry;

                        END;

VAR

   a_single_student : STUD_REC;

 

This allows us to store the information of ONE student in just one variable.

So we could set the student’s name by using the following command:

a_single_student.name := ‘Bob Robert Jones’;

 

We could set the student’s ID by using the following command:

a_single_student.id := ‘123-45-6789’;

 

We could set the student’s FIRST test score with the command:

a_single_student.test[1] := 89.56;

 

To set the SECOND test score we would use the command:

a_single_student.test[2] := 83.2;

 

To set the THIRD test score we would use the command:

a_single_student.test[3] := 85.92;

 

Which is great, right?  Of course it is. Now what if we want to store the records of say, 100 students? We make 100 variables, all of different names, of type STUD_REC? Like this:

VAR

   stud1 : STUD_REC;

   stud2 : STUD_REC;

   stud3 : STUD_REC;

   :

   stud100 : STUD_REC;

NO!

Instead we will use an array indexed from 1 to 100 of type STUD_REC. The full code to do this is below:

 

CONST

   NUM_STUDS = 100;

   MAX_TESTS = 3;

 

TYPE

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

   STUD_REC = RECORD

                            name : string;

                            id      : string;

                            test    : TestAry;

                        END;

 

   STUD_ARRAY = array[1..NUM_STUD] of STUD_REC;

 

VAR

   student : STUD_ARRAY;

 

So we could set the name of student 7 name by using the following command:

student[7].name := ‘Bob Robert Jones’;

 

We could set the ID of student 7 by using the following command:

student[7].id := ‘123-45-6789’;

 

We could set the student 7’s FIRST test score with the command:

student[7].test[1] := 89.56;

 

To set the SECOND test score of student 7 we would use the command:

student[7].test[2] := 83.2;

 

To set the THIRD test score of student 7 we would use the command:

student[7].test[3] := 85.92;

 

 

Likewise if we wanted to set the information of student 23 we could say:

student[23].name := ‘Sally Sue Sanderson’;

student[23].id := ’987-65-4321’;

student[23].test[1] := 97;

student[23].test[2] := 87;

student[23].test[3] := 92.4;

 

 

 

DEFINITION:

KNOW that in the above example name, id, test are examples of FIELDS of a record.

 

So in the Record, STUD_REC, there are 3 fields named: name, id and test.

Each field may be of a different type.

 

Notice if you declare a variable to be of a record type you access the fields of the variable by saying variable_name.field_name.  Notice the period between variable_name and field_name.

 

You will be expected to be able to write and determine the output of code using variables of defined record types.

Please see the sample program rec_grad.pas posted on the web.

 

 

IMPORTANT:

There are two other items from Chapter 11 you MUST KNOW:

Stacks and Queues. Please read the book on them.

Specifically be absolutely certain you know:

Stacks are like stacks of plates – they work as a Last In First Out Structure (LIFO)

Queues are like a line –  they work as a First In First Out Structure (FIFO)

 

(notice Last In First Out implies First In Last Out and First In First Out implies Last In Last Out)