An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.

Slides:



Advertisements
Similar presentations
An Introduction to Programming with C++ Fifth Edition Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Advertisements

Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Chapter 2: Algorithm Discovery and Design
Chapter 5: Control Structures II (Repetition)
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.
Chapter 2: Beginning the Problem-Solving Process
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 6: The Repetition Structure
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Chapter 7 Problem Solving with Loops
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
An Introduction to Programming with C++ Sixth Edition
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
CIS 115 AID Teaching Effectively/cis115aid.com FOR MORE CLASSES VISIT
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
Chapter 4: Control Structures I (Selection)
Chapter 8: More on the Repetition Structure
Topic 4: Looping Statements
More on the Selection Structure
REPETITION CONTROL STRUCTURE
An Introduction to Programming with C++ Sixth Edition
1-1 Logic and Syntax A computer program is a solution to a problem.
Exam 1 Review.
Completing the Problem-Solving Process
Programming Logic and Design Fourth Edition, Comprehensive
EGR 2261 Unit 4 Control Structures I: Selection
CHAPTER 5A Loop Structure
The Selection Structure
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
CIS 16 Application Development Programming with Visual Basic
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
Problem Solving.
Chapter 5: Control Structures II (Repetition)
Topics Introduction to Repetition Structures
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure

Objectives Include a posttest loop in pseudocode Include a posttest loop in a flowchart Code a posttest loop using the C++ do while statement Nest repetition structures Raise a number to a power using the pow function An Introduction to Programming with C++, Sixth Edition2

3 Posttest Loops Repetition structures can be either pretest or posttest loops Pretest loop – condition evaluated before instructions are processed Posttest loop – condition evaluated after instructions are processed Posttest loop’s instructions are always processed at least once Pretest loop’s instructions may never be processed

An Introduction to Programming with C++, Sixth Edition4 Figure 8-1 Problem specification and algorithms containing pretest and posttest loops

Posttest Loops (cont’d.) An Introduction to Programming with C++, Sixth Edition5 Figure 8-2 Selection structure added to Algorithm 2 from Figure 8-1

An Introduction to Programming with C++, Sixth Edition6 Flowcharting a Posttest Loop Decision symbol in a flowchart (a diamond) representing a repetition structure contains the loop condition Decision symbol appears at the top of a pretest loop, but at the bottom of a posttest loop

An Introduction to Programming with C++, Sixth Edition7 Figure 8-3 Miller Incorporated problem specification and algorithm shown in flowchart form

An Introduction to Programming with C++, Sixth Edition8 Figure 8-3 Second algorithm for Miller Incorporated problem shown in flowchart form

An Introduction to Programming with C++, Sixth Edition9 Flowcharting a Posttest Loop (cont’d.) Figure 8-4 First sales and bonus amount recorded in the desk-check table Figure 8-5 Second sales amount recorded in the desk-check table Figure 8-6 Current status of the desk-check table

An Introduction to Programming with C++, Sixth Edition10 The do while Statement do while statement is used to code posttest loops in C++ Syntax: do { one or more statements to be processed one time, and thereafter as long as the condition is true } while ( condition ); Some programmers use a comment (such as: //begin loop ) to mark beginning of loop

An Introduction to Programming with C++, Sixth Edition11 The do while Statement (cont’d.) Programmer must provide loop condition –Must evaluate to a Boolean value –May contain variables, constants, functions, arithmetic operators, comparison operators, and logical operators Programmer must also provide statements to be executed when condition evaluates to true Braces are required around statements if there are more than one

An Introduction to Programming with C++, Sixth Edition12 The do while Statement (cont’d.) Figure 8-7 How to use the do while statement

An Introduction to Programming with C++, Sixth Edition13 The do while Statement (cont’d.) Figure 8-7 How to use the do while statement (cont’d.)

An Introduction to Programming with C++, Sixth Edition14 Figure 8-8 IPO chart information and C++ instructions for the Miller Incorporated program

An Introduction to Programming with C++, Sixth Edition15 The do while Statement (cont’d.) Figure 8-9 A sample run of the Miller Incorporated program

An Introduction to Programming with C++, Sixth Edition16 Nested Repetition Structures Like selection structures, repetition structures can be nested You can place one loop (the inner, or nested loop) inside another loop (the outer loop) Both loops can be pretest loops or posttest loops, or the two loops may be different types Programmer decides whether a problem requires a nested loop by analyzing the problem specification

An Introduction to Programming with C++, Sixth Edition17 Nested Repetition Structures (cont’d.) Figure 8-10 Problem specification and algorithm for signing one book for each customer

An Introduction to Programming with C++, Sixth Edition18 Nested Repetition Structures (cont’d.) Figure 8-11 Modified problem specification and algorithm for signing zero or more books for each customer

An Introduction to Programming with C++, Sixth Edition19 Nested Repetition Structures (cont’d.) Figure 8-12 Logic used by a clock’s minute and second hands

An Introduction to Programming with C++, Sixth Edition20 The Asterisks Program Simple program that prints asterisks to the screen in different patterns First version contains a single loop Second version contains a nested loop –Prints out three lines of five asterisks each using a nested for loop to print each line

An Introduction to Programming with C++, Sixth Edition21 The Asterisks Program (cont’d.) Figure 8-13 Problem specification IPO chart information and C++ instructions for the asterisks program

An Introduction to Programming with C++, Sixth Edition22 The Asterisks Program (cont’d.) Figure 8-13 IPO chart information and C++ instructions for the asterisks program (cont’d.)

An Introduction to Programming with C++, Sixth Edition23 The Asterisks Program (cont’d.) Figure 8-15 Sample run of the asterisks program Figure 8-14 Completed desk-check table for the asterisks program

An Introduction to Programming with C++, Sixth Edition24 The Asterisks Program (cont’d.) Figure 8-16 Problem specification IPO chart information and C++ instructions for the modified asterisks program

An Introduction to Programming with C++, Sixth Edition25 Figure 8-16 IPO chart information and C++ instructions for the modified asterisks program (cont’d.)

An Introduction to Programming with C++, Sixth Edition26 The Asterisks Program (cont’d.) Figure 8-17 Desk-check table and output after the nested loop’s cout statement is processed the first time Figure 8-18 Desk-check table and output after the nested loop’s cout statement is processed the second time

An Introduction to Programming with C++, Sixth Edition27 The Asterisks Program (cont’d.) Figure 8-19 Desk-check table and output after the nested loop’s cout statement is processed the third time Figure 8-20 Desk-check table and output after the nested loop’s cout statement is processed the fourth time

An Introduction to Programming with C++, Sixth Edition28 Figure 8-21 Desk-check table and output after the nested loop’s cout statement is processed the fifth time Figure 8-22 Current status of the desk-check table and output

An Introduction to Programming with C++, Sixth Edition29 The Asterisks Program (cont’d.) Figure 8-23 Desk-check table and output after the nested loop ends the second time

An Introduction to Programming with C++, Sixth Edition30 Figure 8-24 Desk-check table and output after the nested loop ends the third time The Asterisks Program (cont’d.)

An Introduction to Programming with C++, Sixth Edition31 The Asterisks Program (cont’d.) Figure 8-25 Sample run of the modified asterisks program

An Introduction to Programming with C++, Sixth Edition32 The Savings Calculator Program Calculate the value of a one-time deposit into a savings account after some period of time Program uses an exponential formula to calculate the total account value some number of years after the initial investment given the investment amount and the annual interest rate

An Introduction to Programming with C++, Sixth Edition33 The Savings Calculator Program (cont’d.) Figure 8-26 Problem specification and sample calculations for the savings calculator program

An Introduction to Programming with C++, Sixth Edition34 Figure 8-26 IPO chart and desk-check table for the savings calculator program

An Introduction to Programming with C++, Sixth Edition35 The pow Function The pow function is a convenient tool to raise a number to a power (exponentiation) The pow function raises a number to a power and returns the result as a double number Syntax is pow(x, y), in which x is the base and y is the exponent At least one of the two arguments must be a double Program must contain the #include directive to use the pow function

An Introduction to Programming with C++, Sixth Edition36 Figure 8-27 How to use the pow function

An Introduction to Programming with C++, Sixth Edition37 Coding the Savings Calculator Program Solution to the savings calculator problem and its corresponding C++ code (following slides) Code uses the pow function to calculate the total account value some number of years after the initial investment given a fixed annual interest rate of 3%

An Introduction to Programming with C++, Sixth Edition38 Figure 8-28 IPO chart information and C++ instructions for the savings calculator program

An Introduction to Programming with C++, Sixth Edition39 Figure 8-29 Savings calculator program

An Introduction to Programming with C++, Sixth Edition40 Coding the Savings Calculator Program (cont’d.) Figure 8-30 Sample run of the savings calculator program

An Introduction to Programming with C++, Sixth Edition41 Modifying the Savings Calculator Program Savings calculator program is modified to calculate the total account value for interest rates of 3%, 4%, and 5%

An Introduction to Programming with C++, Sixth Edition42 Modifying the Savings Calculator Program (cont’d.) Figure 8-31 Modified IPO chart information and C++ instructions

An Introduction to Programming with C++, Sixth Edition43 Figure 8-31 Modified IPO chart information and C++ instructions (cont’d.)

An Introduction to Programming with C++, Sixth Edition44 Figure 8-32 Modified savings calculator program

An Introduction to Programming with C++, Sixth Edition45 Modifying the Savings Calculator Program (cont’d.) Figure 8-33 Sample run of the modified savings calculator program

Summary A repetition structure can be either a pretest loop or a posttest loop In a pretest loop, the loop condition is evaluated before the instructions in the loop are processed In a posttest loop, the evaluation occurs after the instructions within the loop are processed Use the do while statement to code a posttest loop in C++ Use either the while statement or the for statement to code a pretest loop in C++ An Introduction to Programming with C++, Sixth Edition46

Summary (cont’d.) Repetition structures can be nested, which means one loop (called the inner or nested loop) can be placed inside another loop (called the outer loop) For nested repetition structures to work correctly, the entire inner loop must be contained within the outer loop You can use the built-in C++ pow function to raise a number to a power The pow function returns the result as a double An Introduction to Programming with C++, Sixth Edition47

An Introduction to Programming with C++, Sixth Edition48 Lab 8-1: Stop and Analyze Study the program in Figure 8-34 and answer the questions The program displays the total sales made in Region 1 and the total sales made in Region 2

Lab 8-2: Plan and Create An Introduction to Programming with C++, Sixth Edition49 Figure 8-35 Problem specification for Lab 8-2

Lab 8-3: Modify Modify the program in Lab 8-2 Change both loops to posttest loops Use the program to display the multiplication tables for the multiplicands 6, 9, and 2 An Introduction to Programming with C++, Sixth Edition50

An Introduction to Programming with C++, Sixth Edition51 Figure 8-41 Code for Lab 8-4 Lab 8-4: Desk-Check Desk-check the code in Figure 8-41 What will the code display on the computer screen?

Lab 8-5: Debug Follow the instructions for starting C++ and opening the Lab8-5.cpp file Debug the program An Introduction to Programming with C++, Sixth Edition52