Computer Science 1620 Programming & Problem Solving.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
This set of slides is provided by the author of the textbook1 Introductory Topics (Continued) l Computer Components l Basic Control Structures l Problem.
Computer Science 1620 Loops.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
Chapter 1: An Overview of Computers and Programming Languages
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 1: An Overview of Computers and Programming Languages.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
CHAPTER 1: AN OVERVIEW OF PROGRAMMING INSTRUCTOR: MOHAMMAD MOJADDAM How to Program in C++
Software Engineering 1 (Chap. 1) Object-Centered Design.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 4 Selection Structures: Making Decisions.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Overview of Programming and Problem Solving Textbook Chapter 1 1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 3: Assignment, Formatting, and Interactive Input.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
22/11/ Selection If selection construct.
1 Original Source : and Problem and Problem Solving.ppt.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
1 Original Source : and Problem and Problem Solving.ppt.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
A First Book of C++ Chapter 4 Selection.
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to Computer Programming
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 2 Assignment and Interactive Input
Chapter 1: An Overview of Computers and Programming Languages
Computing Fundamentals
Let’s all Repeat Together
Life is Full of Alternatives
Chapter 5: Control Structures II (Repetition)
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Computer Science 1620 Programming & Problem Solving

Programming is the science (art) of solving a problem using a computer program one of the greatest challenges involves translation: problem is stated in human language solution is in computer language how do we make this translation?

Programming programming efficiency can be improved through three steps pre-coding analysis and design structured programming incremental programming

Analysis & Design you are typically given a problem statement often, from a client with no programming background this means the description will be far from C++ you must somehow develop a solution to this problem using C++ what steps do you take? Solution Problem

One Solution (New Programmers) start coding right away generate a complete program right away Solution Problem Coding

One Solution (New Programmers) start coding right away generate a complete program right away Code is compiled compiler errors result in recoding Solution Problem Coding Compiling Errors

One Solution (New Programmers) start coding right away generate a complete program right away Code is compiled compiler errors result in recoding Code is tested runtime errors result in recoding Solution Problem Coding Compiling Execution Errors

One Solution (New Programmers) start coding right away generate a complete program right away Code is compiled compiler errors result in recoding Code is tested runtime errors result in recoding When no errors are tested, code is considered a solution Solution Problem Coding Compiling Execution Errors

Problem: many errors are not a result of programming error, but rather with a flaw in the program logic many of these errors could be avoided if the program was designed more carefully Solution Problem Coding Compiling Execution Errors

Solution before you write any code: analyze the problem thoroughly analysis design the solution without any code algorithm design

Solution Problem Coding Compiling Execution Errors Analysis Algorithm Design These two steps, if done correctly, will reduce the number of corrections to be made here.

Analysis (from text): 1. Thoroughly understand the problem 2. Understand the problem requirements. Requirements can include: 1. whether the program requires interaction with the user 2. whether it produces output, what the output looks like 3. what types of data will be used 3. If the problem is complex, it may need to be divided into smaller problems (divide and conquer). Analysis needs to be applied to each step.

Algorithm Design a step by step solution of what the program will do NOT WRITTEN IN CODE use pseudocode, or an English description of what you want your program to do your pseudocode will progressively get closer and closer to real code

Example (Text): A program to compute the perimeter and area of a rectangle. The length and width of the rectangle will be specified by the user, as an integer. Analysis: will this program require input from the user? Yes: we will need to know the length and width of the rectangle will this program require output? Yes: we will need to display the area and perimeter of the rectangle. what will the format of the output be? int, since the inputs are ints what will the data types be: int

Design Step 1: Read the length of the rectangle from the user. Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Example (Text): Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with the store for five years or less, the bonus is $10 for each year that he or she has worked there. If the salesperson has been with the store for more than five years, the bonus $20 for each year that he or she has worked there. The salesperson can earn an additional bonus through commission: if the sales made by the person are more than $5000 but less than $10000, the salesperson receives a 3% commission on sales. If the total sales by the person is at least $10000 the salesperson receives a 6% sales commission. Write a program to calculate the monthly paycheque for an employee.

Analysis: will this program require input from the user? base salary how long the person has worked at the store what the monthly sales of the person was will this program require output? Yes: The monthly salary total. what will the format of the output be? should probably restrict the output to 2 decimal places (currency) what will the data types be: base salary: floating point time with store: integer or floating point monthly sales: floating point salary total: floating point Example (Text): Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with the store for five years or less, the bonus is $10 for each year that he or she has worked there. If the salesperson has been with the store for more than five years, the bonus $20 for each year that he or she has worked there. The salesperson can earn an additional bonus through commission: if the sales made by the person are more than $5000 but less than $10000, the salesperson receives a 3% commission on sales. If the total sales by the person is at least $10000, the salesperson receives a 6% sales commission. Write a program to calculate the monthly salary for an employee.

Design Step 1: Read the base salary of the employee. Step 2: Read the number of years employee has been with company Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Example: Suppose you invest X dollars into an investment at Y%. Suppose further that every 4 years, you withdraw Z dollars from the investment. However, you may not withdraw more than the investment is currently worth. Write a program that computes and outputs the value of the investment for 20 years.

Analysis: will this program require input from the user? starting dollar amount interest rate withdraw amount will this program require output? Yes: The yearly balance of the investment. what will the format of the output be? should probably restrict the output to 2 decimal places (currency) what will the data types be: everything is floating point. Example: Suppose you invest X dollars into an investment at Y%. Suppose further that every 4 years, you withdraw Z dollars from the investment. However, you may not withdraw more than the investment is currently worth. Write a program that computes and outputs the value of the investment for 20 years.

Design Step 1: Read the starting balance of the investment Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output account balance for the next twenty years

Structured Programming "divide and conquer programming" a methodology for turning design into code Process: for each step in your design if the step can be translated into a line of code, then replace it with that line of code otherwise, decompose that step into smaller, simpler steps repeat until every step has been converted to code

Design (area and perimeter calculation) Step 1: Read the length of the rectangle from the user. Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) Step 1: Read the length of the rectangle from the user. Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen. Can this step be translated to one line of code? not really let's decompose it into simpler steps

Design (area and perimeter calculation) Step 1.1 Declare storage for the length Step 1.2 Prompt the user for a length Step 1.3 Read the entered value into defined storage Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) Step 1.1 Declare storage for the length Step 1.2 Prompt the user for a length Step 1.3 Read the entered value into defined storage Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen. Can this step be translated to one line of code?

Design (area and perimeter calculation) int length; Step 1.2 Prompt the user for a length Step 1.3 Read the entered value into defined storage Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; Step 1.2 Prompt the user for a length Step 1.3 Read the entered value into defined storage Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen. Can this step be translated to one line of code?

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; Step 1.3 Read the entered value into defined storage Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; Step 1.3 Read the entered value into defined storage Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen. Can this step be translated to one line of code?

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; Step 2: Read the width of the rectangle from the user. Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen. Can this step be translated to one line of code? not really decompose into simpler elements

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; Step 2.1 Declare storage for the width Step 2.2 Prompt the user for a width Step 2.3 Read the entered value into defined storage Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; Step 3: Compute the perimeter of the rectangle. Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); Step 4: Compute the area of the rectangle Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); int area = length * width; Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); int area = length * width; Step 5: Output the results of this computation to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); int area = length * width; Step 5.1: Output the perimeter to the screen Step 5.2: Output the area to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); int area = length * width; cout << "The perimeter of the rectangle is " << perimeter << endl; Step 5.2: Output the area to the screen.

Design (area and perimeter calculation) int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); int area = length * width; cout << "The perimeter of the rectangle is " << perimeter << endl; cout << "The area of the rectangle is " << area << endl;

#include using namespace std; int main() { return 0; } Write program to compute the perimeter and area of a rectangle. The size of the rectangle will be specified by the user. int length; cout << "Please enter the rectangle's length: "; cin >> length; int width; cout << "Please enter the rectangle's width: "; cin >> width; int perimeter = 2 * (length + width); int area = length * width; cout << "The perimeter of the rectangle is " << perimeter << endl; cout << "The area of the rectangle is " << area << endl;

Design (salary program) Step 1: Read the base salary of the employee. Step 2: Read the number of years employee has been with company Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) Step 1: Read the base salary of the employee. Step 2: Read the number of years employee has been with company Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) Step 1.1 Declare storage for the base Step 1.2 Prompt the user for a base salary Step 1.3 Read the entered value into defined storage Step 2: Read the number of years employee has been with company Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; Step 2: Read the number of years employee has been with company Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; Step 2: Read the number of years employee has been with company Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; Step 2.1 Declare storage for employee # of years Step 2.2 Prompt the user for the employee's # of years Step 2.3 Read the entered value into defined storage Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; Step 3: Read the monthly sales of the employee Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; Step 3.1 Declare storage for monthly sales Step 3.2 Prompt the user for the monthly sales of the employee Step 3.3 Read the entered value into defined storage Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; Step 4: Calculate the time bonus of the employee Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total If the salesperson has been with the store for five years or less, the bonus is $10 per year of work. If the salesperson has been with the store for more than five years, the bonus is $20 for each year of work.

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; Step 4.1: If the years with the company is less than or equal to 5 Step 4.1.1: Compute the time bonus as 10 times the years with the company Step 4.2: Otherwise Step Compute the time bonus as 20 times the years with the company Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total If the salesperson has been with the store for five years or less, the bonus is $10 per year of work. If the salesperson has been with the store for more than five years, the bonus is $20 for each year of work.

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; if (years <= 5) Step 4.1.1: Compute the time bonus as 10 times the years with the company Step 4.2: Otherwise Step Compute the time bonus as 20 times the years with the company Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; if (years <= 5) double bonus = 10.0 * years; Step 4.2: Otherwise Step Compute the time bonus as 20 times the years with the company Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; if (years <= 5) double bonus = 10.0 * years; else Step Compute the time bonus as 20 times the years with the company Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; if (years <= 5) double bonus = 10.0 * years; else double bonus = 20.0 * years; Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total Bonus will not be usable outside of the if statement.

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; Step 5: Calculate the commission bonus of the employee Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total If the sales made by the person are more than $5000 but less than $10000, the salesperson receives a 3% commission on sales. If the total sales by the person is at least $10000 the salesperson receives a 6% sales commission.

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; Step 5.1: If sales are greater than or equal to Step commission is 6% of sales Step 5.2 otherwise if sales are over 5000 and less than Step commission is 3% of sales Step 5.3 otherwise Step commission is 0 Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; Step 5.2 otherwise if sales are over 5000 and less than Step commission is 3% of sales Step 5.3 otherwise Step commission is 0 Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if ( (sales )) commission = 0.03 * sales; else commission = 0; Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if (sales > ) commission = 0.03 * sales; else commission = 0; Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if (sales > ) commission = 0.03 * sales; else commission = 0; Step 6: Sum the base salary, time bonus, and commission bonus of the employee Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if (sales > ) commission = 0.03 * sales; else commission = 0; double salary = base + bonus + commission; Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if (sales > ) commission = 0.03 * sales; else commission = 0; double salary = base + bonus + commission; Step 7: Output the computed total

Design (salary program) double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if (sales > ) commission = 0.03 * sales; else commission = 0; double salary = base + bonus + commission; cout << setprecision(2) << fixed << showpoint; cout << "Your total salary for the month is $" << salary << endl;

#include using namespace std; int main() { double base; cout << "Please enter the employee's monthly base salary: "; cin >> base; int years; cout << "Please enter employee's years with the company: "; cin >> years; double sales; cout << "Please enter the monthly sales of the employee: "; cin >> sales; double bonus; if (years <= 5) bonus = 10.0 * years; else bonus = 20.0 * years; double commission; if (sales >= ) commission = 0.06 * sales; else if (sales > ) commission = 0.03 * sales; else commission = 0; double salary = base + bonus + commission; cout << setprecision(2) << fixed << showpoint; cout << "Your total salary for the month is $" << salary << endl; return 0; }

As you gain experience, your translation will become more efficient you will be able to translate one design statement into multiple lines of code certain statements can be combined int length; cout << "Please enter a length: "; cin >> length; read in a length from the user Design Code double salary = base + bonus; cout << salary << endl; cout << base + bonus << endl;

Design Step 1: Read the starting balance of the investment Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) Step 1: Read the starting balance of the investment. Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; Step 4: Repeat twenty times Step 4.1 Add the interest to the investment balance Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account Indent to show that all these steps are part of a statement block.

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { Step 4.1 Add the interest to the investment balance Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { Step 4.1 Add the interest to the investment balance Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance = balance + (balance * interest); Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance += (balance * interest); Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); Step 4.2 If this is a withdrawal year Step remove withdrawal from balance Step 4.3 Output the balance of the account } "Suppose further that every 4 years, you withdraw Z dollars from the investment."

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (this is a withdrawal year) Step remove withdrawal from balance Step 4.3 Output the balance of the account } This needs to be true every fourth year, and false otherwise.

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; int count = 0; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); count++; if (count == 4) { Step remove withdrawal from balance count == 0; } Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) Step remove withdrawal from balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) Step remove withdrawal from balance Step 4.3 Output the balance of the account } "However, you may not withdraw more than the investment is currently worth."

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) Step if the account has enough money to withdraw Step make the full withdrawal amount Step otherwise Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) Step make the full withdrawal amount Step otherwise Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) Step make the full withdrawal amount Step otherwise Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; Step otherwise Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; Step otherwise Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else Step withdraw the entire balance Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance -= balance; Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; Step 4.3 Output the balance of the account }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << fixed << showpoint << setprecision(2); cout << "Balance after year " << year << " = $" << balance << endl; }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; }

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

From Design to Code Hints: certain keywords hint at a selection structure "if" if this is a withdrawal year "whether" determine whether employee has been with store for more than 10 years

From Design to Code Hints: certain keywords hint at a loop while, for while an amount is less than 2000 repeat repeat for twenty years each output each value between 0 and 5

Incremental Coding programmers often try to code the entire solution to a problem immediately when the code is compiled, there are many compiler errors remember: compiler errors can compound (produce other compiler errors) the code is difficult to debug difficult to know where runtime errors are Solution: incremental coding

Incremental Coding Approach: 1. Translate a small part of your design to code 2. compile the program fix any compiler errors 3. run the program fix any runtime errors use cout statements to identify errors 4. repeat steps 1-3 until your program is written

Design (investment program) Step 1: Read the starting balance of the investment. Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

The previous compiles with no errors How do we check for runtime errors Many programming IDEs have a debugger run in debug mode as the program executes, debugger shows instructions being executed step by step Without debugger: Use a dummy cout statement The cout statement will echo the value of the programmer's input Once you are sure the program is reading the variable correctly, you can delete the cout statement

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; cout << balance << endl; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

Is this really necessary? suppose that the programmer had accidentally used an int instead of a double

#include using namespace std; int main() { int balance; cout << "Please enter the starting balance of investment: "; cin >> balance; cout << balance << endl; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; cout << balance << endl; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; } Once you are confident the program is working, you can delete your dummy cout statements.

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; } Once you are confident the program is working, you can delete your dummy cout statements.

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; Step 2: Read the interest rate of the investment Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; Step 3: Read the withdraw amount of the investment Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; Step 4: Output balance for the next twenty years

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << "balance = " << balance << endl; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << "balance = " << balance << endl; cout << "interest = " << interest << endl; cout << "withdrawal = " << withdrawal << endl; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; Step 4: Output balance for the next twenty years

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; Step 4: Repeat twenty times Step 4.1 Add the interest to the investment balance Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account

Design (investment program) double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; for (int year = 1; year <= 20; year++) { Step 4.1 Add the interest to the investment balance Step 4.2 If this is a withdrawal year, remove withdrawal from balance Step 4.3 Output the balance of the account }

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { balance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

#include using namespace std; int main() { double balance; cout << "Please enter the starting balance of investment: "; cin >> balance; double interest; cout << "Please enter the interest rate of investment: "; cin >> interest; double withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; cout << fixed << showpoint << setprecision(2); for (int year = 1; year <= 20; year++) { cout << "year = " << year << endl; alance *= (1 + interest); if (year % 4 == 0) if (balance >= withdrawal) balance -= withdrawal; else balance = 0; cout << "Balance after year " << year << " = $" << balance << endl; } return 0; }

Now we know the loop statement executes 20 times.

And so on dummy cout statements can be used in other statements as well conditionals tells you whether the statement of an if is executing when you expect it to functions tells you whether a function is being called correctly

Summary Programming and Problem Solving Analysis and Program Design Structured Programming Divide and Conquer Incremental Programming