0% found this document useful (0 votes)
50 views

Algorithms & Flowcharting Ii

Here is an algorithm and flowchart to print the multiplication table for 6: Algorithm: 1. Set counter = 1 2. Print counter "x 6 = " counter * 6 3. Increment counter by 1 4. If counter is less than or equal to 12, go to step 2 5. Stop Flowchart: START Set counter = 1 Print counter "x 6 = " counter * 6 Increment counter by 1 counter <= 12? Yes No STOP
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Algorithms & Flowcharting Ii

Here is an algorithm and flowchart to print the multiplication table for 6: Algorithm: 1. Set counter = 1 2. Print counter "x 6 = " counter * 6 3. Increment counter by 1 4. If counter is less than or equal to 12, go to step 2 5. Stop Flowchart: START Set counter = 1 Print counter "x 6 = " counter * 6 Increment counter by 1 counter <= 12? Yes No STOP
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

ALGORITHMS & FLOWCHARTING II

LOOPS
Computers are particularly well suited to applications in which operations are repeated many times. If the same task is repeated over and over again a loop can be used to reduce program size and complexity

Iteration (Loops)
Loops repeat a set of instructions Two types of loops:

loops ( for ) perform instructions explicit number of times Indefinite loops ( while ) perform instructions an indefinite number of times (until a certain test fails)
Definite

Questions for You

take 2 minutes and draw a situation on paper that would necessitate that you use a while loop to solve the problem

for Loops

General form: for ( <initialize variable>; <test>; <increment> ) { <instruction set> }

for Loops (cont.)


<initialize variable> sets a variable/identifier to a certain value (variable is usually count, i, j) <test> is a test that is evaluated each time the body is about to be executed (when false, loop is exited) <increment> changes the loop control variable (usually x++ or x--)

for Loops (cont.)

Example: for (int x=1; x<=5; x++) { marker.move(); }


This causes maker to move 5 times

for Loops (cont.)


Flow of for loops:
Initialization statement executes 2. If test is true, proceed to step 3; if test is false, go to step 6
1.

Instructions in body of loop are executed 4. Increment statement executes 5. Return to step 2 6. Program proceeds to statements after loop
3.

while Loops
General form: while ( <test> ) { <instruction list> } Loop continues until test is false

Questions for You

back on slide 3 you drew up a situation that would best be solved using a while loop - now write the code to solve your problem

while Loops (cont.)

Example: while (rebot.frontIsClear ()) { rebot.move (); }


Causes rebot to move continuously until there is a wall in front of it

while Loops (cont.)


Flow of while loops
If test is true, proceed to step 2; if test is false, go to step 4 2. Instructions in body of loop are executed 3. Go to step 1 4. Statements after loop are executed
1.

Building a While Loop


Formalizing (making easier) the ad-hoc way youve been writing while loops to this point..

1 Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing) Step 2 Use the opposite form of step 1s result as the <test> for the loop. You just need to negate the entire thing.
Step

Building a While Loop (contd)


Step 3 make progress toward the goal (completion of the loop) within the loop Step 4 Do whatever is required before and/or after the loop to ensure that we solve the given problem Example: Pick all beepers from a corner without knowing how many there are.

Lets build a while loop


Move a robot until it is not on a corner with any other robots and there is a wall on the left, then pick up the beeper that will be waiting there (the
hard part is getting the condition correct without using a trialand-error method)

You may use && and || for this exercise. Use the 4-step process now
while (???) { ??? }

Your try the while condition


You may use && and || for this Write a loop that moves a robot forward until it is either next to a robot, next to a beeper, or there is no wall on its right while ( ??? ) { ??? }

Infinite Loops

In a for or while loop, it is possible for the test to never be false

When this happens, the loop continues infinitely Depending on the compiler, application, and other circumstances, an error may occur or the app may crash

Example 8: Write an algorithm and draw a flowchart to calculate 24 .


Algorithm: Step 1: Base 2 Step 2: Product Base Step 3: Product Product * Base Step 4: Product Product * Base Step 5: Product Product * Base Step 6: Print Product

Flowchart
START

Base2
Product Base Product Product * Base Product Product * Base Product Product * Base Print Product

STOP

Question: What happens if you want to

calculate 2 to the power of 1000? Answer: Use a LOOP (repeated execution of the same set of instructions)

Example 9: Write an algorithm and draw a flowchart to calculate 24 using a loop approach? Verify your result by a trace table.

Algorithm:
Base 2 Power 4 Product Base Counter 1 While Counter < Power Repeat Step 5 through step 7 Step 6: Product Product * Base Step 7: Counter Counter +1 Step 8: Print Product Step 1: Step 2: Step 3: Step 4: Step 5:

START

Base 2 Power 4
Product Base Counter 1

is Counter < Power

Y Product Product * Base Counter Counter + 1

Print Product

STOP

TRACING
BASE POWER PRODUCT COUNTER COUNTER < POWER STEP 1: STEP 2: STEP 3: STEP 4: STEP 5: STEP 6: STEP 7: STEP 5: STEP 6: STEP 7: STEP 5: STEP 6: STEP 7: STEP 5: 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ? 4 4 4 4 4 4 4 4 4 4 4 4 4 ? ? 2 2 2 2x2=4 4 4 4x2=8 8 8 8x2=16 16 16 ? ? ? 1 1 1 1+1=2 2 2 2+1=3 3 3 3+1=4 4 ? ? ? T T T T T T T T T F F

Step 1: Base 2 Step 2: Power 4 Step 3: Product Base Step 4: Counter 1 Step 5: While Counter < Power Repeat Step 5 through step 7 Step 6: Product Product * Base Step 7: Counter Counter +1 Step 8: Print Product

STEP 8:

print

16.

Example 10: Write down an algorithm and draw a flowchart to find and print the largest of three numbers. Read numbers one by one. Verify your result by a trace table. (Use 5, 7, 3 as the numbers read)

Algorithm

Step 1: Step 2: Step 3: Step 4:

Step 5: Step 6:

Step 7:

Input N1 Max N1 Input N2 If (N2>Max) then Max = N2 endif Input N3 If (N3>Max) then Max = N3 endif Print The largest number is:,Max

Flowchart & Tracing


N1 Step1: Step 2: Step 3: Step 4: Step 5: Step 6: 5 5 5 5 5 5 N2 ? ? 7 7 7 7 N3 ? ? ? ? 3 3 Max N2>Max N3>Max ? 5 5 7 7 7 ? ? T T F F ? ? ? ? F F

START

INPUT N1

MAXN1

INPUT N2

N N2>MAX Y MAXN2

INPUT N3

Step 8: Print Largest Number is 7

N N3>MAX Y

MAXN3

Print Largest Number is, MAX

STOP

Example 11: Write down an algorithm and draw a flowchart to find and print the largest of N (N can be any number) numbers. Read numbers one by one. Verify your result by a trace table. (Assume N to be 5 and the following set to be the numbers {1 4 2 6 8 })

Algorithm:

Step 1: Step 2: Step 3: Step 4: Step 5:


Step 6: Step 7: Step 8:

Step 9:

Input N Input Current Max Current Counter 1 While (Counter < N) Repeat steps 5 through 8 Counter Counter + 1 Input Next If (Next > Max) then Max Next endif Print Max

START Input
N, Current

Max Current Counter 1

N
Counter < N

Y
Counter Counter +1

Print Max

Input
Next

STOP Next >Max Y Max Next N

Tracing
N
Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 5 Step 6 Step 7 Step 8 Step 5 Step 6 Step 7 Step 8 Step 5 Step 6 Step 7 Step 8 Step 5 Step 9 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

Current
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Max

Counter

Counter < N

Next

Next > Max

1 1 1 1 1 4 4 4 4 4 4 4 4 6 6 6 6 8 8

1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5

T T T T T T T T T T T T T T F F F F

4 4 4 4 2 2 2 2 6 6 6 6 8 8 8

T F F F F F F T T F F T T F

8 output

Examples

Write an algorithm and draw a flowchart to print the multiplication table for 6's. i.e. ---- 1 6 = 6 ---- 2 6 = 12 ---- 12 6 = 72

You might also like