10.1 - 2 - 3 Data Types & Structures
10.1 - 2 - 3 Data Types & Structures
A 2020
Data Type: A classification determining the type of value an item of data can take and how it can be used.
Declaring Variables/Identifiers
Variable: A storage location for a value that can change during the execution of a program. It has an identifier.
Before data can be used in pseudocodes and programs, the type needs to be decided. This is done by declaring the data type
for each item to be used.
Pseudocode structure:
Examples:
Sample Pseudocode:
Sum ← 0
FOR Count ← 1 TO 10
PRINT “Enter number: “
INPUT Number
Sum ← Sum + Number
NEXT
Average ← Sum/10
PRINT “The average is: “ , Average
Page 1 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Declaring a constant
Pseudocode structure:
Example:
CONSTANT pi = 3.14
CONSTANT esc = “Exit”
1. Your name
2. The number of children in a class
3. The time taken to run a race
4. Whether a door is open or closed
Records
Record data type is a composite data type comprising several related items that may be of different data types.
Sometimes variables of different data types are a logical group, such as data about a person (name, date of birth, height,
number of siblings, whether they are full-time student). Each data item may be of different data types.
We can declare a record type known as a user-defined type because we can decide which variables to include as a record.
Pseudocode Structure:
TYPE <TypeName>
DECLARE <identifier> : <data type>
DECLARE <identifier> : <data type>
DECLARE <identifier> : <data type>
…
…
ENDTYPE
Pseudocode example
TYPE TPersonRecord
Name : STRING
DateOfBirth : DATE
Height : REAL
NoOfSiblings : INTEGER
IsFullTimeStud : BOOLEAN
ENDTYPE
Page 2 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
The data type, TPersonRecord, is now available and an identifier may be declared as:
Pseudocode Structure:
Example:
We can now assign a value to a field of this Person record using the dot notation
Pseudocode Structure:
Example:
Person.Name ← “Ajmal”
Person.DateOfBirth ← #14/08/2003#
Person.Height ← 1.75
Person.NoOfSiblings ← 2
Person.IsFullTimeStud ← TRUE
Pseudocode Structure:
Example:
OUTPUT Person.Name
Activity: Write the declaration of a record type to store the details of a book: Title, Year of publication,
Price, ISBN.
Write the statements required to assign the values “Computer Science”, 2019, $44.95, “9781108733755”
to the fields respectively.
Page 3 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Arrays
An array is a data structure containing several elements of the same data type.
The elements can be accessed using the same identifier name.
The position of each element in array is identified using the array’s index.
An array index is the row or column number of an individual array element.
The index of the first element in an array is the lower bound.
The index of the last element is the upper bound.
Declaring a 1D array
Pseudocode structure:
Examples:
A specific element in array is accessed using an index value. In pseudocode, this gives:
Examples:
Example of a 1D Array
Index MyList
[0] 27
[1] 19 The lower bound is [0] and the upper bound is [8]. Nine elements are available in the array named
[2] 36 MyList.
[3] 42
[4] 16 MyList[8] refers to the 9th element.
[5] 89
[6] 21
[7] 16
[8] 55
Page 4 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
To access each array element in turn (populate the array), a loop can be used, e.g.
FOR Index ← 0 TO 8
INPUT MyList[Index]
NEXT Index
Initialising an array
FOR Index ← 0 TO 8
MyList[Index] ← “ “ //for a string data type, Null value
NEXT
………………………………………………………………………………………………………………………………………………….
FOR Index ← 0 TO 8
MyList[Index] ← 0 //for an integer data type
NEXT
Activity
1. Define two arrays, one for your friends’ names and one for their ages.
2. Write statements in pseudocode to populate the two arrays that you’ve created.
Declaring a 2D Array
Pseudocode structure:
Rows Columns
Examples:
Page 5 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Example of a 2D Array
MyScores Array
Column Index
[r,0] [r,1] [r,2]
[0,c] 27 31 17 ← Lower Bound Row
[1,c] 19 67 48
[2,c] 36 98 29
Row [3,c] 42 22 95
Index [4,c] 16 35 61
[5,c] 89 46 47
[6,c] 21 71 28
[7,c] 16 23 13
[8,c] 55 11 77 ← Upper Bound Row
↑ ↑
Lower Upper
Bound Bound
Column Column
Example:
MyScores[7,0] ← 16
Structured English:
Page 6 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
To output the contents of a 2D array (all the values in one row on the same line)
Activity
1. Write statements in pseudocode to populate the array MyScores above using FOR…TO…NEXT loop.
Note:
Initialising means declaring the array (its identifier name, subscript range and data type) and also populating each array cell
with a value.
Linear Search: Checking each element of an array in turn for a required value.
Structured English:
Pseudocode:
UpperBound ← 8
LowerBound ← 1
Page 7 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Found ← FALSE
Index ← LowerBound
REPEAT
IF MyList[Index] = SearchItem // OR IF SearchItem = MyList[Index]
THEN
Found ← TRUE
OUTPUT “Item was FOUND at position “ , Index
ELSE
Index ← Index + 1
ENDIF
UNTIL (Found = TRUE) OR (Index > UpperBound)
IF Found = FALSE
THEN
OUTPUT “Item NOT FOUND”
ENDIF
Sorting means arranging data into a specific order, e.g. in ascending or descending order.
One method of sorting is bubble sort.
Bubble sort is a sort method where adjacent pairs of values are compared and swapped. (Smaller values slowly rise
to the top).
Page 8 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Once the first pass is completed, the largest value is in the correct position at the end of the array.
We need to work through the array again and again.
After each pass, the next largest value will be in its correct position.
N ← MaxIndex – 1
REPEAT
NoMoreSwaps ← TRUE
FOR j ← 0 T0 n
IF MyList[j] > MyList[j + 1]
THEN
Temp ← MyList[j]
MyList[j] ← MyList[j + 1]
MyList[j + 1] ← Temp
NoMoreSwaps ← FALSE
ENDIF
NEXT j
N ← n – 1
UNTIL NoMoreSwaps = TRUE
Page 9 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Page 10 of 10