COMP 11 - Introduction to Computer Science - Fall 2002

Program Designs

The design for a program is simply an English-language outline of how you program will perform its task. Writing a program design requires you to think about and organize your program before you write it. Experience says that this saves a lot of time and effort when compared to trying to write your program directly.

Our textbook presents designs using hierarchical charts. We ask you to write simple text for your designs. To get a similar effect showing the hierarchical structure of the design simply use indentation.

Here is an example design for programming challenge 5.3 (page 311) of your textbook:

Write a program that calculates how much a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continued to double every day. The program should ask the user for the number of days, display a table showing how much the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies.
Input Validation: Do not accept a number less than one for the number of days worked.

The Design

1.
Input number of days
(a)
(b)
Validate that the number is valid (>=1), looping as necessary.

2.
Calculate and print salary.
(a)
For each day
i.
Calculate salary for the current day.
ii.
Print salary for the current day.
a.
Convert value from pennies to dollars.
b.
Print Value.
iii.
Update total earnings so far.
(b)
Print total earnings.
i.
Convert value from pennies to dollars.
ii.
Print Value.

The program is given on the next page. Notice how some of the text of the design can be used as comments in your program.

```// File: designs.cpp
// Solves programming Challenge 5.3: pennies for the day

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int days,          // number of days
cur_day,         // current day number
cur_sal,         // current day salary
total;           // total salary
float dollars;     // the dollar amount equivalent

// Input number of days
do {
cout << "Please enter the number of days: ";
cin >> days;
} while (days <1);

// Initialize values
cur_sal=1;
total=0;
cout << setiosflags(ios::fixed);
cout << setprecision(2);

// Calculate and print salary
for (cur_day=1; cur_day <= days; cur_day++) {

// Calculate and print salary for the current day
dollars = cur_sal/100.0;
cout << "Salary for day " << setw(5) << cur_day;
cout << " is " << setw(10) << dollars << endl;

// Update total earnings so far
total += cur_sal;

cur_sal *=2;  // next day's salary amount is prepared here
}

// Print total earnings
dollars = total/100.0;
cout << "Total Salary        ";
cout << " is " << setw(10) << dollars << endl;

return 0;

}
```