video

Lesson video

In progress...

Loading...

Hi, I'm Kashif, your computer science teacher for the algorithms unit.

In this lesson, we're going to be looking at tracing algorithms. For this lesson, you're going to need a pen, some paper, and you're going to need to remove any distractions that are get in your way of focusing.

Once you've done that, let's begin.

What is the output? What will be the output when the flowchart is executed? Is it four, 10, 15, or will there be an error? Is it four, 10, 15, or will there be an error? Write your answers down.

Okay, let's see how you got on.

So, we're going to work through this algorithm.

So at the moment the algorithm starts, we've got the rectangle there, we looked at the shapes last lesson.

So the count is initialised at one, and the total is initialised at zero.

The process assigns one to the variable count, assigns zero to the variable total, and at the moment there's no output.

Then we're onto the diamond, and we're checking if the condition is true.

We're adding the value of count to the value held by total, so now count is one, total is one.

We're adding one to the value held by count.

So now count is two and total is one.

Now we're looping round again to check if the condition is true.

So the condition here is: is count less than five, and yes it is.

So we're going to go around once more.

So we're adding the value of count to the value held by total once again, and then adding one to the value held by count.

Now we've looped round again.

Is, is the condition true? Yes, count is three, it is less than five, let's go around again.

We're adding the value of count to the value held by total.

So now count is three, total is six.

We're adding one to the value held by count.

So count is four, total is six, and we've looped around again.

At the moment, the number for count is four, and it is less than five.

So the process here, check if the condition is true, it is true, let's go round once more.

We're adding the value of count to the value held by total.

So now we've got count is four, total is 10.

We're adding one to the value held by count.

So now count is five, total is 10, and now we're checking the condition.

This time, five is not less five, so therefore the loop terminates and the instruction from the false brunch is executed and the output is 10.

So how did you get on? Did you get 10? If not, don't worry, this lesson is all about walking through algorithms and figuring out what the variables are at certain stages.

In this lesson you will, use a trace table to walk through code that contains a while loop, a for loop, and the list of items, and use a trace table to detect, and correct errors in a programme.

Using a trace table.

Walking through an algorithm and checking the state of the variables is a useful tool for understanding how the algorithm works.

It is also great for detecting and correcting errors.

To help us with this, you can use a trace table.

On the right hand side we have an example of a trace table there.

So just like when we had different iterations where we were going through that loop numerous times and the count kept increasing, you can see the count increasing there.

You can see what the total is and if the condition is true or false.

So this is the trace table that's linked to the algorithm that we've just, or the flow chart that we've just gone through.

A trace table allows you to formally record the state of variables, the outputs and the condition evaluations as you mentally execute the algorithm.

Trace table example.

So on the left-hand side, we have some code.

So print "Enter a number", num equals int input, while num is more than two or greater than two, if num, now the symbol there is mod, which we'll talk about in a little bit.

So, if num mod three equals equals zero print num, okay, So, if num mod three equals equals zero print num, okay, and then we've got num equals num minus one.

A learner has written a Python programme that should output all the numbers between zero and the input value that are divisible by three.

You want to test that the code executes without any logical errors.

Here is how a trace table could help you check the programme.

Here's a blank trace table.

We can identify the variables and conditions and add them to the headings to help keep track of the algorithm.

So we can see there we've got line, the variable num, the condition, and the output.

Then walk through the code.

The first line has no variables, conditions, or output.

So we don't add any, anything to the table.

On line two, the programme asks the user for a value.

We will choose the value six, which will then be assigned to num, to the num variable, so we record this on the table.

So we can see line two, num six.

Line three has a condition, num greater than two.

The condition is evaluated and we record the value on the table.

Okay, so is num greater than two? Yes it is, because six is greater than two.

So therefore, the condition there is true.

Line four has a condition, num percentage three equals equals zero.

This uses the modulo or mod, to calculate the remainder of num divided by three.

So once we divide the number, mod gives us the remainder.

So the whole, the whole number we have and then we've got the remainder after it.

So for example, we've got six, six divided by three is two.

Now we've got no remainder after that, so the remainder will be zero.

And here, yes, it does equal to zero.

Therefore, we equate the condition to true.

In this case, six mod three will calculate as zero, so the condition evaluates to true.

As the condition for line four was true, we step into the if statement.

Line five produces an output, so we record it on the table.

Line six has a variable assignment.

The expression, num minus one retrieves the current value of num and reduces it by one.

This new value then replaces the value of num.

As we have reached the end of the while block, we go back and check the condition again.

Line three's condition is still true, so we record this.

Line four uses modulo or mod to calculate the remainder of num divided by three.

In this case, five mod three will calculate as two, this is because in five, we can only have one set of three within five.

Then we've got two leftover.

So hence why we use two, and hence why this condition is false, because we want to have zero leftover.

So the condition evaluates to false.

As the condition for line four was false, we move onto line six.

Line six has a variable assignment, so we record this.

As we have reached the end of the while block, we go back and check the condition again.

Line three's is condition is still true.

Why? Because we've got the number four that we're dealing with right now.

Is it greater than two? Yes, it is.

So we're going to still keep on going around this loop.

Line three's condition is still true, so we record this.

So as you can see there, is recorded as true.

Now, a question for you, what will the condition on line four evaluate to? Have a moment to think about it.

Okay, what did you get? Let's have a look.

In this case four mod three will calculate as one, so the condition evaluates to false.

Why? Because how many threes, have we got in four? We've got one set of threes, and then what's the remainder? Mod is always the remainder, the whole, the whole number remainder, so we've got one left over.

How did you get on with that? Let's continue.

As the condition for line four was false, we move on to line six.

Line six has a variable assignment, so we record this.

As we have reached the end of the while block, we go back and check the condition again.

Line three's condition is still true.

Why? Because num right now is three, is it greater than two? Yes it is, we're going to keep going around.

So the condition's true and we've recorded it.

In this case, three mod three, will calculate as zero, so the condition evaluates to true.

As the condition for line four was true, we step into the if statement.

So as you can see with this indentation, we only step into line five if the condition is true.

Line five produces an output, so we'll record it on the table.

Line six has a variable assignment, so we record this.

So you can see there line six, num now is equivalent to two.

Now, line three's condition is now false.

Why? Because two is not greater than two.

So we have recorded this and there are no more lines of code after the while block, the programme will now end.

So on the right hand side we have the truth table for the Python programme on the left hand side.

The trace table has shown us that the programme does only output numbers divisible by three based on the given input.

Because, as we can see on the trace table, the right-hand column with output, them numbers are both divisible by three.

If there had been any logic errors, it would have been easy to spot exactly where the problem was when executing the code.

So the main purpose of using trace tables is to spot if we do have any logical errors because they don't get flagged up.

When we have syntax errors, Python, the programme, will, it'll stop it.

It'll say syntax error, or even give you like the line that the problems on.

With logical errors you won't get that prompt from Python, to find them we have to trace the code and we've got to use trace tables to see where that problem might be.

Task one, tracing code.

For the next task, you will be completing a series of trace tables for different scenarios.

Pause the video to complete your task.

Task one, tracing code.

Using the worksheet, complete task one, the three scenarios are Russian multiplication, lowest number in a list, and nested loops.

Resume once you're finished.

Thank you for joining me on this journey.

So I hope you picked up some skills there in terms of tracing the code, in terms of understanding how to trace an algorithm.

Share your work with Oak national.

If you'd like to, please ask a parent or carer to share your work on Instagram, Facebook, or Twitter tagging Oak National and hashtag learn with oak.

Thank you very much for your time today, and hopefully I'll see you on the next lesson.

Goodbye.