lect 8 and9
-
Upload
waqasfarooq75396423 -
Category
Documents
-
view
219 -
download
0
Transcript of lect 8 and9
-
8/14/2019 lect 8 and9
1/32
ALGORITHMS &
FLOWCHARTING II
-
8/14/2019 lect 8 and9
2/32
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
-
8/14/2019 lect 8 and9
3/32
Iteration (Loops)
Loops repeat a set of instructions
Two types of loops:
Definite loops ( for) perform instructions
explicit number of times
Indefinite loops ( while) perform instructions
an indefinite number of times (until a certaintest fails)
-
8/14/2019 lect 8 and9
4/32
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
-
8/14/2019 lect 8 and9
5/32
forLoops
General form:for ( ;
; )
{
}
-
8/14/2019 lect 8 and9
6/32
forLoops (cont.)
sets avariable/identifier to a certain value
(variable is usually count, i, j) is a test that is evaluated each
time the body is about to be executed(when false, loop is exited)
changes the loop controlvariable (usually x++or x--)
-
8/14/2019 lect 8 and9
7/32
forLoops (cont.)
Example:for (int x=1; x
-
8/14/2019 lect 8 and9
8/32
forLoops (cont.)
Flow of forloops:
1. Initialization statement executes
2. If test is true, proceed to step 3; if test isfalse, go to step 6
3. Instructions in body of loop are executed
4. Increment statement executes5. Return to step 2
6. Program proceeds to statements after loop
-
8/14/2019 lect 8 and9
9/32
whileLoops
General form:while ( )
{
}
Loop continues until test is false
-
8/14/2019 lect 8 and9
10/32
-
8/14/2019 lect 8 and9
11/32
whileLoops (cont.)
Example:while (rebot.frontIsClear ())
{rebot.move ();
}
Causes rebot to move continuously untilthere is a wall in front of it
-
8/14/2019 lect 8 and9
12/32
whileLoops (cont.)
Flow of whileloops
1. If test is true, proceed to step 2; if test is
false, go to step 42. Instructions in body of loop are executed
3. Go to step 1
4. Statements after loop are executed
-
8/14/2019 lect 8 and9
13/32
Building a While Loop
Formalizing (making easier) the ad-hoc way youve been writing while
loops to this point..
Step 1Identify what is true when the
loop is finished (this is always easierthan determining what is false while it is
still executing)
Step 2Use the opposite form of step1s result as the for the loop. You
just need to negate the entire thing.
-
8/14/2019 lect 8 and9
14/32
Building a While Loop (contd)
Step 3make progress toward the goal
(completion of the loop) within the loop
Step 4Do whatever is required beforeand/or after the loop to ensure that we
solve the given problem
Example: Pick all beepers from a cornerwithout knowing how many there are.
-
8/14/2019 lect 8 and9
15/32
-
8/14/2019 lect 8 and9
16/32
Your try the while condition
You may use &&and ||for this
Write a loop that moves a robot forwarduntil it is either next to a robot, next to abeeper, or there is no wall on its right
while ( ??? ) {
???
}
-
8/14/2019 lect 8 and9
17/32
Infinite Loops
In a foror while loop, it is possible for
the test to never be false
When this happens, the loop continuesinfinitely
Depending on the compiler, application,
and other circumstances, an error mayoccur or the app may crash
-
8/14/2019 lect 8 and9
18/32
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
-
8/14/2019 lect 8 and9
19/32
FlowchartSTART
ProductBase
Print
Product
STOP
ProductProduct * Base
ProductProduct * Base
ProductProduct * Base
Base2
-
8/14/2019 lect 8 and9
20/32
Question: What happens if you want tocalculate 2 to the power of 1000?
Answer: Use a LOOP (repeated execution
of the same set of instructions)
-
8/14/2019 lect 8 and9
21/32
Example 9: Write an algorithm and draw a
flowchart to calculate 24using a loop
approach? Verify your result by a tracetable.
-
8/14/2019 lect 8 and9
22/32
Algorithm:
Step 1: Base2
Step 2: Power 4
Step 3: Product Base
Step 4: Counter 1
Step 5: While Counter < PowerRepeat Step 5 through step 7
Step 6: Product Product * Base
Step 7: Counter Counter +1
Step 8: Print Product
-
8/14/2019 lect 8 and9
23/32
START
ProductBaseCounter1
Print
Product
STOP
Y
is
Counter < Power
ProductProduct * Base
CounterCounter + 1
N
Base2
Power4
-
8/14/2019 lect 8 and9
24/32
TRACING
BASE POWER PRODUCT COUNTERCOUNTER < POWER
STEP 1: 2 ? ? ? ?
STEP 2: 2 4 ? ? ?
STEP 3: 2 4 2 ? ?
STEP 4: 2 4 2 1 T
STEP 5: 2 4 2 1 T
STEP 6: 2 4 2x2=4 1 T
STEP 7: 2 4 4 1+1=2 T
STEP 5: 2 4 4 2 T
STEP 6: 2 4 4x2=8 2 T
STEP 7: 2 4 8 2+1=3 T
STEP 5: 2 4 8 3 T
STEP 6: 2 4 8x2=16 3 T
STEP 7: 2 4 16 3+1=4 F
STEP 5: 2 4 16 4 F
STEP 8: print 16.
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
-
8/14/2019 lect 8 and9
25/32
Example 10:Write down an algorithm and
draw a flowchart to find and print the
largest of three numbers. Read numbersone by one. Verify your result by a trace
table. (Use 5, 7, 3 as the numbers read)
-
8/14/2019 lect 8 and9
26/32
Algorithm
Step 1: InputN1
Step 2: MaxN1
Step 3: InputN2
Step 4: If (N2>Max) then
Max = N2endif
Step 5: InputN3
Step 6: If (N3>Max) then
Max = N3
endif Step 7: Print The largest number is:,Max
-
8/14/2019 lect 8 and9
27/32
Flowchart &
Tracing
START
INPUT
N1
MAXN1
INPUT
N2
N2>MAX
MAXN2
INPUT
N3
MAXN3
Print
Largest
Number is,
MAX
STOP
N3>MAX
N
Y
N
Y
N1 N2 N3 Max N2>Max N3>Max
Step1: 5 ? ? ? ? ?
Step 2: 5 ? ? 5 ? ?
Step 3: 5 7 ? 5 T ?
Step 4: 5 7 ? 7 T ?
Step 5: 5 7 3 7 F F
Step 6: 5 7 3 7 F F
Step 8: Print Largest Number is 7
-
8/14/2019 lect 8 and9
28/32
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 tobe the numbers {1 4 2 6 8 })
-
8/14/2019 lect 8 and9
29/32
Algorithm:
Step 1: InputN
Step 2: Input Current
Step 3: MaxCurrent
Step 4: Counter1
Step 5: While (Counter < N)Repeat steps 5 through 8
Step 6: CounterCounter + 1
Step 7: InputNext
Step 8: If (Next > Max) then
MaxNextendif
Step 9: Print Max
-
8/14/2019 lect 8 and9
30/32
START
InputN, Current
Max Current
Print
Max
STOP
Y
Counter < NN
Counter 1
Counter Counter +1
InputNext
Next
>Max
Y
N
Max Next
-
8/14/2019 lect 8 and9
31/32
TracingN Current Max Counter Counter < N Next Next > Max
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
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
4
4
4
4
4
4
4
4
6
6
6
6
8
8
8 output
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/14/2019 lect 8 and9
32/32
Examples
Write an algorithm and draw a flowchartto print the multiplication table for 6's. i.e.
---- 1
6 = 6 ---- 2 6 = 12
---- 12 6 = 72