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

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

Calculate and print salary.
For each day
Calculate salary for the current day.
Print salary for the current day.
Convert value from pennies to dollars.
Print Value.
Update total earnings so far.
Print total earnings.
Convert value from pennies to dollars.
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
  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;


About this document ...

This document was generated using the LaTeX2HTML translator Version 98.1p1 release (March 2nd, 1998)

Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 -no_navigation -no_images -dir TEMPHTML designs.tex.

The translation was initiated by Roni Khardon on 2002-09-27

Roni Khardon