Tables

A table is essentially an array of records - each element of the array has a number of fields of possibly different types. A table is generally held in the computer's memory and is used to 'look up' information.

A hash table is a special table which holds variable names and their addresses, so that the address of any variable can be quickly looked up.

Example : A teacher might have a list of pupil names,  forms and exam marks. If he wants to know what exam mark a certain pupil had, then he looks down the list of names until he finds the one he wants, then looks across to the corresponding mark.

'Marks' is a table of pupils and their exam marks.
Each record has three fields:

  • Name : String
  • Form : String
  • ExamMark : Integer
  Name Form ExamMark
1 Tom Jones 11G 78
2 Bessie Smith 11B 56
3 John Conrad 11G 46
4 Sarah Williams 11Y 88

This table has 4 records.

Marks[3].Name is 'John Conrad' - a subscript and a field name is needed.

 

Exercise

Using the table (array of records) called Marks, what would be output by the following code segments...

output(Marks[2].Form)
num := 4;
output(Marks[num].ExamMark)
for num := 1 to 4
    if Marks[num].Name="Bessie Smith" then
          output(Marks[num].ExamMark)
next num
(Green indicates success)