0% found this document useful (0 votes)
27 views11 pages

Section 10 - Data Types and Structures - 10.1.1 To 10.4.3 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

Section 10 - Data Types and Structures - 10.1.1 To 10.4.3 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Section 10 - Data Types and Structures

10.1.1 - Data Types

What is the importance of data types?


Data types allow programming languages to provide classifications for items of data, so that
they can be used for different purposes

The data types:

Data Type Definition Pseudocode Python

Boolean Logical values, True (1) and False (2) BOOLEAN bool

Char Single alphanumeric character CHAR not used

Date Value to represent a date DATE class datetime

Integer Whole number, positive or negative INTEGER int

Real Positive or negative number with a decimal point REAL float

String Sequence of alphanumeric characters STRING str

Declaring data types:


Before data can be used, the type needs to be decided. It is done by declaring the data type for
each item to be used.
- The data item is identified by a unique name called an identifier

Declaring data type in pseudocode:


● Format: DECLARE <identifier> : <data type>
● Example: DECLARE myBirthday : DATE

10.1.2 - Records

Composite Data Type: a data type constructed using several of the basic data types available in
a programming language

Record: it is a composite data type formed by the inclusion of several related item of different
data types

Note: a record contains a fixed number of items


Record data type in pseudocode:

Format Example

TYPE TYPE
<Typename> TbookRecord
DECLARE <identifier> : <data type> DECLARE title : STRING
DECLARE <identifier> : <data type> DECLARE author : STRING
DECLARE <identifier> : <data type> DECLARE publisher : STRING
ENDTYPE DECLARE noPages : INTEGER
DECLARE fiction : BOOLEAN
ENDTYPE

10.2.1 - 1D Arrays

Array: it is a data structure containing several elements of the same data type. The elements
can be accessed using the same identifier name

- Lower Bound: the index of the first element in an array


- Upper Bound: the index of the last element in an array

What is a 1D Array?
It can be referred to as a list

Declaring 1D Array in pseudocode:


● Format: DECLARE <identifier> : ARRAY [LB:UB] OF <data type>
● Example: DECLARE myList : ARRAY [0:8] OF INTEGER
● Process: myList[7] ← 16

10.2.2 - 2D Arrays

What is a 2D Array?
It can be referred to as a table, with rows and columns

Declaring 2D Arrays in pseudocode:


The lower bound for rows (LBR) and upper bound for rows (UBR), the lower bound for columns
(LBC) and upper bound for columns (UBC) and data type is included.
● Format: DECLARE <identifier> : ARRAY [LBR:UBR , LBC:UBC] OF <data type>
● Example: DECLARE myArray : ARRAY [0:8 , 0:2] OF INTEGER
● Process: myArray [7,0] ← 16
10.2.3 - Using a Linear Search

Linear Search: it is a method for finding items stored in an array. Each element of the array is
checked from lower to upper bound until the item is found.

Example of Linear Search:


The search algorithm to find if an item is in the populated 1D array myList could be written in
pseudocode as:

Algorithm Explained:

- The variables upper and lower bound is used to


adapt for different lengths of a list

- A repeat-until loop is used so that the algorithm is


efficient
10.2.4 - Using a bubble sort

Bubble Sort: a method of sorting data in an array into alphabetical/numerical order by


comparing items and swapping them if they’re in the wrong order

Example of Bubble Sort:


The bubble sort algorithm to sort the populated 1D array myList could be written in pseudocode
as:
10.3 - Files

Computer programs store data in a file. Every file is identified by its filename

Handling text files in Pseudocode

Pseudocode for opening a file: OPEN <file identifier> FOR <file mode>

3 modes for opening files:


● READ - reads data from the file
● WRITE - writes data to the file and can overwrite existing data
● APPEND - adds data to the end of the file

Pseudocode for READ mode: READFILE <file identifier> , <variable>


Pseudocode for WRITE/APPEND mode: WRITEFILE <file identifier> , <variable>

Note: variable must be of data type string


The EOF Function:
This function is used to test for the end of a file. The TRUE
value is returned if the end of the file is reached, or FALSE
otherwise.

- Pseudocode for EOF function: EOF (<file identifier>)

Pseudocode for closing a file: CLOSEFILE <file identifier>

Example pseudocode for editing a text file →

10.4.0 - Abstract Data Types (ADTs)

What is an Abstract Data Type (ADT)?


It is a collection of data and a set of operations on that data. There are three ADT’s, stack,
queue and linked list

- Uses of Stacks: memory management, expression evaluation, backtracking in recursion


- Uses of Queues: management of files sent to a printer, buffers used in keyboards,
scheduling
- Linked Lists: using arrays to implement a stack, queue and binary tree

What is a Stack, Queue and Linked List?

Stack Queue Linked List

It is a list containing items It is a list containing items it is a list containing items


operating on the last in, first out operating on the first in, first out where each item in the list
(LIFO) principle. (FIFO) principle. points to the next item in the list.

The first item added to a stack is The first item added to a queue is A new item is always added to
the last item removed the first item removed the start of the list

Note:
- Items can be added to the stack (push) and removed (pop)
- Items can be added to the queue (enqueue) and removed (dequeue)
Use of Pointers:
Stacks, queues and linked lists make use of pointers to manage their operations. Items in
stacks and queues are added at the end. Linked lists use an ordering algorithm to order them in
ascending or descending order.

Pointers used by a Stack Pointers used by a Queue

Front Pointer: points to the first item in the stack Front Pointer: points to the first item in the queue

Top Pointer: points to the last item in the stack Rear Pointer: points to the last item in the queue

The pointers are equal when there’s only one item The pointers are equal where there’s only one
in the stack item in the queue

Pointers used by a Linked List:


A start pointer points to the first item in the linked list. Every item is stored together with a pointer to
the next item. This is a node. The last item in the linked list has a null pointer.
10.4.1 - Stack Operations

The value of the base pointer remains the same during stack operations. A stack can be
implemented using an array and a set of pointers

Pseudocode for Stack Operations:


10.4.2 - Queue Operations

The value of the frontPointer changes after


dequeue, but the value of the rearPointer
changes after enqueue.

How is a queue used?


A queue is implemented using an array and a
set of pointers. The queue should be managed
as a circular queue to avoid moving the position
of items in an array every time an item is
removed.

Pseudocode for Queue Operations:


10.4.3 - Linked List Operations

A linked list is implemented using two 1D arrays:


- One for items in the linked list,
- And another for the pointers to the next item in the list, and a set of pointers

Items can be removed from any position in the linked list, the empty positions in the array must
be managed as a empty linked list, called the heap

For Example

The startPointer = –1, as the list has no elements. The heap is set up as a linked list ready for
use.

The startPointer is set to the element pointed to by the heapPointer where 37 is inserted. The
heapPointer is set to point to the next element in the heap by using the value stored in the
element with the same index in the pointer list. Since this is also the last element in the list the
node pointer for it is reset to –1.
The startPointer is changed to the heapPointer and 45 is stored in the element indexed by the
heapPointer. The node pointer for this element is set to the old startPointer. The node pointer for
the heapPointer is reset to point to the next element in the heap by using the value stored in the
element with the same index in the pointer list.

You might also like