Chapter 7 Problem Solving with Loops

Slides:



Advertisements
Similar presentations
Repetition Control Structures
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Chapter 5: Loops and Files.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Structured Program Development in C
Chapter 3 Planning Your Solution
ALGORITHMS AND FLOWCHARTS
The University of Texas – Pan American
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
ITEC113 Algorithms and Programming Techniques
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
REPETITION CONTROL STRUCTURE
PROGRAM CONTROL STRUCTURE
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
( Iteration / Repetition / Looping )
Topics Introduction to Repetition Structures
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Iteration: Beyond the Basic PERFORM
ALGORITHMS AND FLOWCHARTS
Loops CIS 40 – Introduction to Programming in Python
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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

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

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

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 (incrementing @ decrementing) Accumulating ( calculating a sum @ total)

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

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

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

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)

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

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

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

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

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 value @ flag??

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

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:

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

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)

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

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

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

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

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

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

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

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

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

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

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

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:

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

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.

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

Flow Chart It’s drawing time 

Sample Output: Let’s try to code 

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

More exercises pp.186, PROBLEMS Question 1 Question 3

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

Loop Challenge - 1

Loop Challenge - 2

Loop Challenge - 3

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

Loop Challenge - 5

Loop Challenge - 6

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.

Loop Challenge – 7 (cont.)

Loop Challenge – 8