Coding and Algorithms

Lesson

Writing computer programs (coding) can be a process of trial and error. It is quite common for programmers to make errors when they are writing their code. These mistakes are known as 'bugs' and they stop the computer program (code) from doing what it is supposed to.

An important part of programming is testing your program and 'debugging' (which means removing or fixing the bugs).

Here is an algorithm which lists the instructions for making breakfast

- Take a bowl out of cupboard, a spoon out of the drawer and place them on the table
- Pour cereal and milk into the bowl
- Place the bowl in the sink
- Eat the cereal out of the bowl

Question: What is the bug in this algorithm?

Think: Imagine you are making breakfast in the morning and follow each step of the Algorithm. Can you find the error?

Do: Lines 3 and 4 are in the wrong order.

1. Try to identify where the program or code is not working

2. Go through this section of your code step, by step thinking about what each command does, writing down the result of each step if possible

3. If you are still having difficulty finding the bug, take a break and look at it again, or ask a friend to look at your code.

The list of numbers `4, 9, 8, 1, 6`

is to be sorted using the selection sort algorithm.

Enter the output after each line of the selection sort algorithm.

Step Number Algorithm Step `OUTPUT`

$1$1 Find the smallest number.

Swap it with the first number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

$2$2 Find the second-smallest number.

Swap it with the second number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

$3$3 Find the third-smallest number.

Swap it with the third number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

$4$4 Find the fourth-smallest number.

Swap it with the fourth number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

A bug is an error in the algorithm which results in the algorithm not producing the desired output.

The algorithm below contains a bug. What is the output of each line of the algorithm?

Step Number Algorithm Step `OUTPUT`

$1$1 Find the smallest number.

Swap it with the first number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

$2$2 Find the second-smallest number.

Swap it with the first number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

$3$3 Find the third-smallest number.

Swap it with the third number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

$4$4 Find the fourth-smallest number.

Swap it with the fourth number.`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,`

Which step contains the bug?

Step 1

AStep 3

BStep 4

CStep 2

DStep 1

AStep 3

BStep 4

CStep 2

D

The linear search algorithm will find the position of the number `6`

in the list of five numbers `9, 4, 6, 7, 3`

.

Each step of the algorithm is listed below.

Step Number Algorithm Step `OUTPUT`

1 `Value`

= 62 If first number in list = `Value`

then print`1`

3 If second number in list = `Value`

then print`2`

4 If third number in list = `Value`

then print`3`

5 If fourth number in list = `Value`

then print`4`

6 If fifth number in list = `Value`

then print`5`

Which step of the algorithm produces an output?

Step 3

AStep 1

BStep 2

CStep 5

DStep 4

EStep 6

FStep 3

AStep 1

BStep 2

CStep 5

DStep 4

EStep 6

FWhat is the output of this algorithm?

`OUTPUT: $\editable{}$`

A bug is an error in the algorithm which results in the algorithm not producing the desired output.

The algorithm below contains a bug.

Step Number Algorithm Step `OUTPUT`

1 Number to be found = `Value`

2 If `Value`

= first number in list then print`1`

3 If `Value`

= second number in list then print`2`

4 If `Value`

= third number in list then print`3`

5 If `Value`

= fourth number in list then print`4`

6 If `Value`

= third number in list then print`5`

Which steps of the algorithm produce an output? Select all that apply.

Step 1

AStep 5

BStep 2

CStep 4

DStep 6

EStep 3

FStep 1

AStep 5

BStep 2

CStep 4

DStep 6

EStep 3

FWhat are the outputs of these two steps of the algorithm?

`OUTPUT: $\editable{},\editable{}$,`

Which step contains the bug?

Step 6

AStep 5

BStep 3

CStep 2

DStep 1

EStep 4

FStep 6

AStep 5

BStep 3

CStep 2

DStep 1

EStep 4

F

Look at the pseudo code below, which sorts a list of five numbers from smallest to largest.

```
1 START
2 SET list = [4, 9, 7, 3, 6]
3 SET a = 0
4 FOR n FROM 1 TO 4
5 a = list element at position n
6 FOR m FROM (n + 1) TO 5
7 IF list element at position m < a
8 a = list element at position m
9 END IF
10 END FOR
11 SWAP a WITH list element at position n
12 PRINT list
13 END FOR
14 END
```

```
OUTPUT:
3, 9, 7, 4, 6
3, 4, 7, 9, 6
3, 4, 6, 9, 7
3, 4, 6, 7, 9
```

The pseudo code was changed and gave the following outputs:

`INPUT: 4, 9, 7, 3, 6 OUTPUT: 3, 9, 7, 3, 6 3, 3, 7, 3, 6 3, 3, 3, 3, 6 3, 3, 3, 3, 6`

Which line most likely contains the bug?

Line 7

ALine 11

BLine 8

CLine 5

DLine 7

ALine 11

BLine 8

CLine 5

DWhich of the following replacement lines would have resulted in this output?

`list element at position n = a`

A`a = list element at position m`

B`a = list element at position n`

C`list element at position m = a`

D`list element at position n = a`

A`a = list element at position m`

B`a = list element at position n`

C`list element at position m = a`

DThe pseudo code was changed and gave the following outputs:

`INPUT: 4, 9, 7, 3, 6 OUTPUT: 3, 9, 7, 4, 6 3, 4, 7, 9, 6 3, 4, 7, 9, 6 3, 4, 7, 9, 6`

Which line most likely contains the bug?

Line 4

ALine 5

BLine 11

CLine 6

DLine 4

ALine 5

BLine 11

CLine 6

DWhich of the following replacement lines would have resulted in this output?

`FOR n FROM (m + 1) TO 5`

A`FOR m FROM n TO 5`

B`FOR m FROM (n + 1) TO 4`

C`FOR n FROM m TO 4`

D`FOR n FROM (m + 1) TO 5`

A`FOR m FROM n TO 5`

B`FOR m FROM (n + 1) TO 4`

C`FOR n FROM m TO 4`

D