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

Slides:



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

ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
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.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms (more examples…)  Supplementary Notes: 1.For your reference… (esp. those new to programming)
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:
Repeating Actions While and For Loops
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Chapter 2: Algorithm Discovery and Design
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Chapter 2 The Algorithmic Foundations of Computer Science
Loops – While, Do, For Repetition Statements Introduction to Arrays
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.
C++ for Engineers and Scientists Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Loops Doing the same thing over and over and over and over and over and over…
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, Animation of SQL Queries To illustrate three SQL queries: –Q1: simple select (one.
Chapter 12: How Long Can This Go On?
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
LeongHW, SoC, NUS (UIT2201: Networks) Page 1© Leong Hon Wai, (MACP) Medium Access Control Protocol.
Control Structures RepetitionorIterationorLooping Part I.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Introduction to Computers and Programming Lecture 7:
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Introduction To Flowcharting
Repetition-Counter control Loop
Topics Introduction to Repetition Structures
Control Structure Senior Lecturer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
One-Dimensional Array Introduction Lesson xx
Chapter 8 Arrays Objectives
Loops CIS 40 – Introduction to Programming in Python
Algorithm Discovery and Design
Faculty of Computer Science & Information System
Chapter 8 Arrays Objectives
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm: Array-Sum (find sum of n numbers) Observe carefully: sequential instruction conditional statements, repetitive statements,

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum Let’s animate the execution of this algorithm on the input: Input to Algo. Array-Sum: n = 6 Array A = [2, 5, 10, 3, 12, 24]

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 Model of Computer: Initial State We assume that the values of n and the array A[1..6] has been read into the computer. We assume that the values of n and the array A[1..6] has been read into the computer. A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF ? Sum ? k ? Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 Start of execution… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF ? Sum ? k ? Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 executing… A[1] A[2] A[3] A[4] A[5] A[6] n 6 Sum_SF  0; CPU Sum_SF 0 Sum ? k ? Assignment statement; The value of 0 is stored in the storage box called Sum_sf. Assignment statement; The value of 0 is stored in the storage box called Sum_sf. Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 executing… A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  1; CPU Sum_SF 0 Sum ? k 1 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. Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 executing beginning of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 0 Sum ? k 1 Conditional: (k <= n) Reads value of k and n; Check outcome of (k <= n), Condition is True, execute loop; Value of k, n are unchanged. Conditional: (k <= n) Reads value of k and n; Check outcome of (k <= n), Condition is True, execute loop; Value of k, n are unchanged. Is (k <= n)? Is (1 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 1) (A[k] = 2) RHS = = 2 CPU Sum_SF 0 Sum ? k 1 Calculate value of expression on the right-hand-side; Note: In A[k], k is used as an index (subscript) to the array A Calculate value of expression on the right-hand-side; Note: In A[k], k is used as an index (subscript) to the array A Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 1) (A[k] = 2) RHS = = 2 Sum_SF  2 CPU Sum_SF 2 Sum ? k 1 Calculate value of expression on the right-hand-side; Assign result to variable on left; Calculate value of expression on the right-hand-side; Assign result to variable on left; Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  k + 1; k  1 + 1; CPU Sum_SF 2 Sum ? k 1 This increments value of k; 2 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 End of body of loop, loop back... A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 2 Sum ? k 2 Reached end of the loop body. Must loop back and re-test the loop condition. (We will skip this in future loops) Reached end of the loop body. Must loop back and re-test the loop condition. (We will skip this in future loops) Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 12 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 2 Sum ? k 2 Testing the loop condition again with new value of k. (This is call a pre-test loop, where testing is done before entering the loop.) Testing the loop condition again with new value of k. (This is call a pre-test loop, where testing is done before entering the loop.) Is (k <= n)? Is (2 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 13 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 2) (A[k] = 5) RHS = = 7 CPU Sum_SF 2 Sum ? k 2 Notice that A[k] now gets the second number in the list; Notice that A[k] now gets the second number in the list; Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 14 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 2) (A[k] = 5) RHS = = 7 Sum_sf  7 CPU Sum_SF 7 Sum ? k 2 Notice that A[k] now gets the second number in the list; This number (5) is added to Sum_SF Notice that A[k] now gets the second number in the list; This number (5) is added to Sum_SF Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 15 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  k + 1; k  2 + 1; CPU Sum_SF 7 Sum ? k 2 This increments value of k; 3 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 16 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 7 Sum ? k 3 Testing the loop condition again with new value of k. Testing the loop condition again with new value of k. Is (3 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 17 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 3) (A[k] = 10) Rhs = Sum_SF  17 CPU Sum_SF 7 Sum ? k 3 Now A[k] gets the third element Add A[3] to Sum_sf; Now A[k] gets the third element Add A[3] to Sum_sf; 17 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 18 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  k + 1; k  3 + 1; CPU Sum_SF 17 Sum ? k 3 increment k; 4 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 19 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 17 Sum ? k 4 Testing the while loop condition again with new value of k. Testing the while loop condition again with new value of k. Is (4 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 20 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 4) (A[k] = 3) Rhs = Sum_SF  20 CPU Sum_SF 17 Sum ? k 4 Add A[4] to Sum_SF; 20 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 21 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  4 + 1; CPU Sum_SF 20 Sum ? k 4 increment k; 5 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 22 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 20 Sum ? k 5 Testing the while loop condition again with new value of k. Testing the while loop condition again with new value of k. Is (5 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 23 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 5) (A[k] = 12) Rhs = Sum_SF  32 CPU Sum_SF 20 Sum ? k 5 Add A[5] to Sum_SF; 32 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 24 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  5 + 1; CPU Sum_SF 32 Sum ? k 5 increment k; 6 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 25 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 32 Sum ? k 6 Testing the while loop condition again with new value of k. Testing the while loop condition again with new value of k. Is (6 <= 6)? True Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 26 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 (k = 6) (A[k] = 24) Rhs = Sum_SF  56 CPU Sum_SF 32 Sum ? k 6 Add A[6] to Sum_SF; 56 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 inside body of loop A[1] A[2] A[3] A[4] A[5] A[6] n 6 k  6 + 1; CPU Sum_SF 56 Sum ? k 6 increment k; 7 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 Test loop condition again… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 56 Sum ? k 7 The loop condition fails (is false); Skip the body of loop. Go to the statement after the while loop; The loop condition fails (is false); Skip the body of loop. Go to the statement after the while loop; Is (7 <= 6)? False Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 29 Jump out of loop, to next stmt… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 56 Sum 56 k 7 Copy value of Sum_SF to Sum Sum  Sum_SF Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 30 Print statement… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_SF 56 Sum 56 k 7 Output of Algrithm Sum: Sum is 56 Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 31 End of Algorithm… A[1] A[2] A[3] A[4] A[5] A[6] n 6 CPU Sum_sf 56 Sum 56 k 7 Output of Algrithm Sum: Sum is 56 End of the Algorithm Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 32 Your Homework: On your own, execute algorithm Sum on this input. Input to Algorithm Sum: n = 4 Array A = [4, 7, 3, 9, 2, 20, 10] Array-Sum(A, n); begin Sum_SF  0; k  1; while (k <= n) do Sum_SF  Sum_SF + A[k]; k  k + 1; endwhile Sum  Sum_SF; Print “Sum is”, Sum; end; Algorithm Array-Sum

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 33 Summary Review: A. Did you understand the following concepts: 1. variables (storage boxes) 2. arrays (a collection of contiguous variables) 3. array index or subscript B. Can you follow the execution of sequential instruction, the loop pre-test, repetitive statements?

© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 34 Self Assessment Self Test 1: (based on the example) 1. How many times was the loop executed? 2. How many times was the pre-loop test executed? 3. What is the value of k at the end of the algorithm? Self Test 2: (for a general value of n) 1. How many times was the loop executed? 2. How many times was the pre-loop test executed? 3. What is the value of k at the end of the algorithm? Self Test 3: (Modifying the algorithm slightly to …) 1. compute the average of the n numbers in array A. 2. the “sum-of-squares” of the n numbers.