Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Problem Solving

Similar presentations


Presentation on theme: "Topic 1: Problem Solving"— Presentation transcript:

1 Topic 1: Problem Solving
Correcting Algorithms

2 Understanding Algorithms
There may come a point where we don’t create the algorithm ourselves We may be given an algorithm to look at We’ll need to learn how to understand what this algorithm does As well as Work out what the algorithm outputs Test to see if the algorithm is correct Problem Solving: Correcting Algorithms

3 Understanding Algorithms
When it comes to understanding algorithms, knowing the constructs of programming always helps Understanding these constructs, and how they work, are how we can work out What an algorithm is generally doing What is happening to the data in the algorithm The constructs are Sequencing Branching Iteration Problem Solving: Correcting Algorithms

4 Understanding Algorithms
Sequencing The key here is that instructions happen from top to bottom If a variable has a different value placed in it in an instruction That value will be in that variable further down Until a different value is placed in it Example Say we have the following algorithm BEGIN F() x  INPUT(‘Enter a number’) x  x * 2 RETURN x END F First we get a number from the user, and stores it in a variable. The next instruction then doubles the number in the variable. When that variable is returned, we can tell that it will be twice as big as whatever the user types in. Problem Solving: Correcting Algorithms

5 Understanding Algorithms
Branching For this, we need to understand the condition being used As more instructions are only run depending on said condition There is also an important note to make There may be multiple routes to go down We need to be able to spot when we can only go down one If there are lots of ifs on there own We can go down as many as possible If there are lots of else ifs We can only go down one Example BEGIN F() x  INPUT(‘Enter a number’) IF MOD(x, 2) = 0 RETURN TRUE ELSE RETURN FALSE END IF END F We can only go down the IF or the ELSE, not both. This is dependent on the condition of the IF. Can you tell what this algorithm achieves? Problem Solving: Correcting Algorithms

6 Understanding Algorithms
Iteration Here we need to work out how many times the loop happens This is based on The condition of the WHILE loop Or the given range of the FOR loop Both repeat instructions inside them WHILE loops act a bit like Ifs FOR loops require a bit more thought Example BEGIN F() x  INPUT(‘Enter a number’) FOR i FROM 0 TO x OUTPUT(i) END FOR END F Here we need to understand the range of the FOR loop. First the user enters a number (in x). The FOR loop then creates its own variable (i) and makes it go from 0 to the number in x. If the user enters 5, then the range is: (5 – 0) + 1 Why “+ 1”? Because the 5 is inclusive: 0, 1, 2, 3, 4, 5 Problem Solving: Correcting Algorithms

7 Problem Solving: Correcting Algorithms
Can you work out what this algorithm is doing? BEGIN F(x) n  0 WHILE x > 0 IF MOD(x, 2) = 0 OUTPUT(n) x  x - 1 END IF n  n + 1 END WHILE END F Problem Solving: Correcting Algorithms

8 Problem Solving: Correcting Algorithms
Determining Output There may be cases where we’re given an algorithm And we need to work out what the output will be These cases will often give us the values to use as input All we have to do is Work our way through the algorithm (using the input) Make note of any OUTPUT or RETURN statements If OUTPUT is used, then text will be output to the console We will need to state the output in the correct order If RETURN is used, then a value is going to be returned from the function We need to state what that returned value will be Problem Solving: Correcting Algorithms

9 Problem Solving: Correcting Algorithms
Determining Output For example, let’s take the algorithm from earlier Let’s say the input is 10 here Can you work out what the output will be? BEGIN F(x) n  0 WHILE x > 0 IF MOD(x, 2) = 0 OUTPUT(n) x  x - 1 END IF n  n + 1 END WHILE END F Problem Solving: Correcting Algorithms

10 Problem Solving: Correcting Algorithms
Work out what this algorithm is doing Then work out its output with the following inputs TEST 1: x = 10, y = 8 TEST 2: x = 4, y = 12 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Problem Solving: Correcting Algorithms

11 Problem Solving: Correcting Algorithms
Testing Algorithms We may get lost working through an algorithm Looking for its output/return values There is a tool we can employ to help us Called trace tables They are tables that list All the variables used in the algorithm (as columns) Values of the variables every time they change (as rows) The idea is we can read, from top-to-bottom, how the variables change To help us work out what the algorithm does Problem Solving: Correcting Algorithms

12 Problem Solving: Correcting Algorithms
Testing Algorithms Let’s take the algorithm from the exercise as an example We had to see how the values of the variables changed To work out what the output was We can use a trace table to make this job easier BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Problem Solving: Correcting Algorithms

13 Problem Solving: Correcting Algorithms
Testing Algorithms x y z i RETURN 10 8 7 6 5 4 3 2 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Above is what the trace table for this algorithm could look like Using TEST 1 from the previous exercise Note that you can also find trace tables that split rows up by line number (and what happens on them). In that case, the above table would look like this: line x y z i RETURN 1 10 8 2 8 3 8 3 7 3 6 3 5 3 4 3 3 3 2 Problem Solving: Correcting Algorithms

14 Problem Solving: Correcting Algorithms
Testing Algorithms x y z i RETURN 10 8 7 6 5 4 3 2 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F We write the variables in order of them being created State values for them whenever they change Problem Solving: Correcting Algorithms

15 Problem Solving: Correcting Algorithms
Testing Algorithms x y z i RETURN 10 8 7 6 5 4 3 2 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F If an earlier variable changes before one on the right We right it on a new row Problem Solving: Correcting Algorithms

16 Problem Solving: Correcting Algorithms
Create two the trace tables for this algorithm Using the inputs: x = 4, y = 12 and then x = 15, y = 10 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Problem Solving: Correcting Algorithms

17 Problem Solving: Correcting Algorithms
Create a trace table for the following algorithm Using an input of x = 8 BEGIN F(x) n  0 WHILE x > 0 IF MOD(x, 2) = 0 OUTPUT(n) x  x - 1 END IF n  n + 1 END WHILE END F Problem Solving: Correcting Algorithms

18 Correcting Algorithms
We can also use trace tables to help us correct algorithms There may be times when the algorithm we have is incorrect It OUTPUT/RETURNs the wrong value We can use a trace table (with our own input) to check the algorithm Create inputs to use Think about what value we’re expecting See what the OUTPUT/RETURN value is (based off the input) If they are different, then the algorithm is incorrect Problem Solving: Correcting Algorithms

19 Correcting Algorithms
Take this algorithm, for example It sees if a number is a prime number There is a problem with this algorithm Can you spot it? Don’t worry if you can’t We’ll use a trace table to help us work out where the problem is BEGIN IsPrime(x) FOR n FROM 3 TO x IF MOD(x, n) = 0 RETURN FALSE END IF END FOR RETURN TRUE END ISPrime Problem Solving: Correcting Algorithms

20 Correcting Algorithms
Let’s use a trace table with an input of 5 The algorithm gets through the FOR loop and returns true The number is prime Which makes sense (5 is a prime number) x n RETURN 5 3 4 TRUE BEGIN IsPrime(x) FOR n FROM 3 TO x IF MOD(x, n) = 0 RETURN FALSE END IF END FOR RETURN TRUE END ISPrime Problem Solving: Correcting Algorithms

21 Correcting Algorithms
Now let’s try an input of 4 The algorithm returns true Despite 4 not being a prime number Can you see where the problem is? x n RETURN 4 3 TRUE BEGIN IsPrime(x) FOR n FROM 3 TO x IF MOD(x, n) = 0 RETURN FALSE END IF END FOR RETURN TRUE END ISPrime We’re not checking to see if values are divisible by 2! Problem Solving: Correcting Algorithms

22 Problem Solving: Correcting Algorithms
Use a trace table to help you fix the following algorithm The name of the function should give you a hint as to what it does Come up with your own inputs for the trace table tests BEGIN OutputRectangle(width, height) FOR i FROM 0 TO width FOR j FROM 0 TO width OUTPUT(‘0’) END FOR OUTPUT(‘\n’) END OutputRectangle Problem Solving: Correcting Algorithms

23


Download ppt "Topic 1: Problem Solving"

Similar presentations


Ads by Google