CSC 101 Lecture Notes Week 5
Introduction to Functional Program Design
-- REVISED --
Figure 1: High-level design for change making program.
Figure 2: C++ design for change making program.
////
//
// This program makes change, given an amount of purchase and an amount
// tendered. The output is a total amount of change, followed by change in
// five denominations of money: dollars, quarters, dimes, nickels, and pennies.
// All inputs and outputs are in integer cents.
//
// The program performs two input validity checks and outputs an appropriate
// error message if either check fails. The validity checks are:
//
// (1) The inputs are both positive.
// (2) The amount tendered is greater than or equal to the purchase amount.
//
// The program performs multiple change-making transactions by continuing to
// accept input until the user enters a value of zero for the purchase amount.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 26par99
// Modified: 26apr99
//
////
#include <iostream.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
int change; // Total change due
int dollars; // Number of dollars in change
int quarters; // " " quarters " "
int dimes; // " " dimes " "
int nickels; // " " nickels " "
int pennies; // " " pennies " "
//
// Prompt for and input the amount of purchase, for the first transaction.
//
cout << "Input the amount of the purchase, in cents (enter 0 to stop): ";
cin >> purchase;
//
// Continue processing transactions until a purchase amount of 0 is
// entered.
//
while (purchase != 0) {
//
// Prompt for and input the amount tendered.
//
cout << "Input the amount tendered, in cents: ";
cin >> tendered;
cout << endl;
//
// Perform input validity checks.
//
if (tendered <= 0 || purchase <= 0) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " both inputs must be greater than zero."
<< endl << endl;
}
else if (tendered < purchase) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " the amount tendered is less than the amount of the purchase."
<< endl << endl;
}
else {
//
// Compute the total amount of change due, in cents.
//
change = tendered - purchase;
//
// Output the total change amount, followed by a blank line.
//
cout << "Total change due = " << change << endl
<< endl;
//
// Compute the number of dollars in change by dividing the total
// change by 100.
//
dollars = change / 100;
//
// Compute the remaining amount of change by subtracting the amount
// of change in dollars from the total change.
//
change = change - (dollars * 100);
//
// Proceed in the same way as for dollars with quarters, dimes,
// nickels, and pennies.
//
quarters = change / 25;
change = change - (quarters * 25);
dimes = change / 10;
change = change - (dimes * 10);
nickels = change / 5;
pennies = change - (nickels * 5);
//
// Output the results of the pieces of change computations.
//
cout << "Change in dollars through pennies is:" << endl
<< " " << dollars << " dollars" << endl
<< " " << quarters << " quarters" << endl
<< " " << dimes << " dimes" << endl
<< " " << nickels << " nickels" << endl
<< " " << pennies << " pennies" << endl << endl;
}
//
// Prompt for and input the amount of purchase for the next
// transaction.
//
cout << "Input the amount of the purchase, in cents (enter 0 to stop): ";
cin >> purchase;
}
return 0;
}
////
//
// This program makes change, given an amount of purchase and an amount
// tendered. The output is a total amount of change, followed by change in
// five denominations of money: dollars, quarters, dimes, nickels, and pennies.
// All inputs and outputs are in integer cents.
//
// The program performs two input validity checks and outputs an appropriate
// error message if either check fails. The validity checks are:
//
// (1) The inputs are both positive.
// (2) The amount tendered is greater than or equal to the purchase amount.
//
// The program performs multiple change-making transactions by continuing to
// accept input until the user enters a value of zero for the purchase amount.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 26par99
// Modified: 26apr99
//
////
#include <iostream.h>
#include "Boolean.h"
////
//
// Function ReadInput prompts for and reads an integer value from the
// terminal. The prompting message is input as a string. The input value is
// returned.
//
////
int ReadInput(
char* prompt // Prompting message
);
////
//
// Function CheckInputs performs two input validity checks on a given purchase
// amount and amount tendered. The validity checks are:
//
// (1) The inputs are both positive.
// (2) The amount tendered is greater than or equal to the purchase amount.
//
// If either check fails, an error message is output to the terminal.
//
////
Boolean CheckInputs(
int purchase, // Amount of purchase
int tendered // Amount tendered
);
////
//
// Function ComputeChange performs change-making computations, given a purchase
// amount and an amount tendered. The computations entail determining the
// amount of change in each of five denominations: dollars, quarters, dimes,
// nickels, and pennies. The denomination amounts are output in five separate
// lines to the terminal.
//
////
void ComputeChange(
int purchase, // Amount of purchase
int tendered // Amount tendered
);
////
//
// Function main performs change-making transactions until a purchase amount of
// zero is entered by the user.
//
////
int main() {
//
// Declare program variables to hold the purchase amount and amount
// tendered.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
//
// Prompt for and input the amount of purchase, for the first transaction.
//
purchase = ReadInput(
"Input the amount of the purchase, in cents (enter 0 to stop): ");
//
// Continue processing transactions until a purchase amount of 0 is
// entered.
//
while (purchase != 0) {
//
// Prompt for and input the amount tendered.
//
tendered = ReadInput("Input the amount tendered, in cents: ");
cout << endl;
//
// Perform input validity checks and proceed if checks succeed.
//
if (CheckInputs(purchase, tendered)) {
//
// Perform the change making computations and output the results.
//
ComputeChange(purchase, tendered);
}
//
// Prompt for and input the amount of purchase for the next
// transaction.
//
purchase = ReadInput(
"Input the amount of the purchase, in cents (enter 0 to stop): ");
}
return 0;
}
int ReadInput(char* prompt) {
int input;
cout << prompt;
cin >> input;
return input;
}
Boolean CheckInputs(int purchase, int tendered) {
Boolean result = TRUE; // Result of checks
//
// Check that both inputs are positive.
//
if (tendered <= 0 || purchase <= 0) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " both inputs must be greater than zero."
<< endl << endl;
result = FALSE;
}
//
// Check that tendered is >= purchase.
//
else if (tendered < purchase) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " the amount tendered is less than the amount of the purchase."
<< endl << endl;
result = FALSE;
}
return result;
}
void ComputeChange(int purchase, int tendered) {
//
// Declare function variables to hold total change amount and the amounts
// of each denomination of change, from dollars to pennies.
//
int change; // Total change due
int dollars; // Number of dollars in change
int quarters; // " " quarters " "
int dimes; // " " dimes " "
int nickels; // " " nickels " "
int pennies; // " " pennies " "
//
// Compute the total amount of change due, in cents.
//
change = tendered - purchase;
//
// Output the total change amount, followed by a blank line.
//
cout << "Total change due = " << change << endl
<< endl;
//
// Compute the number of dollars in change by dividing the total
// change by 100.
//
dollars = change / 100;
//
// Compute the remaining amount of change by subtracting the amount
// of change in dollars from the total change.
//
change = change - (dollars * 100);
//
// Proceed in the same way as for dollars with quarters, dimes,
// nickels, and pennies.
//
quarters = change / 25;
change = change - (quarters * 25);
dimes = change / 10;
change = change - (dimes * 10);
nickels = change / 5;
pennies = change - (nickels * 5);
//
// Output the results of the pieces of change computations.
//
cout << "Change in dollars through pennies is:" << endl
<< " " << dollars << " dollars" << endl
<< " " << quarters << " quarters" << endl
<< " " << dimes << " dimes" << endl
<< " " << nickels << " nickels" << endl
<< " " << pennies << " pennies" << endl << endl;
}
Details of C++ Functions
Figure 3: Change-making program design with input/output annotations.
Overall Program Comment Library Includes Comment for First Function Prototype for First Function . . . Comment for Nth Function Prototype for Nth Function Definition of Function main Definition of First Function . . . Definition of Nth Function
return Expressionwhere Expression is any legal expression of the same type as the return type of the function.
MakeChange(purchase, tendered);
orpurchase = ReadInput( ... );
if (CheckInputs(purchase, tendered)) { ...
More on General Design Principles