CHAPTER #7 Problem Solving with Loop
Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction and symbol Recursion
Objectives Develop problems using the loop logic structure Use the problem-solving tools when developing a solution using the loop logic structure Use counters and accumulators in a problem solution Use nested loop instructions Distinguish the different uses of three types of loop logic structures. Use recursion in a simple problem.
Background The loop logic structure is the repeating structure Some ‘process’ is repeated several times (known) or until a condition is reached (unknown) for different sets of data So this structure is extremely important
Example(s) Emptying a pool with small pail Cut a cucumber into small piece Sharpening a pencil Push up 20 times Etc...
Increment The task of incrementing, or counting, as we said, is done by adding a constant, such as 1 or 2, to the value of a variable To write the instruction to increment a variable, you use an assignment statement For example, Counter = Counter + 1 when counting by ones
What’s happen? No of iteration Expression/Statemen t New Value 0 (init)Counter = 1 (example, its called initialization) 1Counter = Counter + 1Counter = = 2 2Counter = Counter + 1Counter = = 3 3Counter = Counter + 1Counter = = 4 etc Red written indicates old value
Accumulating The process of accumulating is similar to incrementing, except a variable instead of a constant is added to another variable, which holds the value of the sum or total The instruction for accumulating is the following: Sum = Sum + Variable
What’s happen? No of iteratio n Old value of sum Example of ‘Value’ Sum = Sum + Value 1Sum = 0Value = 1Sum = = 1 2 Sum = 1 Value = 2Sum = = 3 3 Sum = 3 Value = 3Sum = = 6 4 Sum = 6 Value = 4Sum = = 10 etc Red written indicates old value Initialization : Sum = 0
Product Calculating the product of a series of numbers is similar to finding the sum of a series of numbers with two exceptions First, the plus sign is replaced by the multiplication sign (*) Second, the product variable must be initialized to 1 instead of 0. For example Product = Product * Number
What’s happen? No of iteratio n Old value of Product Example of ‘Number’ Product = Product * Number 1Product = 1Number = 1Product = 1 * 1 = 1 2 Product = 1 Number = 2Product = 1 * 2 = 2 3 Product = 2 Number = 3Product = 2 * 3 = 6 4 Product = 6 Number = 4Product = 6 * 4 = 24 etc Red written indicates old value Initialization : Product = 1
Loop Logic Structure
Types of Loop Structures While/While End For/End For Repeat/Until
Case study Develop a problem-solving solution for counting an average for 10 numbers.
While/WhileEnd Pretest Looping (While Loop) 0-15
Figure 7.1 Flowchart Diagram of While/WhileEnd 0-16
Algorithm #1 using WHILE Set Sum = 0 Set count = 0 While (count < 10) –Enter Number –Sum = Sum + Number –Count = Count + 1 End While //finish when count = 10 or more (>= 10) Average = Sum / Count //count = 10 Display Average
Automatic Counter Loop Format Pretest Looping (For / Counter Loop)
Flowchart of Automatic- Counter Loop
Algorithm #2 using FOR Set Sum = 0 For count = 1 to 10 –Enter Number –Sum = Sum + Number End For //finish for (10 – 1 + 1) repetitions Average = Sum / Count //count = 10 Display Average
Repeat/Until Algorithm Format Posttest Looping (Do-While Loop)
Flowchart Diagram of Repeat/Until
Algorithm #3 using REPEAT/UNTIL Set Sum = 0 Set count = 0 Repeat –Enter Number –Sum = Sum + Number –Count = Count + 1 Until (count >= 10) //finish when count = 10 or more Average = Sum / Count //count = 10 Display Average
Exercise Develop a problem-solving solution for counting an average for determined numbers (entered by user).
Nested Loops
Exercise – Nested Loop Write a problem-solving solution for a problem that ask user for an input (called n), then display a ‘triangle’ with this format * * * * * * *... (depends on n)
Review Mention about problem solving that involves three logic structures
Indicators Logical variables that a programmer sets within a program to change the processing path or to control when the processing of a loop should end Therefore you will get unstopped loop The user has no knowledge of these indicators during processing
Recursion Recursion occurs when a module or a function calls itself The condition that ends the loop must be within the module Recursive procedures can be replaced by conventional loop structures; however, recursive procedures are usually faster
When we using recursion? Recursion is difficult to use in a solution However, it is needed more and more in today’s programming A recursive technique is used in finding the power of a number, sorting large amounts of data, and selecting the correct form from many report forms, and find factorial for a number
Assignment Write a problem-solving solution to solve a problem that takes an integer value and returns the number with its reversed digits. For example, given the number 7631, the solution should return 1367 Hint : Use the loop structure and modulo operation %