Animation of Algorithm Goal To understand an algorithm

  • Slides: 35
Download presentation
Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:

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: o sequential operations/statements o conditional statements, o iterative statements, © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS (UIT 2201: Algorithms) Page 1

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Let’s animate the execution of this

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Let’s animate the execution of this simple algorithm. ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish Finish: print out the value of sum © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS (UIT 2201: Algorithms) Page 2

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Initial state of the algorithm ALGORITHM

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Initial state of the algorithm ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k ? ? ? sum ? ? ? Step 0. CPU Finish: print out the value of sum © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Our abstract model of the computer (UIT 2201: Algorithms) Page 3

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Start of execution, at Step 1.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Start of execution, at Step 1. ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k sum ? ? ? 0 Step 1. Sum 0; CPU Finish: print out the value of sum Leong. HW, So. C, NUS Assignment statement; The value of 0 is stored in the (UIT 2201: Algorithms) Page 4 © Leong Hon Wai, 2003 -2008 storage box called sum.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 2. ALGORITHM Sum-1 -to-5;

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 2. ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 1 sum 0 Step 2. k 1; CPU Finish: print out the value of sum Leong. HW, So. C, NUS Assignment statement; The value of 1 is stored in the (UIT 2201: Algorithms) © Leong Hon Wai, 2003 -2008 storage box called k. Page 5

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. start of “body-of-loop”

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. start of “body-of-loop” ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 1 sum 1 Step 3. sum + k =0+1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Assignment statement; The new value of sum is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 6

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. inside “body-of-loop” ALGORITHM

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. inside “body-of-loop” ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 2 sum 1 Step 4. k+1 =1+1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Assignment statement; The new value of k is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 7

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. loop-test ALGORITHM Sum-1

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. loop-test ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 2 sum 1 Step 5. (k > 5)? (2 > 5)? = NO CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 5)? Algorithms) 8 © Leong Hon Wai, 2003 -2008 NO (UIT 2201: execute Step Page 6 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. ALGORITHM Sum-1 -to-5;

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 2 sum 1 Step 6. goto repeat (Step 3) CPU Finish: print out the value of sum Leong. HW, So. C, NUS goto “repeat” means to get algorithm to continue at (UIT 2201: Algorithms) Page 9 © Leong Hon Wai, 2003 -2008 the step labelled “repeat”.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 2 nd round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 2 nd round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 2 sum 3 Step 3. sum + k =1+2 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Add 2 to sum; The new value of sum is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 10

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 2 nd round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 2 nd round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 3 sum 3 Step 4. k+1 =2+1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Increment k; The new value of k is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 11

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 2 nd loop-test

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 2 nd loop-test ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 3 sum 3 Step 5. (k > 5)? (3 > 5)? = NO CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 5)? Algorithms) 12 © Leong Hon Wai, 2003 -2008 NO (UIT 2201: execute Step Page 6 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 2 nd round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 2 nd round ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 3 sum 3 Step 6. goto repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Goto Step 3 and Execute the loop-body again. (UIT 2201: Algorithms) Page 13

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 3 rd round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 3 rd round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 3 sum 6 Step 3. sum + k =3+3 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Add 3 to sum; The new value of sum is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 14

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 3 rd round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 3 rd round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 4 sum 6 Step 4. k+1 =3+1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Increment k; The new value of k is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 15

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 3 rd loop-test

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 3 rd loop-test ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 4 sum 6 Step 5. (k > 5)? (4 > 5)? = NO CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 5)? Algorithms) 16 © Leong Hon Wai, 2003 -2008 NO (UIT 2201: execute Step Page 6 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 3 rd round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 3 rd round ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 4 sum 6 Step 6. goto repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Goto Step 3 and Execute the loop-body again. (UIT 2201: Algorithms) Page 17

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 4 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 4 th round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 4 sum 10 Step 3. sum + k =6+4 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Add 4 to sum; The new value of sum is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 18

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 4 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 4 th round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 5 sum 10 Step 4. k+1 =4+1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Increment k; The new value of k is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 19

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 4 th loop-test

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 4 th loop-test ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 5 sum 10 Step 5. (k > 5)? (5 > 5)? = NO CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 5)? Algorithms) 20 © Leong Hon Wai, 2003 -2008 NO (UIT 2201: execute Step Page 6 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 4 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 4 th round ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 5 sum 10 Step 6. goto repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Goto Step 3 and Execute the loop-body again. (UIT 2201: Algorithms) Page 21

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 5 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 5 th round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 5 sum 15 Step 3. sum + k = 10 + 5 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Add 5 to sum; The new value of sum is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 22

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 5 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 5 th round of loop-body ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 6 sum 15 Step 4. k+1 =5+1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Increment k; The new value of k is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 23

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 5 th loop-test

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 5 th loop-test ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 6 sum 15 Step 5. (k > 5)? (6 > 5)? = YES CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 5)? Algorithms) 24 © Leong Hon Wai, 2003 -2008 YES(UIT 2201: execute Step. Page 7 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the iterative loop ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish: print out the value of sum k 6 sum 15 CPU © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Step 7. goto finish (Step 8) Goto finish (Step 8) (exit(UIT 2201: the iterative loop!) Algorithms) Page 25

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 8. print output and

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 8. print output and END ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish: print out the value of sum k 6 sum 15 Step 8. Print to output CPU Output of Algorithm: 15 Leong. HW, So. C, NUS © Leong Hon Wai, 2003 -2008 Print statement; print(UIT 2201: to output the value of sum Algorithms) Page 26

Summary v Summary of Steps: o 1, 2, (3, 4, 5, 6), (3, 4,

Summary v Summary of Steps: o 1, 2, (3, 4, 5, 6), (3, 4, 5, 7), 8 v. Note the sequential execution, except for o Conditional statements o Goto statements o iterative statements v. Questions: o Where is the “loop-body”? o How many iteration of the loop-body? o How many times is the loop-test done? © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS (UIT 2201: Algorithms) Page 27

Explore further (DIY) v. We did Sum 1 -to-5 (instead of Sum 1 -to-100)

Explore further (DIY) v. We did Sum 1 -to-5 (instead of Sum 1 -to-100) v. DIY: Simulate the execution for the original algorithm for Sum 1 -to-100? v(Use the following “ending”-slides to help you. ) © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS (UIT 2201: Algorithms) Page 28

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 99 th loop-test

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 99 th loop-test ALGORITHM Sum-1 -to-100; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no goto repeat 7. yes goto finish k 100 sum 5050 Step 5. (k > 100)? (100 > 100)? = NO CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 100)? Algorithms) 29 © Leong Hon Wai, 2003 -2008 NO (UIT 2201: execute Step Page 6 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 99 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 6. 99 th round ALGORITHM Sum-1 -to-5; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no goto repeat 7. yes goto finish k 100 sum 4950 Step 6. goto repeat (Step 3) CPU Finish: print out the value of sum © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Goto Step 3 and Execute the loop-body again. (UIT 2201: Algorithms) Page 30

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 100 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 3. 100 th round of loop-body ALGORITHM Sum-1 -to-100; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no goto repeat 7. yes goto finish k 100 sum 5050 Step 3. sum + k = 4950 + 100 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Add 100 to sum; The new value of sum is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 31

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 100 th round

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 4. 100 th round of loop-body ALGORITHM Sum-1 -to-100; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no goto repeat 7. yes goto finish k 101 sum 5050 Step 4. k+1 = 100 + 1 CPU Finish: print out the value of sum Leong. HW, So. C, NUS Increment k; The new value of k is stored; (UIT 2201: © Leong Hon Wai, 2003 -2008 the old value. Algorithms) is gone. Page 32

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 100 th loop-test

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 5. 100 th loop-test ALGORITHM Sum-1 -to-100; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no goto repeat 7. yes goto finish k 101 sum 5050 Step 5. (k > 100)? (101 > 100)? = YES CPU Finish: print out the value of sum Leong. HW, So. C, NUS Condition check: evaluate (k > 100)? Algorithms) 33 © Leong Hon Wai, 2003 -2008 YES(UIT 2201: execute Step. Page 7 next.

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the

Simulating an Algorithm 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Executing Step 7. & exit the iterative loop ALGORITHM Sum-1 -to-100; 1. sum 0 2. k 1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no goto repeat 7. yes goto finish: print out the value of sum k 101 sum 5050 CPU © Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS Step 7. goto finish (Step 8) Goto finish (Step 8) (exit(UIT 2201: the iterative loop!) Algorithms) Page 34

© Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS (UIT 2201: Algorithms)

© Leong Hon Wai, 2003 -2008 Leong. HW, So. C, NUS (UIT 2201: Algorithms) Page 35