· 2019-09-02 · Problem Solving REAL WORLD . Problem Solving . REAL WORLD roblem Solving
Problem Solving with Loops
description
Transcript of Problem Solving with Loops
![Page 1: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/1.jpg)
Lesson 7
McManusCOP1006 1
![Page 2: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/2.jpg)
Flowchart Symbols
The Loop Logic Structure
Incrementing/Decrementing
Accumulating While/WhileEnd Repeat/Until
Automatic-Counter Loop (For)
Nested Loops Indicators Algorithm
Instructions Recursion
McManusCOP1006 2
![Page 3: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/3.jpg)
Decision◦ True/False/Else
Process Assign
McManusCOP1006 3
Assign
Process
Decision
![Page 4: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/4.jpg)
Allows the programmer to specify that an action is to be repeated based on the truth or falsity of some condition.While there are more timecards to be processed
Obtain time from each card and calculate payAs long as there are items still remaining on the
list the loop will continue.◦ Otherwise known as iteration.
McManusCOP1006 4
![Page 5: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/5.jpg)
Known as definite because the number of iterations to be performed at runtime is known.
Also known as Counter-controlled Repetition ◦ Uses a variable called a counter to control the
number of times a set of statements should execute.
McManusCOP1006 5
![Page 6: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/6.jpg)
Known as indefinite because the number of iterations at runtime is not known before the loop begins executing.◦ Uses a sentinel value (signal value, a flag value,
or a dummy value to indicate “end of data entry.” When using a sentinel
Choose a sentinel that will not naturally exist within the range of data being used.
Ex. 999-99-9999 for Social Security Numbers
McManusCOP1006 6
![Page 7: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/7.jpg)
one of the three types of program control structures ◦ Sequence◦ Selection ◦ Repetition
McManusCOP1006 7
![Page 8: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/8.jpg)
Initialization Condition (the test) Increment (or Decrement)
The Accumulator, although used in frequently in loops, is NOT part of the generic looping process
McManusCOP1006 8
![Page 9: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/9.jpg)
The Initialization set to an initial value
◦ usually zero but not all the time Examples:
◦ Count = 0 ◦ Count = 1◦ Count = 100
McManusCOP1006 9
![Page 10: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/10.jpg)
The Test or Condition◦ tested before the start of each loop repetition,
called the iteration or pass. Examples:
◦ Count < 3 Count = 10◦ Count <= 3 Count <> 10◦ Count > 5◦ Count >= 5
McManusCOP1006 10
![Page 11: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/11.jpg)
The Increment (or Decrement)◦ updates the variable during each iteration◦ must be part of the loop body (usually the last
line) Examples:
◦ Count = Count + 1◦ Count = Count - 1
McManusCOP1006 11
![Page 12: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/12.jpg)
Variables used to store values being computed in increments during the execution of a loop.
Not part of the Looping Process◦ But quite often an integral addition to the process◦ Very often looks like an increment, which is part of the
looping process Examples:
◦ Sum = Sum + 1◦ TotalGrade = TotalGrade + CurrentGrade
McManusCOP1006 12
![Page 13: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/13.jpg)
The Three Most Common Loop-control statements: ◦ the while, ◦ the for, and ◦ the repeat
But there are lots of other loop variations dependent on the language.
McManusCOP1006 13
![Page 14: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/14.jpg)
The while statement ◦ The most versatile of the loops◦ The loop body contains the instructions to be
repeated.◦ The loop-repetition condition is the Boolean
expression after the reserved word while which is evaluated before each repetition of the loop body.
McManusCOP1006 14
![Page 15: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/15.jpg)
Set counter To 0While counter < final_value
statementsIncrement counter by 1
While-end
McManusCOP1006 15
?
Init
Stmt
true
false
Stmt
![Page 16: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/16.jpg)
x = 3; How many times does theCount = 0; loop execute?while Count < 3 do begin What is displayed? X = X * 2; 6 Print (X); 12 Count = Count + 1 24 end; {while Count}
Pascal Code
McManusCOP1006 16
![Page 17: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/17.jpg)
x = 3Count = 0While Count < 3 X = X * 2 Print X Count = Count + 1 End While
VB Code
McManusCOP1006 17
![Page 18: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/18.jpg)
int X = 3;int count = 0;While (count < 3)
{ X = X * 2 cout << X << endl; Print statement Count = Count + 1;}
C++ Code
McManusCOP1006 18
![Page 19: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/19.jpg)
A Sentinel-Controlled Loop◦Controlled by a sentinel value.◦Examples
end-of-file marker (EOF)end-of-line marker (EOL)999999999 for SSN 999999 for date
McManusCOP1006 19
![Page 20: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/20.jpg)
A Sentinel-Controlled Loop ◦ Pseudocode Template
Initialize Sum to 0 Read the first value into counter variable While counter variable is not sentinel do
Add counter value to SumRead next value into counter value
While-end; {while}
McManusCOP1006 20
![Page 21: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/21.jpg)
Boolean Flag-Controlled Loops◦ Executes until the event being monitored occurs.
A program flag, or flag, is a Boolean variable whole value (True or False) signals whether a particular event occurs.
The flag should initially be set to False and reset to True when the event occurs.
McManusCOP1006 21
![Page 22: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/22.jpg)
Boolean Flag-Controlled Loops◦Pseudocode TemplateInitialize flag to Falsewhile not flag
statements.. Reset flag to True if the event being monitored
occurs while-end; {while}
McManusCOP1006 22
![Page 23: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/23.jpg)
Logical Opposite of the While Loop
Unlike the While/While-End, the Do/Until tests for falsity.◦ Should be used when the
question being asked is more naturally asked in the negative.
McManusCOP1006 23
Counter < 5
Init
Stmt
false
true
Stmt
![Page 24: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/24.jpg)
x = 3Count = 3Do Until Count < 1 X = X * 2 Print X Count = Count - 1Loop
McManusCOP1006 24
![Page 25: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/25.jpg)
Similar to the While/While-End structure◦ In the While Loop
loop-continuation is tested at the beginning of the loop before the body of the loop is performed.
◦ In the Repeat Until Loop loop-continuation is tested after the loop body is
performed, thus executing the loop body at least once.
McManusCOP1006 25
![Page 26: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/26.jpg)
Pseudocode TemplateRepeat
statementsincrement counterVariable
Until testCondition Notice that the
statements are executed before thecondition to end the loop
McManusCOP1006 26
counter = 1
counter < 5
True
False
statements
ActionoccursbeforeTest
![Page 27: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/27.jpg)
Combines all of the Loop Process components into one statement.
Notes on the Counter:◦ Can be used non-destructively but must not be
modified (destructively) within the loop body.◦ Should be a local variable.
The loop body will not be executed if initial is greater than the final value, unless the increment value is negative.
McManusCOP1006 27
![Page 28: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/28.jpg)
Pseudocode ExampleFor counter = initialvalue To finalvalue Step 1
statements… ‘to incrementNext counter
For counter = finalvalue To initialvalue Step -1statements… ‘to decrement
Next counter
McManusCOP1006 28
![Page 29: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/29.jpg)
Note that the “To” is equivalent to “while less than or equal to”
McManusCOP1006 29
counter = 1
Print countercounter =
counter + 1counter
<= 5
True
False
(implicit) (implicit)
(implicit)
For counter = 1 to 5 Step 1
Print counter
Next counter
![Page 30: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/30.jpg)
Loops can be nested just as if statements.◦ Cannot use the same counter-control variable for
the inner loop as is used for the outer loop.
McManusCOP1006 30
x yTrue
False False
True
![Page 31: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/31.jpg)
Initialize outer loopWhile outer loop test {while}
statements... Initialize inner loop
While inner loop test {while} Inner loop processing and Update inner loop variable
while-end {inner while} statements... Update outer loop variable
while-end {outer while}
McManusCOP1006 31
![Page 32: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/32.jpg)
Dim outercounter As IntegerDim innercounter As Integeroutercounter = 1 While outercounter <= 3
innercounter = 1While innercounter <= 3 innercounter = innercounter + 1Wend ‘innercounteroutercounter = outercounter + 1
Wend ‘outercounterVB Code
McManusCOP1006 32
![Page 33: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/33.jpg)
Assertions about the characteristics of a loop that always must be true for a loop to execute properly.
The assertions are true on loop entry, at the start of each loop iteration, and on exit from the loop.◦ They are not necessarily true at each point within
the body of the loop.
McManusCOP1006 33
![Page 34: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/34.jpg)
when the loop is skipped entirely (zero iteration loop)
when the loop body is executed just once When the loop executes some normal
number of times When the loop fails to exit (infinite loop)
McManusCOP1006 34
![Page 35: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/35.jpg)
Verify the Algorithm◦ Test the value of the algorithm
before the loop during the loop, and after the loop.
McManusCOP1006 35
![Page 36: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/36.jpg)
Beware of infinite loops Beware of off-by-one Loop Errors
◦ Executes the loop one too many times◦ Executes the loop one too few times
McManusCOP1006 36
![Page 37: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/37.jpg)
Or twisted tails
McManusCOP1006 37
![Page 38: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/38.jpg)
Occurs when a function calls itself from within the body of the function
Also known as “procedural iteration” Classic Examples of Recursion
◦ Factorial◦ Fibonacci
McManusCOP1006 38
![Page 39: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/39.jpg)
How it works:If X = 3, the chain of recursive calls would be as
follows:Factorial(3) 3 * Factorial(2) 3 * (2 * Factorial(1) )
Precondition x 0Postcondition
Returns the product 1 * 2 * 3 * …* x for x > 1Returns 1 when X is 0 or 1.
McManusCOP1006 39
![Page 40: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/40.jpg)
Private Function Factorial(ByRef y As Double) _As Double ‘2nd double defines Factorial
If y <= 1 Then Factorial = 1 ' Base case Else Factorial = y * Factorial(y - 1) ' Recursive step End IfEnd FunctionThe Base Case is also known as the Stopping Case.
McManusCOP1006 40
![Page 41: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/41.jpg)
Investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances.
Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on.
The puzzle that Fibonacci posed was...
How many pairs will there be in one year?
At the end of the first month, they mate, but there is still one only 1 pair.
At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field.
At the end of the third month, the original female produces a second pair, making 3 pairs.
At the end of the fourth month, the original female has produced yet another new pair, the female born two months ago produces her first pair also, making 5 pairs.
McManusCOP1006 41
![Page 42: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/42.jpg)
McManusCOP1006 42
FemaleMale
![Page 43: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/43.jpg)
We can make another picture showing the Fibonacci numbers 1,1,2,3,5,8,13,21
If we start with two small squares of size 1 next to each other.
On top of both of these draw a square of size 2 (=1+1).
McManusCOP1006 43
![Page 44: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/44.jpg)
The number of clockwise spirals and the number of counterclockwise spirals formed by the seeds of certain varieties of flowers
McManusCOP1006 44
![Page 45: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/45.jpg)
McManusCOP1006 45
![Page 46: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/46.jpg)
Fibonacci NumbersEach Fibonacci number is the sum of the two preceding Fibonacci numbers. The Fibonacci series defined recursively:
Fibonacci(0) = 0
Fibonacci(1) = 1
Fibonacci(2) = 1
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n - 2)
McManusCOP1006 46
A Shortened Form:Fib (1) = 1 Fib (2) = 1Fib (n) = Fib (n - 1) + Fib (n - 2)
Fib(3) = Fib(2) + Fib(1) = 1 + 1 = 2Fib(4) = Fib(3) + Fib(2) = 2 + 1 = 3Fib(5) = Fib(4) + Fib(3) = 3 + 2 = 5Fib(6) = Fib(5) + Fib(4) = 5 + 3 = 8
![Page 47: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/47.jpg)
Chapter 8
McManusCOP1006 47
![Page 48: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/48.jpg)
Used for menu-driven programs and event-driven programs
Menu ◦ A list of options that a program based on case
logic can perform Event-driven
◦ Order is dictated by the user, not the programmer, by which button is clicked on
McManusCOP1006 48
![Page 49: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/49.jpg)
The SetupList1.AddItem “England” ‘adds items to aList1.AddItem “Germany” ‘Windows list boxList1.AddItem “Spain”List1.AddItem “Italy”
McManusCOP1006 49
VB Code
![Page 50: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/50.jpg)
Label3.Text = List1.Text‘Sets the caption for the Label ‘above the output textbox
‘Next, the appropriate language phrase is displayed in the textbox based on the Country selected.
McManusCOP1006 50
VB Code
![Page 51: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/51.jpg)
Select Case List1.ListIndexCase 0
Label4.Caption = “Hello, programmer”Case 1
Label4.Caption = “Hallo, programmierer”Case 2
Label4.Caption = “Hola, programador”Case 3
Label4.Caption = “Ciao, programmatori”End Select
McManusCOP1006 51
VB Code
![Page 52: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/52.jpg)
Select Case AgeCase 16
LabelAge.Caption = “You can drive now! Parents Beware!”Case 18
LabelAge.Caption = “You can vote now!”Case 21
LabelAge.Caption = “You can drink wine with your meals.”Case 65
LabelAge.Caption = “Time to retire and have fun!”Case Else
LabelAge.Caption = “You’re a great age! Enjoy it!End Select
McManusCOP1006 52
![Page 53: Problem Solving with Loops](https://reader035.fdocuments.in/reader035/viewer/2022081519/5681472c550346895db464d8/html5/thumbnails/53.jpg)
McManusCOP1006 53