y
9618 As COMPUTER SCIENCE REVIEW
NOTES
m
Adam Li
2025 10 23
de
CHAPTER 10 DATA TYPE AND STRUCTURE
10.1 DATA TYPES AND RECORDS
a
(a) Select and use appropriate data types for a problem solution,including
integer, real, char, string, Boolean, date (pseudocode will use the fol-
Ac
lowing data types: INTEGER, REAL, CHAR, STRING, BOOLEAN,
DATE, ARRAY, FILE)
• Data Type
A data type defines the domain of values that a variable can
take and the operations that can be performed on those values.
Data type
n
integer real , boolean, string, char, date, array, file.
Domain of values and Oper-
sio
ations
Domain of values: Integer → ; Boolean →
True False ; Character → ’A’ ’@’ ’9’
Operations: Integer → ; String → con-
catenation ; Boolean → AND OR NOT
Vi
• Data type Selection
– A school stores information about students in a database.
Each student has the following attributes:
1
∗ StudentID (e.g., ”S1024”)
∗ FirstName (e.g., ”Amina”)
∗ DateOfBirth (e.g., 2007-03-12)
y
∗ AverageGrade (e.g., 82.5)
∗ IsEnrolled (e.g., TRUE or FALSE)
m
– A program is used to store the exam marks for 30 students.
Each mark is an integer between 0 and 100.
– A company saves its daily sales records permanently. Each
record includes the date, salesperson ID, and amount of sale.
de
The program reads these records the next day to generate a
sales report.
– A library system stores information about books, including
the title, author, ISBN, and a Boolean value indicating whether
a
the book is currently on loan.
Ac
(b) Show understanding of the purpose of a record structure to hold a set
of data of different data types under one identifier, Write pseudocode
to define a record structure. Write pseudocode to read data from a
record structure and save data to a record structure
Benefits of Array:
1. Elements in an array can be accessed or modified directly using
an index.
n
[Link] can be used to efficiently process all elements in the ar-
[Link] for tasks like searching or sorting.
[Link] elements can be stored using a single identifier. This
sio
helps simplify code and improves readability.
//Separate Variables (Not Efficient)
DECLARE Mark1, Mark2, Mark3, Mark4 : INTEGER
DECLARE Total : INTEGER
Vi
Mark1 ← 70
Mark2 ← 85
Mark3 ← 90
Mark4 ← 65
© 2025 Vision Academy
Total ← Mark1 + Mark2 + Mark3 + Mark4
OUTPUT "Average mark is ", Total / 4
y
m
// Using an Array (Efficient)
DECLARE Marks : ARRAY[1:4] OF INTEGER
DECLARE Total, Index : INTEGER
Marks[1] ← 70
de
Marks[2] ← 85
Marks[3] ← 90
Marks[4] ← 65
Total ← 0
a
FOR Index ← 1 TO 4
Total ← Total + Marks[Index]
ENDFOR
Ac
OUTPUT "Average mark is ", Total / 4
• Define a Recrod
TYPE <Typename>
DECLARE <identifier> : <data type>
DECLARE <identifier> : <data type>
n
DECLARE <identifier> : <data type>
::
::
sio
ENDTYPE
• Example of a Record
TYPE StudentRecord
Vi
DECLARE Name : STRING
DECLARE ID : STRING
DECLARE Mark : INTEGER
ENDTYPE
© 2025 Vision Academy
DECLARE Student1 : StudentRecord
DECLARE Students : ARRAY[1:4] OF StudentRecord
y
Students[1].Name ← "Ali"
Students[1].ID ← "S1001"
Students[1].Mark ← 85
m
Benefits of recrod:
1. A record allows multiple related items to be stored under a
single identifier
de
2. It can store different data types (e.g. string, integer, Boolean)
within one entity.
3. It makes the program easier to write, understand, and maintain
a
TYPE StudentRecord
DECLARE Name : STRING
Ac
DECLARE ID : STRING
DECLARE Mark : INTEGER
ENDTYPE
DECLARE Students : ARRAY[1:N] OF StudentRecord
DECLARE Total : INTEGER
DECLARE Index : INTEGER
n
Total ← 0
FOR Index ← 1 TO N
sio
Total ← Total + Students[Index].Mark
ENDFOR
OUTPUT "Average mark is ", Total / N
10.2 ARRAYS
Vi
(a) Use the technical terms associated with arrays Select a suitable data
structure (1D or 2D array) to use for a given task; Including index,
upper and lower bound
© 2025 Vision Academy
(b) Write pseudocode for 1D and 2D arrays
• 2D Array Example :
A school stores the exam marks of 5 students in 3 different sub-
y
jects.
A 2D array allows you to store multiple rows (students) and mul-
m
tiple columns (subjects) efficiently. Each element can be accessed
using two indices: one for the student and one for the subject.
DECLARE Marks : ARRAY[1:5, 1:3] OF INTEGER
de
DECLARE StudentIndex, SubjectIndex : INTEGER
// Example of assigning a mark
Marks[2, 3] ← 85 // Student 2, Subject 3
a
// Calculate total marks for each student
FOR StudentIndex ← 1 TO 5
DECLARE Total : INTEGER
Ac
Total ← 0
FOR SubjectIndex ← 1 TO 3
Total ← Total + Marks[StudentIndex, SubjectIndex]
ENDFOR
OUTPUT "Total marks for Student ", StudentIndex, "
is ", Total
ENDFOR
// Lower bound: 1 (for both rows and columns in this
n
// example)
// upper bound: 5 for students (rows), 3 for subjects
// (columns)
sio
// Indexing: Marks[StudentIndex, SubjectIndex] to
// access a specific mark
// 2D array: It matches the table structure:
// multiple rows of related columns
(c) Write pseudocode to process array data, Sort using a bubble sort
Vi
Search using a linear search
• Linear search
Search for a target value in a 1D array using linear search.
© 2025 Vision Academy
// Linear Search on a 1D Array
DECLARE List : ARRAY[1:5] OF INTEGER
y
DECLARE Target, Index : INTEGER
DECLARE Found : BOOLEAN
m
// Example array values
List[1] ← 34
List[2] ← 18
List[3] ← 92
List[4] ← 56
de
List[5] ← 77
Target ← 92
Index ← 1
Found ← FALSE
a
WHILE Index � 5 AND NOT Found
IF List[Index] = Target THEN
Ac
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found THEN
OUTPUT "Value found at position ", Index
n
ELSE
OUTPUT "Value not found in the array."
ENDIF
sio
• 2D array Linear search
DECLARE Marks : ARRAY[1:5, 1:3] OF INTEGER
DECLARE Target, Row, Col : INTEGER
DECLARE Found : BOOLEAN
Vi
// Example data (you can assume it's already filled)
Marks[1,1] ← 45
Marks[1,2] ← 60
Marks[1,3] ← 70
© 2025 Vision Academy
Marks[2,1] ← 88
Marks[2,2] ← 55
Marks[2,3] ← 76
y
// ... and so on up to Marks[5,3]
Target ← 76
m
Row ← 1
Found ← FALSE
WHILE Row � 5 AND NOT Found
de
Col ← 1
WHILE Col � 3 AND NOT Found
IF Marks[Row, Col] = Target THEN
Found ← TRUE
ELSE
Col ← Col + 1
a ENDIF
ENDWHILE
IF NOT Found THEN
Ac
Row ← Row + 1
ENDIF
ENDWHILE
IF Found THEN
OUTPUT "Value found at Row ", Row, ", Column ", Col
ELSE
OUTPUT "Value not found in the 2D array."
n
ENDIF
• Bubble sort
sio
A teacher has recorded the test marks of 5 students. These marks
are stored in a 1D array called Marks.
The teacher wants to sort the marks in ascending order so they
can give awards to the top-performing students.
Vi
// (Not Efficient)
DECLARE List : ARRAY[1:5] OF INTEGER
DECLARE Index, Pass, Temp : INTEGER
© 2025 Vision Academy
// Sample data
List[1] ← 34
List[2] ← 12
y
List[3] ← 56
List[4] ← 22
List[5] ← 10
m
// Bubble Sort - 5 elements → up to 4 passes
FOR Pass ← 1 TO 4
FOR Index ← 1 TO 4
de
IF List[Index] > List[Index + 1] THEN
// Swap values
Temp ← List[Index]
List[Index] ← List[Index + 1]
List[Index + 1] ← Temp
ENDIF
a
ENDFOR
ENDFOR
Ac
// Output sorted list
FOR Index ← 1 TO 5
OUTPUT List[Index]
ENDFOR
– It always performs 4 full passes, even if the list becomes sorted
earlier.
n
• Efficient Bubble Sort 1D Array (Pseudocode)
sio
DECLARE Marks : ARRAY[1:5] OF INTEGER
DECLARE Swapped : BOOLEAN
DECLARE Index, EndIndex, Temp : INTEGER
// Sample values
Marks[1] ← 34
Marks[2] ← 12
Vi
Marks[3] ← 56
Marks[4] ← 22
Marks[5] ← 10
EndIndex ← 5
© 2025 Vision Academy
Swapped ← TRUE
WHILE Swapped = TRUE
y
Swapped ← FALSE
FOR Index ← 1 TO EndIndex - 1
IF Marks[Index] > Marks[Index + 1] THEN
m
// Swap the elements
Temp ← Marks[Index]
Marks[Index] ← Marks[Index + 1]
Marks[Index + 1] ← Temp
de
Swapped ← TRUE
ENDIF
ENDFOR
EndIndex ← EndIndex - 1
ENDWHILE
a
// Output sorted array
FOR Index ← 1 TO 5
OUTPUT Marks[Index]
Ac
ENDFOR
– Uses a Swapped flag: loop stops early if no swaps are made
(i.e., array is sorted)
– Shrinks the unsorted range each time: avoids comparing already-
sorted values at the end
• Pseudocode: Sort Students by Mark (2D Array)
n
DECLARE Students : ARRAY[1:5, 1:2] OF STRING
sio
DECLARE Swapped : BOOLEAN
DECLARE TempID : STRING
DECLARE TempMark : STRING
DECLARE Index, EndIndex : INTEGER
// Example data: [StudentID, Mark as STRING]
Students[1,1] ← "S001" // ID
Vi
Students[1,2] ← "78" // Mark
Students[2,1] ← "S002"
Students[2,2] ← "55"
Students[3,1] ← "S003"
Students[3,2] ← "90"
© 2025 Vision Academy
Students[4,1] ← "S004"
Students[4,2] ← "66"
Students[5,1] ← "S005"
y
Students[5,2] ← "72"
Swapped ← TRUE
m
EndIndex ← 5
WHILE Swapped = TRUE
Swapped ← FALSE
de
FOR Index ← 1 TO EndIndex - 1
IF Students[Index,2] > Students[Index+1,2] THEN
// Swap IDs
TempID ← Students[Index,1]
Students[Index,1] ← Students[Index+1,1]
Students[Index+1,1] ← TempID
a // Swap Marks
TempMark ← Students[Index,2]
Ac
Students[Index,2] ← Students[Index+1,2]
Students[Index+1,2] ← TempMark
Swapped ← TRUE
ENDIF
ENDFOR
EndIndex ← EndIndex - 1
ENDWHILE
n
// Output sorted list
FOR Index ← 1 TO 5
sio
OUTPUT "ID: ", Students[Index,1], " Mark: ",
Students[Index,2]
ENDFOR
Vi
© 2025 Vision Academy