0% found this document useful (0 votes)
32 views58 pages

12 Trace - Iteration

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

12 Trace - Iteration

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

1 Fetch Decode Execute

What are algorithms and pseudo code?

Hardware & Processing


2 CPU Components &
Registers

3 CPU Performance
& Assessment

Lesson intention:

Communication, Networks &


4 Search Engines

5 Festival & House style

• Preform an algorithm trace and use flowgorithm as a problem solving tool and

IT
repeat instructions (iteration) 6 Produce & Navigate

Assessment & 7 Binary digits

Representation
Grade Descriptors:

Data & Data


8 Large Quantities

2-3 I know what an algorithm is and I can express simple algorithms using symbols.
2-3 I know that computers need precise instructions.
2-3 I can show care and precision to avoid errors 9 Secret code & Assessment
3-4 I know that algorithms are implemented on digital devices as programs.
3-4 I can design simple algorithms using loops, and selection i.e. if statements.
3-4 I can use logical reasoning to predict outcomes. 10 Flowcharts & Pseudocode
3-4 I can find and correct errors i.e. debugging, in algorithms.
4-5 I can design solutions (algorithms) that use repetition and two-way selection i.e. if, then and else. 11 Algorithms - Making

Algorithms
4-5 I can use flowcharts to express solutions. Decisions
4-5 I can use logical reasoning to predict outputs, showing an awareness of inputs.
5-6 I can design solutions by decomposing a problem and creates a sub-solution for each of these parts (decomposition). 12 Trace & Iteration
5-6 I know that different solutions exist for the same problem.
6-7 I know that iteration is the repetition of a process such as a loop. 13 Flowgorithm & Assessment
6-7 I know that different algorithms exist for the same problem.
6-7 I can represent solutions using pseudocode
6-7 I can identify similarities and differences in situations and can use these to solve problems (pattern recognition). 14 Sequencing, Variables &
7-8 I know a recursive solution to a problem repeatedly applies the same solution. Compatibility
7-8 I know the notion of performance for algorithms and I know that some algorithms have different performance characteristics for the same task.
8-9 I know that the design of an algorithm is distinct from its expression in a programming language.

ing & Development


15 Mu Images & Lists
8-9 I know where information can be filtered out in generalizing problem solutions (abstraction).
8-9 I can use logical reasoning to explain how an algorithm works. 16 Calculator
Excep - Ican design a solution to a problem that depends on solutions to smaller instances of the same problem (recursion
17 Procedures & Functions &
Assessment
Key Questions
Key Words

ting a lgorithms is a
es
Do you think that t time?
waste of
What is re
petition?
What is an algorithm trace?
w c h a r t s a nd
Wh a t d o f lo
i n c o m m o n?
o de h a v e
What is differe pseudoc
nt about the
decision symb
ol?
is a fo r l o o p?
What

Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration) 2
Flo the hedgehog likes to play a game with her friend Sue.
You may have played this game yourself. This is how it goes:
• Flo thinks of a number between 1 and 100.
• Sue then tries to guess what the number is.
• If Sue’s guess is too low, then Flo will answer: “too low, guess again”.
(No surprises there!)
• If Sue’s guess is too high, then Flo will answer: “too high, guess again”.
• If Sue’s guess is correct, then Flo will answer: “that’s right!”
• Then Sue thinks of a number between 1 and 100 and Flo has to guess.

Let’s see how this works…


OK so I’ve thought of a number…

Is it 50?

Too low, try again.

Is it 75?

Too high, try again.

Is it 63?

That’s right! Your turn Sue…


Now you know Start
the rules, your
challenge is to Person 1: Pick a number between 1 and 100
create a
flowchart that Person 2: Make a guess
describes the
High-Low
Hedgehogs Is guess to
game. low?
Complete it!

Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration)
Answer
Here’s a better answer. Why is this a better flowchart?

Start

Person 1: Pick a number between 1 and 100

Person 2: Make a guess

Yes
Is guess too low? Too low – try again.

No
Yes
Is guess too high? Too high – try again.
No
That’s right!

Stop
Algorithm Tracing
Sometimes it’s useful to do an Algorithm Trace. This means stepping through the
algorithm (in this case our flowchart) one step at a time.
Let’s imagine that Person 1 picks the number 26….

Start

Person 1: Picks the number 26


Algorithm Tracing
Person 2 then guesses 12…

Start

Person 1: Picks the number 26

Person 2: Makes a guess of 12


Algorithm Tracing
12 is lower than 26, so the result is ‘Too low – try again.’
Follow the flowlines of the flowchart, and Person 2 has another guess…

Start

Person 1: Picks the number 26

Person 2: Makes a guess of 12

Yes
Is guess too low? Too low – try again.

No
Algorithm Tracing
Person 2 now makes a guess of 34.
34 is higher than 26 so this time the result is ‘Too high – try again.’

Start

Person 1: Picks the number 26

Person 2: Makes a guess of 34

Yes
Is guess too low?

Yes
Is guess too high? Too high – try again.
Algorithm Tracing
Finally, Person 2 gets lucky and guesses 26.
26 is not lower or higher than 26 so the result is ‘That’s right!’ and the game
is over.

Start

Person 1: Picks the number 26

Person 2: Makes a guess of 26

Yes
Is guess too low?
No
Yes
Is guess too high? Too high – try again.
No
That’s right!

Stop
Inputs and Outputs
We can improve our flowchart even further by using inputs and outputs.

Keywords
An input is information (data) that we put into an algorithm.
An output is information that we get out of the algorithm.

Here’s the flowchart symbol for an input or output:

This shape is a
Input / Output parallelogram.
Inputs and Outputs
Let’s add these input and output boxes to
Start our High-Low Hedgehog flowchart:

Person 1: Picks a number from 1 to 100

Person 2: Makes a guess

Yes
Is guess too low? Too low – try again.
No
Yes
Is guess too high? Too high – try again.
No
That’s right!
This doesn’t look
much different!
Stop
Flowgorithm
• Time to put our skills and knowledge into action.

• Flowgorithm is an excellent resource


that can help us to design flowcharts.

• What makes Flowgorithm particularly


useful is that we can use it to test our
flowcharts.

• It will even turn flowcharts into


pseudocode.
Flowgorithm

We’ll start by creating together


this flowchart in
Flowgorithm:
Flowgorithm
Step one:

Click on the flowline


between the Main and End
terminators.

This will open a menu as shown


here.

In the menu, click on the Declare


box.
Flowgorithm
Step two:

You should be able to see your


new Declare box.

Double-click on the Declare box


to open this Declare Properties
window:

Under Variable Name: type in the


word age as shown here.

Then press OK.


Flowgorithm
Step three:

Here we can see age. In our


flowchart, age is a variable. We’ll
find out what variables are next
lesson.

Next, click on the flowline


underneath age.

This will open the menu. In the


menu click on the Input box.
Flowgorithm
Step four:

Your new Input box will magically appear


underneath age:

Double-click on the Input box.


Flowgorithm
Step four:

The Input Properties window will open like this:

Once again, type in the word


age in this box.

Then click the OK button.


Flowgorithm
Step five:

Here is our new Input box.

Next, click on the flowline underneath


the Input box:
Flowgorithm
Step five:

The menu will open again:

Click on the If box. Remember: this


is a flowchart decision box.
Flowgorithm
Step six:

This is what our flowchart now looks like:

We’re nearly there!

Click on the False flowline as shown here:


Flowgorithm
Step six:

From the menu:

Click on the Output box:


Flowgorithm
Step six:

Double-click on our new


Output box:
Flowgorithm
Step six:

The Output Properties


window will open.

In this box type the words: “You are


too young to vote.” Note: make sure
to include the “quotation marks”.

Click on OK.
Flowgorithm
Step seven:

Now do the same for the True flowline.

On this side enter the text “You can


vote.”
Flowgorithm
Step eight:

Finally …
Double-click on the If box:

Then in the
If Properties window type:

age > 18

Click OK.
Add a screenshot of your flowchart here.

Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration)
Source Code

Click on the Tools menu at the


top of the Flowgorithm window:

Then click on Source Code Viewer…


Source Code
The Source Code Viewer will convert
flowcharts into a range of different
programming languages.

Click here and try changing the language.

For pseudocode, select the Gaddis


Pseudocode option.

This is a very useful feature. If you choose


to study Computer Science I can understand this!
at GCSE level and beyond, this feature
will save you hours of time and effort.
Change the language to Python and add a screenshot here.

Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration)
Testing Flowcharts

Converting flowcharts into pseudocode


and other programming languages is a
very useful feature of Flowgorithm.

Flowgorithm can also help us with


testing our flowcharts.

Let’s find out how to test and run our


Voting Age flowchart…
Testing Flowcharts
Click on the green triangular Play button at the top of the Flowgorithm window:

Click on the green triangular Play button


at the top of the Flowgorithm window:

This will open the Console window.

The Console asks us to enter a value for


age:
Testing Flowcharts
Enter your age into the box as shown here:

Click the Enter button. Watch what happens …


Testing Flowcharts

Flowgorithm now runs your flowchart.

Here’s the age you entered:

Here’s the output:

Was the output what you expected?


Testing Flowcharts

Click the green Play button again to run


your flowchart.

Try entering a different age this time.

What was the output this time?

Test your flowchart with different ages.


Testing Flowcharts
Time to put our skills and knowledge into action.
Help!
Flo needs your help once again!

Flo needs to eat at least 10 yummy snails every day to


stay healthy. She has started to make a Flowgorithm
flowchart to help her work out if she has eaten
enough snails or not.

She has almost finished her flowchart but she doesn’t


know what to put inside the decision box.

In Flowgorithm, copy her flowchart on the next slide


and then complete the If decision box. Test and run
your flowchart afterwards.

Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration)
Flo needs your help once again!

Flo needs to eat at least 10 yummy snails every day to


stay healthy. She has started to make a Flowgorithm
flowchart to help her work out if she has eaten
enough snails or not.

She has almost finished her flowchart but she doesn’t


know what to put inside the decision box.

In Flowgorithm, copy the flowchart and then


complete the If decision box. Test and run your
flowchart afterwards. Screenshot opposite.
Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration)
Let’s Bring It All Together
Keywords
Stepping through an algorithm one step at a time and looking at inputs,
values and outputs is called an Algorithm Trace or Dry Running.

Keywords
An input is information (data) that we put into an algorithm.
An output is information that we get out of the algorithm.

Pause for Thought


Do you think that testing algorithms is a waste of time?
A huge amount of public money has been spent on computer
systems that have not been tested properly and then failed.
A quick search on the internet will show you many such situations.
This is YOUR money that has been wasted!
Do you still think that testing is a waste of time?
In the “Hedgehog Run, Hedgehog Go” rap the sequence goes like
this:

Verse 1

Hedgehog Harmonics Verse 1


Verse 2
Chorus
Chorus
Verse 1
Verse 1
Chorus
Chorus
Chorus
Chorus
Verse One: Lights are red, amber and green, but
Hedgehogs are small and sometimes not seen.
Always take care when driving at night, We could simplify the
Make sure you’ve got superb eyesight! sequence like this:

Verse Two: Those cars they come at such a rate, Verse 1 x 2


When you just wanna cross to see your flatmate. Verse 2
It doesn’t matter how many prickles are on your back, Chorus x 2
Those wheels will give you more than a whack! Verse 1 x 2
Chorus x 4
Chorus: Look left, look right,
Cross that road on the green light.
Hedgehog run, hedgehog go,
Don’t get squished for going too slow.

Lesson intention: Preform an algorithm trace and use flowgorithm as a problem solving tool and repeat instructions (iteration)
Iteration

Start off by clicking on the


first flowline.

This will open the menu.

Click once on the Declare box


Iteration

Double-click on the Declare box.

This will open the Declare


Properties.

Type in the variable name:


counter as shown here.

Click OK.
Iteration

Click on the flowline here.

This will open the menu.

Click on the For box.


Iteration

Here we have our first For box.

We use For boxes to repeat instructions.


(Remember: repetition is called iteration)

Double-click on the For box.


Iteration
The next step is to carefully enter some values in the For Properties:

Type in the variable name: counter


in this box.

In the Start Value box enter: 1

In the End Value box enter: 2

Then click OK.


Iteration
Well done, we have just made our first
For loop.

This For loop will repeat things twice.

We can think of this For loop as a simple


counter. It counts from 1 to 2, and then stops.

If we wanted to repeat things 4 times,


all we’d need to do is change
counter = 1 to 2
to
counter = 1 to 4

How could we repeat things 3 times?


Iteration

Answer: to repeat things 3 times,


we’d need to change
counter = 1 to 2
to
counter = 1 to 3

Click on the flowline here…


Iteration

…and then click on the Output box.


Iteration

Remember: our For loop repeats things


for us. (This is called iteration.)

In this flowchart, the For loop will


repeat this Output 2 times.

Double-click on this Output box.


Iteration
In the Output Properties box,
type in the words of your Verse 1.

Don’t forget to add “quotes” at the


start and end.

Click OK.
Iteration

Here’s our Verse 1.

Click on the Run button to see what


happens!
Iteration
Your output should look something like this. Just as we
wanted: verse 1 is iterated (repeated) twice.

Add a screenshot of your flowchart here!


Music Machine
Your next challenge is to add more For loops to complete
your song.

This is what we are aiming for:

Verse 1 repeated twice (we can also say: iterated twice)


Verse 2 once Add a screenshot of your flowchart here!
Chorus repeated twice
Verse 1 repeated twice
Chorus repeated four times (or: iterated four times)

We’ve iterated verse 1 already.


So next we’ll add verse 2, which only runs once.
Then after that the chorus needs iterating twice, and so on.
Music Machine
Finally, run your flowchart to see the whole song. Does it
match mine?:

Add a screenshot of your flowchart here!


Pseudocode
Flowcharts can become quite long and
difficult to follow.

Sometimes it is easier to look at the


pseudocode instead.

The pseudocode for our


For loop here is:

FOR counter = 1 TO 2
OUTPUT “Lights are red ..”
END FOR

Let’s look at the pseudocode for our


complete Music Machine…
Pseudocode
Which version do you prefer, flowchart or
pseudocode?

DECLARE Integer counter

FOR counter = 1 TO 2
OUTPUT “Lights are red, amber ..."
END FOR
OUTPUT “Those cars they come at such ..."
FOR counter = 1 TO 2
OUTPUT "Look left, look right ..."
END FOR
FOR counter = 1 TO 2
OUTPUT " Lights are red, amber ..."
END FOR
FOR counter = 1 TO 4
OUTPUT "Look left, look right ..."
END FOR
Let’s Bring It All Together
Key Term
Iteration is the process of repeating one or more instructions over
and over again.

Key Term
A For loop is used to iterate one or more instructions for a
fixed or known number of times.

Pause for Thought


Electronic music has been around since the 1950s.
Most contemporary popular music and recording rely on
computing technology. Do you think that computers will one day
be able to create and compose music that would rival
Mozart, Beethoven and Justin Bieber?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy