LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, 2003-2008 Animation of Algorithm Goal: To understand an algorithm by animating its execution,

Slides:



Advertisements
Similar presentations
IHE Cardiology. IHE Cardiology Page 2 Problem Statement
Advertisements

Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
1-1 Patterns and Inductive Reasoning
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms  Readings: [SG] Ch. 2 & 3  Chapter Outline: 1.Chapter Goals 2.What are Algorithms 1.Real Life.
Hon Wai Leong, NUS (CS6234, Spring 2009) Page 1 Copyright © 2009 by Leong Hon Wai CS6234: Spring 2009 (Overview) CS6234: Advanced Algorithms  Instructors:
Limestone Ingredients. Basins Observations and profile-view drawing What two rocks are in our basin? In nature, what materials are sandstone made of?
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms (more examples…)  Supplementary Notes: 1.For your reference… (esp. those new to programming)
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © by Leong Hon Wai Database – Info Storage and Retrieval  Aim: Understand basics of  Info.
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © by Leong Hon Wai Database – Info Storage and Retrieval  Aim: Understand basics of  Info.
HTML Recall that HTML is static in that it describes how a page is to be displayed, but it doesn’t provide for interaction or animation. A page created.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.
511 Friday Feb Math/Stat 511 R. Sharpley Lecture #15: Computer Simulations of Probabilistic Models.
© 2007 M. Tallman. Find the mean of 7, 4, 5, Step 1: Add the numbers in the data set. Step 2: Divide the sum by the “number of numbers”
Partial Sums An Addition Algorithm.
Partial Sums An Addition Algorithm Add the hundreds ( ) Add the tens (60 +80) 140 Add the ones (8 + 3) Add the place value sums.
Hon Wai Leong, NUS (CS6234, Spring 2009) Page 1 Copyright © 2009 by Leong Hon Wai CS6234 Lecture 1 -- (14-Jan-09) “Introduction”  Combinatorial Optimization.
Hon Wai Leong, NUS (CS5206, Fall 2008) Page L00.1 Copyright © 2008 by Leong Hon Wai Welcome to CS1101 & SOC  Bad news…  CS1101 is hard,  CS1102 is harder.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Artificial Intelligence  Reading Materials:  Ch 14 of [SG]  Also Section Logic.
Use effective written methods to add whole numbers.
LeongHW, SoC, NUS (UTT2201: Introduction) Page 1  Lecture Outline: 1.The Pervasive Computer 2.Examples of Uses 3.Simple and also Difficult 4.Similar and.
LeongHW, SoC-USP, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, UIT2201: Lecture 1  Lecture Outline: 1.The Pervasive Computer 2.Examples.
LeongHW, SoC, NUS (CS Combinatorial and Graph Algorithms) Page 1 About CS5234: Course Overview CS5234: Combinatorial and Graph Algorithms  Level.
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, Animation of SQL Queries To illustrate three SQL queries: –Q1: simple select (one.
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © by Leong Hon Wai Database – Info Storage and Retrieval  Aim: Understand basics of  Info.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
A Multiplication Algorithm
LeongHW, SoC, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, UIT2201: CS & IT Revolution  Course Web-Site: (Spring 2010) 
Warm-Up Complete each statement with. 1. −9 −2 2.−80 −81 Find the value of each expression. 3.− −6 + (−8) 5.− −5 + (−15) + (−10)
science.howstuffworks.com/ animal-camouflage.htm
LeongHW, SoC, NUS (UIT2201: Networks) Page 1© Leong Hon Wai, (MACP) Medium Access Control Protocol.
LeongHW, SoC&USP, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, UIT2201 Project (25%) [Fall 2013]  Project Info is Online  Team size:
© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Hon Wai Leong, NUS (CS5234, 16 Oct 2007) Page L09.1 Copyright © 2007 by Leong Hon Wai UIT2201 Spring 2010 Notices and Reminders.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Artificial Intelligence  Reading Materials:  Ch 14 of [SG]  Also Section Logic.
Source Page US:official&tbm=isch&tbnid=Mli6kxZ3HfiCRM:&imgrefurl=
Ways to Check for Divisibility Dividing by 2 All even numbers are divisible by 2 Even numbers are numbers that end with either 2, 4, 6, 8, or 0.
LeongHW, SoC, NUS (Dec 2000, CORE/TDP  USP) Page 1 Office of Special Programmes, SoC Programmes for Talented Students in the SoC
Arithmetic Sequences. Arithmetic sequence Before talking about arithmetic sequence, in math, a sequence is a set of numbers that follow a pattern. We.
Introduction to the Framework: Unit 1, Key Topic 3http:// 1.
Building on Multiplication With Fractions Focus Question : How does an area model relate to multiplying fractions? Standards : 6.EE.A.3 & 6.NS.A.1 Do Now.
LeongHW, SoC&USP, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, UIT2201: CS & IT Revolution  Assessment:  Participation: 05 %  Tutorials:20.
Путешествуй со мной и узнаешь, где я сегодня побывал.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Recursive Algorithm.
$100 $200 $300 $400 $100 $200 $300 $400 $300 $200 $100 Adding up to thousands Add up to millionsAdd & balance equations Ways to makes numbers Add up.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.
LeongHW, SoC, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, © Leong Hon Wai, UIT2201: Lecture 1  Lecture Outline: 1.The Pervasive.
$100 $200 $300 $400 $100 $200 $300 $400 $300 $200 $100 Adding doublesAdd 3 numbersAdd 1 to 2 digit numbers Ways to makes numbers Add & find missing.
Inductive and Deductive Reasoning
Algebra 1 Section 10.1 Add and subtract polynomials
Java's for Statement.
Partial Products Algorithm for Multiplication
Page 1. Page 2 Page 3 Page 4 Page 5 Page 6 Page 7.
Learn about the multiples of fractions
Exploring Repeating Decimals
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Design and Implementation
Computer Programming.
Programming: Simple Control Structures
Objective - To add and subtract decimals.
READY?.
Standard Deviation How many Pets?.
Unit 3 Week 4.
Comparing Embryology.
Ways to Check for Divisibility
Properties of Addition and Multiplication
EQ: How can you add detail to your writing?
Hundred Dollar Questions
Similarities Differences

Presentation transcript:

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm: Sum 1-to-5 (find sum from 1 to 5) (Note: Similar to Sum 1-to-100, but shorter!!) Observe carefully: osequential operations/statements oconditional statements, oiterative statements,

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Lets animate the execution of this simple algorithm.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k ??? Step 0. CPU sum ??? Our abstract model of the computer Initial state of the algorithm

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k ??? Step 1. Sum 0; CPU sum 0 Start of execution, at Step 1. Assignment statement; The value of 0 is stored in the storage box called sum. Assignment statement; The value of 0 is stored in the storage box called sum.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 1 Step 2. k 1; CPU sum 0 Assignment statement; The value of 1 is stored in the storage box called k. Assignment statement; The value of 1 is stored in the storage box called k. Executing Step 2.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 1 Step 3. sum + k = CPU sum 1 Assignment statement; The new value of sum is stored; the old value is gone. Assignment statement; The new value of sum is stored; the old value is gone. Executing Step 3. start of body-of-loop

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 4. k + 1 = CPU sum 1 Assignment statement; The new value of k is stored; the old value is gone. Assignment statement; The new value of k is stored; the old value is gone. Executing Step 4. inside body-of-loop

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 5. (k <= 5)? (2 <= 5)? = TRUE CPU sum 1 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. loop-test

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 6. Goto Repeat (Step 3) CPU sum 1 goto Repeat means to get algorithm to continue at the step labelled repeat. goto Repeat means to get algorithm to continue at the step labelled repeat. Executing Step 6.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 3. sum + k = CPU sum 3 Add 2 to sum; The new value of sum is stored; the old value is gone. Add 2 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 2 nd round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 3 Step 4. k + 1 = CPU sum 3 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 2 nd round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 12 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 3 Step 5. (k <= 5)? (3 <= 5)? = TRUE CPU sum 3 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. 2 nd loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 13 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 3 Step 6. Goto Repeat (Step 3) CPU sum 3 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 2 nd round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 14 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 3 Step 3. sum + k = CPU sum 6 Add 3 to sum; The new value of sum is stored; the old value is gone. Add 3 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 3 rd round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 15 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 4 Step 4. k + 1 = CPU sum 6 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 3 rd round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 16 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 4 Step 5. (k <= 5)? (4 <= 5)? = TRUE CPU sum 6 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. 3 rd loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 17 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 4 Step 6. Goto Repeat (Step 3) CPU sum 6 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 3 rd round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 18 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 4 Step 3. sum + k = CPU sum 10 Add 4 to sum; The new value of sum is stored; the old value is gone. Add 4 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 4 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 19 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 5 Step 4. k + 1 = CPU sum 10 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 4 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 20 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 5 Step 5. (k <= 5)? (5 <= 5)? = TRUE CPU sum 10 Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Condition check: evaluate (k <= 5)? TRUE execute Step 6 next. Executing Step 5. 4 th loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 21 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 5 Step 6. goto repeat (Step 3) CPU sum 10 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 4 th round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 22 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 5 Step 3. sum + k = CPU sum 15 Add 5 to sum; The new value of sum is stored; the old value is gone. Add 5 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 5 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 23 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 6 Step 4. k + 1 = CPU sum 15 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 5 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 24 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 6 Step 5. (k <= 5)? (6 <= 5)? = FALSE CPU sum 15 Condition check: evaluate (k <= 5)? FALSE execute Step 7 next. Condition check: evaluate (k <= 5)? FALSE execute Step 7 next. Executing Step 5. 5 th loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 25 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish finish: print out the value of sum k 6 Step 7. goto finish (Step 8) CPU sum 15 Goto finish (Step 8) (exit the iterative loop!) Goto finish (Step 8) (exit the iterative loop!) Executing Step 7. & exit the iterative loop 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 26 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 5) (* means < or = *) 6. then Goto Step 3. Repeat 7. else Goto Finish finish: print out the value of sum k 6 Step 8. Print to output CPU sum 15 Print statement; print to output the value of sum Print statement; print to output the value of sum Executing Step 8. print output and END 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Output of Algorithm: 15

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 © Leong Hon Wai, Summary Summary of Steps: o1, 2, (3,4,5,6), (3,4,5,6), (3,4,5,6), (3,4,5,6), (3,4,5,7), 8 Note the sequential execution, except for oConditional statements oGoto statements oiterative statements Questions: oWhere is the loop-body? oHow many iteration of the loop-body? oHow many times is the loop-test done?

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 © Leong Hon Wai, Explore further (DIY) We did Sum 1-to-5 (instead of Sum 1-to-100) DIY: Simulate the execution for the original algorithm for Sum 1-to-100? (Use the following ending-slides to help you.)

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 29 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 100 Step 5. (k <= 100)? (100 <= 100)? = TRUE CPU sum 5050 Condition check: evaluate (k <= 100)? TRUE execute Step 6 next. Condition check: evaluate (k <= 100)? TRUE execute Step 6 next. Executing Step th loop-test

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 30 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum k 100 Step 6. Goto Repeat (Step 3) CPU sum 4950 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step th round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 31 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 100 Step 3. sum + k = CPU sum 5050 Add 100 to sum; The new value of sum is stored; the old value is gone. Add 100 to sum; The new value of sum is stored; the old value is gone. Executing Step th round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 32 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 4. k + 1 = CPU sum 5050 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step th round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 33 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 5. (k <= 100)? (101 <= 100)? = FALSE CPU sum 5050 Condition check: evaluate (k <= 100)? FALSE execute Step 7 next. Condition check: evaluate (k <= 100)? FALSE execute Step 7 next. Executing Step th loop-test

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 34 © Leong Hon Wai, Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum 0 2. k 1 3. Repeat: add k to sum 4. add 1 to k 5. if (k <= 100) 6. then Goto Step 3. Repeat 7. else Goto Finish finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 7. Goto finish (Step 8) CPU sum 5050 Goto finish (Step 8) (exit the iterative loop!) Goto finish (Step 8) (exit the iterative loop!) Executing Step 7. & exit the iterative loop

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 35 © Leong Hon Wai,