Imagine we want to store some information about someone in a computer program.
name = "Chunhua" #STRING
age = 17 #INTEGER
graduated = False #BOOLEAN
Now we can manipulate the data quite easily. And let's now say that there are two people whose data we want to store and manage.
name = "Chunhua" #STRING
age = 17 #INTEGER
graduated = False #BOOLEAN
name2 = "ChangChang" #STRING
age2 = 19 #INTEGER
graduated2 = True #BOOLEAN
This is still manageable, but it is becoming clear that there is no particular relationship between the variables other than the clues given to us by the identifiers.
In a more complex program, this is going to get very messy.
There are many solutions to this problem and a popular one is to create a record data structure.
A record data structure allows a programmer to store related data together under one identifier, similar to an array.
The difference is that a record allows data of different types to be stored together under one identifier.
To illustrate this concept we will design a new data type in Pseudocode:
TYPE Student
DECLARE name:STRING
DECLARE age:INTEGER
DECLARE graduated:BOOLEAN
END TYPE
We can now create a record of this type and assign data to the properties in the record:
DECLARE chunhua : Student
DECLARE changchang : Student
chunhua.name ← "Chunhua"
chunhua.age ← 17
chunhua.graduated ← False
changchang.name ← "Changchang"
changchang.age ← 19
changchang.graduated ← True
And we can continue into programming:
IF chunhua.age > changchang.name THEN
OUTPUT chunhua.name,"is older than",changchang.name
END IF
You may have thought ahead and decided that we could have a type called Student as above and then declare an array of Student records:
DECLARE students : ARRAY[1:10] OF Student
Reminder: in pseudocode, the lowerbound of an array is often 1. The upper bound can be what you need it to be!
Can you visualise the above array? Take a moment to think about it. What does each element in the array store?
Now, let's imagine many records have been added to the array. Let's perform a linear search for a specific name in the array:
FOR i ← 1 TO 10
IF students[i].name = "Ying Yue" THEN
foundIndex ← i
END IF
END FOR
IF foundIndex<>-1 THEN
OUTPUT "That name is in the array of records at position",foundIndex
ELSE
OUTPUT "Name not found"
Do you think you could rewrite this algorithm to implement a more efficient linear search?!