Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Problem Solving with Loops

Similar presentations


Presentation on theme: "Chapter 7 Problem Solving with Loops"— Presentation transcript:

1 Chapter 7 Problem Solving with Loops
CMPF144 Introduction to Problem Solving and Basic Computer

2 Objectives Develop problems using loop logic structure with decision and sequential logic structures Use problem-solving tools (IPO Chart, Pseudocode, Flowchart) when developing solution using loop logic structure Use counters and accumulators in problem solution

3 Objectives (Cont.) Distinguish different uses of two types of loop logic structures Distinguish different uses of two loop designs Use nested loop instructions to develop problem solution Use recursion in simple problem

4 Loop Logic Structure Also known as Repetition Control Structure
Is used when we need to perform certain SAME actions REPEATEDLY Main DIFFICULTY: to identify which instructions should be repeated Performing TWO (2) standard tasks: Counting decrementing) Accumulating ( calculating a total)

5 Loop Logic Structure (Cont.)
Can be categorized into TWO (2) main types: While/End_While Repeat/Until Each type of loop structure has a specific use according to language / problem Each type of structure DO NOT share the same Pseudocode and Flow chart

6 Counting Can be either INCREMENTING or DECREMENTING
Incrementing is done by adding a constant, such as 1 or 2, to the value of a variable Example: Decrementing is a process of subtracting a constant from a current variable’s value Variable names are the same on both sides of equal sign. Is a type of assignment instruction i = i + 1 counter = counter - 2

7 Accumulating A process of summing up a group of numbers
Similar to incrementing, except a variable instead of a constant is added to another variable Example: Is also a type of assignment instruction total = total + number

8 While/End_While Using the keyword of WHILE…THEN….END_WHILE
Tells the computer that while the condition is TRUE, it should repeat ALL instructions between the WHILE and END_WHILE (loop)

9 While/End_While (Cont.)
Diamond is used for the while part, which represents condition / decision Decision will be checked before executing any instructions within the loop If resultant is FALSE at the start, the instructions within the loop will NOT be executed If resultant is TRUE, the complete set of instructions will be executed, and computer will go back to the beginning of the loop and process the condition again, until it is FALSE

10 While/End_While (Cont.)
Pseudocode Flow Chart Begin . WHILE condition(s) THEN Instruction END_WHILE End

11 Exercise 1: Develop an IPO chart, pseudocode, flowchart and complete Python program, to find the average age of all the students in a class

12 IPO Chart Input Process Output age Get age Calculate average
Display average average

13 Pseudocode Begin Set count = 0 Set sum = 0.0 Get age WHILE age <> 0 THEN sum = sum + age count = count + 1 END_WHILE average = sum / count Display average End Tell me, Which one is: Primer Read?? Trip flag??

14 Flow Chart Begin Set count = 0 Set sum = 0.0 F T sum = sum + age
Get age age <> 0 F T sum = sum + age count = count + 1 Get age average = sum/count Display average End

15 Python Code sum = 0.0 count = 0 age = input("How old are you:") while age <> 0: sum = sum + age count = count + 1 average = sum/count print "The average age is", average Output:

16 Repeat/Until Using the keyword of REPEAT….UNTIL
Tells the computer to repeat the set of instructions between the Repeat and the Until, until a condition is TRUE

17 Repeat/Until (Cont.) TWO (2) major differences between Repeat/Until loop with While/End_While loop: In While/End_While loop, program continues to loop as long as resultant of condition is TRUE. Whereas Repeat/Until loop continues to loop while resultant of condition is FALSE In While/End_While loop, condition is processed at the BEGINNING. However, in the Repeat/Until loop, the condition is processed at the END, which cause the instructions in the loop to be processed at least ONCE, regardless of whether it is necessary (hence, While/End_While is often the preferred choice)

18 Repeat/Until(Cont.) Flow Chart Pseudocode Begin . REPEAT Instruction
UNTIL condition(s) End

19 Exercise 2: Refer back to same scenario case in Exercise 1
IPO chart is still the same Pseudocode and Flow chart is different as compared to solution in Exercise 1

20 Pseudocode Begin Set count = 0 Set sum = 0.0 Get age REPEAT sum = sum + age count = count + 1 UNTIL age == 0 average = sum / count Display average End

21 Flow Chart Begin Set count = 0 Set sum = 0.0 sum = sum + age
Get age sum = sum + age count = count + 1 Get age F age == 0 T average = sum/count Display average End

22 General Design of Loop Two general designs: Automatic-counter loop
Sentinel-controlled loop

23 Automatic-Counter Loop
Also known as counter-controlled loop Is used when we need to execute a number of instructions from the program for a finite, pre- determined number of time It increments or decrements a variable (which acts as a counter) each time the loop is repeated

24 Automatic-Counter Loop (Increments)
The programmer uses a variable as a counter that starts counting at a specified number and increments the variable each time the loop is processed. Example: The amount to be incremented is specified by the instruction. The set of instructions within the loop repeats until the counter is greater than an ending number count = 0 count = count + 2

25 Exercise 3: Develop an IPO chart, pseudocode, flowchart and complete Python program, to find the average age of FIVE (5) users

26 IPO Chart Input Process Output age Get age Calculate average
Display average average

27 Pseudocode Begin Set count = 0 Set sum = 0.0 Get age WHILE count < 4 THEN sum = sum + age count = count + 1 END_WHILE average = sum / count Display average End

28 Flow Chart Begin Set count = 0 Set sum = 0.0 F T sum = sum + age
Get age count< 4 F T sum = sum + age count = count + 1 Get age average = sum/count Display average End

29 Python Code sum = 0.0 count = 0 age = input(“Your age:") while count < 4: sum = sum + age count = count + 1 average = sum/count print "The average age is", average Output:

30 Automatic-Counter Loop (Decrements)
The programmer uses a variable as a counter that starts counting at a specified number and increments the variable each time the loop is processed. Example: The amount to be decremented is specified by the instruction. The set of instructions within the loop repeats until the counter is lesser than or not equal to an ending number count = 10 count = count - 1

31 Exercise 4: Develop an IPO chart, pseudocode, flowchart and complete Python program, for a program which will prompt for the user to insert a positive integer. The program will then perform count down (decrement by ONE) towards the integer inserted. The message of “Happy New Year” will be displayed AFTER the integer is decreased to ZERO.

32 IPO Chart Input Process Output number Get number
Display number until 0 Display “Happy New Year” Number Message of “Happy New Year”

33 Pseudocode Begin Get number WHILE number >= 0 THEN Display number number = number - 1 END_WHILE Display “Happy New Year” End

34 Display “Happy New Year”
Flow Chart Begin Get number F number >= 0 T Display number number = number - 1 Display “Happy New Year” End

35 Python Code number=input("Give me a number:") while number >= 0: print number number = number - 1 print "Happy New Year" Output:

36 Sentinel-controlled Loop
Is used when we need to execute a number of instructions from the program indefinitely until the user tells it to stop or a special condition is met Indicator (also known as flag, switches, trip values or sentinel values) needs to be set internally in the program to control when the processing of a loop should stop

37 Exercise 5: Develop an IPO chart, pseudocode, flowchart and complete Python program, for a program which will read in a series of student’s score (as long as the score is not -99). Upon receiving the sentinel value -99, the program will stop reading in students’ score. It will then compute and display average score for all students and the total number of students whom have inserted scores into program.

38 IPO Chart Input Process Output score Get score
Calculate average, count Display average, count average count

39 Pseudocode Begin Set sum = 0.0 Set count = 0 Get score WHILE score <> -99 THEN sum = sum + score count = count + 1 END_WHILE average = sum / count Display average, count End

40 Flow Chart Begin Set sum = 0.0 Set count = 0 F T sum = sum + score
Get score score <> -99 F T sum = sum + score count = count + 1 Get score average = sum/count Display average, count End

41 Why don’t you try to code??
Python Code Why don’t you try to code?? Output:

42 Nested Loops Loops can be nested like decision logic structure
Each inner loop must be nested inside the loop just outside it (outer loop) However, both loops do NOT have to come from the same type of loop structure Example: A While/End_While Loop can be nested inside a Repeat/Until Loop

43 Exercise 6: Develop an IPO chart, pseudocode, flowchart and complete Python program, for a program which will prompt for two inputs from user: The number of multiplication timetables The number of rows required in each set of multiplication timetable The program will then generate a complete timetable set based on user’s requirement

44 IPO Chart Input Process Output number row Get set Get row
Calculate multiply Display multiply multiply

45 Begin Get number Get row set i = 1 WHILE i <= number THEN Set j = 1 WHILE j <= row THEN multiply = i * j Display multiply j = j + 1 END_WHILE i = i + 1 Display “*******” End Pseudocode

46 Flow Chart It’s drawing time 

47 Sample Output: Let’s try to code 

48 Recursion Occurs when a module or a function calls back itself
The condition that ends the loop must be within the module Will be covered in lab module 7

49 More exercises pp.186, PROBLEMS Question 1 Question 3

50 Loop Challenges CONSTRUCT appropriate Pseudocode and Flow chart for each scenario sample output provided Then CONVERT each Flow chart into complete Python program

51 Loop Challenge - 1

52 Loop Challenge - 2

53 Loop Challenge - 3

54 Loop Challenge - 4 Develop a program to calculate how many years does it take for a user to get RM in his saving account, assuming that he deposited RM5000 into a bank, with yearly interest 2.00%

55 Loop Challenge - 5

56 Loop Challenge - 6

57 Loop Challenge - 7 An applicant for a secretarial job will be given a maximum of five typing tests. The applicant will be hired as soon as he or she scores over 50 words per minute on two tests. Write a program that allows a supervisor to enter each typing test score on completion of each test. The program should print Hire as soon as the applicant qualifies without asking for further tests. If after five test scores have been entered the typist still hasn’t qualified, the program should print reject.

58 Loop Challenge – 7 (cont.)

59 Loop Challenge – 8


Download ppt "Chapter 7 Problem Solving with Loops"

Similar presentations


Ads by Google