Download Computer Science 1620

Document related concepts
no text concepts found
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

Problem

Analysis & Design

you are typically given a problem
statement




Solution
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?
Problem

One Solution (New Programmers)


Coding
Solution
start coding right away
generate a complete program right away

Problem
One Solution (New Programmers)



Code is compiled

Coding
Compiling
Solution
Errors
start coding right away
generate a complete program right away
compiler errors result in recoding

Problem
One Solution (New Programmers)



start coding right away
generate a complete program right away
Code is compiled

compiler errors result in recoding
Coding
Compiling
Execution
Solution
Errors

Code is tested

Errors
runtime errors result in recoding

Problem
One Solution (New Programmers)



start coding right away
generate a complete program right away
Code is compiled

compiler errors result in recoding
Coding

Compiling
Execution
Solution

Errors
Errors
Code is tested

runtime errors result in recoding
When no errors are tested, code is
considered a solution

Problem
Problem:


Coding
Compiling
Execution
Solution
Errors
Errors
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

before you write any code:

analyze the problem thoroughly


analysis
design the solution without any code

algorithm design
Problem
Analysis
These two steps, if done correctly,
will reduce the number of
corrections to be made here.
Algorithm Design
Coding
Compiling
Execution
Solution
Errors
Errors

Analysis (from text):
1.
2.
Thoroughly understand the problem
Understand the problem requirements.
Requirements can include:
1.
2.
3.
3.
whether the program requires interaction with the user
whether it produces output, what the output looks like
what types of data will be used
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?


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?


Yes: we will need to know the length and width of the rectangle
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.
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.

Analysis:

will this program require input from the user?




will this program require output?


Yes: The monthly salary total.
what will the format of the output be?


base salary
how long the person has worked at the store
what the monthly sales of the person was
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

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.
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?




will this program require output?


Yes: The yearly balance of the investment.
what will the format of the output be?


starting dollar amount
interest rate
withdraw amount
should probably restrict the output to 2 decimal places (currency)
what will the data types be:

everything is floating point.

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.
Can this step be translated to
of code?
perimeter ofone
theline
rectangle.
• not really
• let's decompose it into
simpler steps
area of the rectangle

Step 3: Compute the

Step 4: Compute the

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.



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
Can this step be translated to
one line from
of code?
rectangle
the user.

Step 2: Read the width of the

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.



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
Can this step be translated to
one line from
of code?
rectangle
the user.

Step 2: Read the width of the

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.



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.
Can this step be translated to
one line of code?

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.



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 ofCan
thethis
rectangle.
step be translated to


one line of code?
• not really
rectangle
• decompose into simpler
elements

Step 4: Compute the area of the

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.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;




Write program to compute the perimeter and area of a rectangle. The size of the
rectangle will be specified by the user.
#include <iostream>
using namespace std;
int main() {
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;
return 0;
}

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: ";
If the salesperson has been with the store
cin >> years;

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
double sales;
bonus is ";
$20 for each year of work.
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;
If the salesperson has been with the store
for five years or less, the bonus is $10 per
double sales;
cout << "Please enter the monthly sales of the employee: year
"; of work. If the salesperson has been
with the store for more than five years, the
cin >> sales;
bonus is $20 for each year of work.

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 4.2.1 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)
Step 4.1.1: Compute the time bonus as 10 times the years with the company
Step 4.2: Otherwise
Step 4.2.1 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 4.2.1 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 4.2.1 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;
Bonus will not be usable outside of the if
statement.
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




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;
If the sales made by the person are more than
$5000 but less than $10000, the salesperson
double bonus;
receives a 3% commission on sales. If the total
if (years <= 5)
sales by the person is at least $10000 the
bonus = 10.0 * years;
salesperson receives a 6% sales commission.

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.1: If sales are greater than or equal to 10000
Step 5.1.1 commission is 6% of sales
Step 5.2 otherwise if sales are over 5000 and less than 10000
Step 5.2.1 commission is 3% of sales
Step 5.3 otherwise
Step 5.3.1 commission is 0

Step 6: Sum the base salary, time bonus, and commission bonus of the employee






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 >= 10000.0)
commission = 0.06 * sales;
Step 5.2 otherwise if sales are over 5000 and less than 10000
Step 5.2.1 commission is 3% of sales
Step 5.3 otherwise
Step 5.3.1 commission is 0

Step 6: Sum the base salary, time bonus, and commission bonus of the employee







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 >= 10000.0)
commission = 0.06 * sales;
else if ( (sales < 10000) && (sales > 5000.0))
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 >= 10000.0)
commission = 0.06 * sales;
else if (sales > 5000.0)
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 >= 10000.0)
commission = 0.06 * sales;
else if (sales > 5000.0)
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 >= 10000.0)
commission = 0.06 * sales;
else if (sales > 5000.0)
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 >= 10000.0)
commission = 0.06 * sales;
else if (sales > 5000.0)
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 >= 10000.0)
commission = 0.06 * sales;
else if (sales > 5000.0)
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 <iostream>
#include <iomanip>
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 >= 10000.0)
commission = 0.06 * sales;
else if (sales > 5000.0)
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
Code
Design
read in a length from the
user

int length;
cout << "Please enter a length: ";
cin >> length;
certain statements can be combined
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 4.2.1 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 4.2.1 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 4.2.1 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 4.2.1 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 4.2.1 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 4.2.1 if the account has enough money to withdraw
Step 4.2.1.1 make the full withdrawal amount
Step 4.2.2 otherwise
Step 4.2.2.1 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 4.2.1.1 make the full withdrawal amount
Step 4.2.2 otherwise
Step 4.2.2.1 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 4.2.1.1 make the full withdrawal amount
Step 4.2.2 otherwise
Step 4.2.2.1 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 4.2.2 otherwise
Step 4.2.2.1 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 4.2.2 otherwise
Step 4.2.2.1 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 4.2.2.1 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 4.2.2.1 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 <iostream>
#include <iomanip>
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



the code is difficult to debug


remember: compiler errors can compound
(produce other compiler errors)
difficult to know where runtime errors are
Solution: incremental coding

Incremental Coding

Approach:
1.
2.
Translate a small part of your design to code
compile the program

3.
run the program


4.
fix any compiler errors
fix any runtime errors
use cout statements to identify errors
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 <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
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);
Once you are confident
the program is working,
you can delete your
dummy cout statements.
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 <iostream>
#include <iomanip>
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);
Once you are confident
the program is working,
you can delete your
dummy cout statements.
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;

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 <iostream>
#include <iomanip>
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 << "balance = " << balance << endl;
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;
}
#include <iostream>
#include <iomanip>
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;
}

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 <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
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
Related documents