You are on page 1of 3

#cs/10

## 10.2 Arrays

The upper and lower bounds of an array refer to the smallest/highest number index
of an array dimension.

### Write pseudocode to process array data

**Linear search**
Checking each element of an array in turn for a required value, O(n).
```
DECLARE MyList: ARRAY[0:6] OF INTEGER
MyList <- LoadedList (of same size)

Index <- -1
Found <- FALSE
MaxIndex <- 6

INPUT SearchValue

REPEAT
Index <- Index + 1
IF MyList[Index] = SearchValue THEN
Found <- TRUE
ENDIF
UNTIL FOUND = TRUE OR Index = MaxIndex

IF Found = TRUE THEN


OUTPUT "value found at index:", Index
ELSE
OUTPUT "value not found"
ENDIF
```

**Bubble sort**
A sort method where adjacent values are compared and swapped. This code will sort
them to ascending order.
```
DECLARE UnsortedList: ARRAY[0:6] OF INTEGER
UnsortedList <- LoadedList (of same size) //unsorted integers

MaxIndex <- 6
n <- MaxIndex - 1

REPEAT
NoMoreSwaps <- TRUE
FOR i <- 0 TO n
IF UnsortedList[i] > UnsortedList[i+1] THEN
Temp <- UnsortedList[i]
UnsortedList[i] <- UnsortedList[i+1]
UnsortedList[i+1] <- Temp
NoMoreSwaps <- FALSE
ENDIF
NEXT i
UNTIL NoMoreSwaps = TRUE

```
## 10.3 Files

**Write pseudocode to handle text files that consist of one or more lines**
- Files are needed for data to be stored permanently.
```
//reading from one file and writing to another (copying)
OPENFILE "one.txt" FOR READ
OPENFILE "two.txt" FOR WRITE

WHILE NOT EOF("one.txt") DO


TempLine <- ""
READFILE "one.txt", TempLine
WRITEFILE "two.txt", TempLine
ENDWHILE

CLOSEFILE "one.txt"
CLOSEFILE "two.txt"
```

## 10.4 Abstract Data Types (ADT)

An ADT is a collection of data with associated operations.

| | Stack | Queue | Linked list |


| ---- | ---- | ---- | ---- |
| **Description** | **FILO** data structure. Think of a Pringle's can, the first
chip put in will be the last eaten. | **FIFO** data structure | A linked list is a
sequential data structure where elements are **not stored contiguously** in memory.
**Each element contains a reference** to the next element in the sequence. |
| **Diagram** | ![[Pasted image 20240204174923.png]] | ![[Pasted image
20240204174933.png]] | ![[Pasted image 20240204165142.png]] |
| **Operations** | - **Push (insert)**: item is added to the top of the stack.
<br>- **Pop (delete)**: item is removed from the top of the stack and returned to
the program for further processing.<br>- If the stack is full/already empty,
depending on the operation an error is raised. | - **Insert**<br>- **Delete** | -
**Insert**<br>- **Delete** |
| **Uses** | Backtracking Algorithms is an example usage (.e.g removing last action
of a program to return to a previous stage) | A buffer (for bit streaming) |
Implementation of a stack or queue. Also useful because to find a place of
insertion you have an O(N) search, but to do an insertion if you already have the
correct position it is O(1). A linked list allows you to grow and shrink a list of
elements without having to reallocate the rest of the list. |
| **How to implement with arrays** | - Lower bound 1 and upper bound N<br>- A
pointer indicates the position of the item that is currently at the 'TOS' position
| - Lower bound 1 and upper bound N<br>- The queue is controlled by 2
pointers.<br>- A 'head' pointer points to the item that is currently at the head of
the queue.<br>- A 'tail' pointer points to the item that is currently at the rear
of the queue.<br>- A new item is always added to the end of the queue (tail + 1
position) | |
| **Pseudocode implementation** | <pre><br>DECLARE Stack:ARRAY[0:6] OF
CHAR<br><br>TOS <- -1 (Top of stack pointer)<br><br>//insert (push) (before check
if stack not full)<br>//increment the stack pointer 'TOS'<br>TOS <- TOS +
1<br>Stack[TOS] = NewItem<br><br>//delete (pop) (before check if stack is not
empty)<br>//output the item at position 'top of stack'<br>//decrement the stack
pointer 'TOS'<br>TOS <- TOS - 1<br></pre> | | |

You might also like