CSC 101 Lecture Notes Week 6
More on Functions
Program Scoping
// The output consists of a line reporting the total change due, followed by a // blank line, followed by zero or more lines of the form // // <amount> <denomination> // // where <amount> is how many of a given denomination is returned in change and // <denomination> is one of "dollar(s)", "quarter(s)", "dime(s)", "nickel(s)", // or "penny(ies)". If the amount for a given denomination is 0, then no line // for that denomination is output. If the amount is 1, then the value 1 and // the singular spelling of the denomination name are output. If the amount is // 2 or more, then the value and plural spelling of the denomination name are // output. // // The program performs multiple change-making transactions by continuing to // accept input until the user enters a value of zero for the purchase amount.
//
// 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;
After:
//
// Output the results of the pieces of change computations.
//
cout << "Change in dollars through pennies is:" << endl;
//
// If there are > 0 dollars in change, output the amount of dollars. Note
// the correct use of "dollar" vs "dollars".
//
if (dollars != 0) {
if (dollars == 1) {
cout << dollars << " dollar" << endl;
}
else {
cout << dollars << " dollars" << endl;
}
}
//
// Output quarters as for dollars.
//
if (quarters != 0) {
if (quarters == 1) {
cout << quarters << " quarter" << endl;
}
else {
cout << quarters << " quarters" << endl;
}
}
//
// Output dimes as for dollars.
//
if (dimes != 0) {
if (dimes == 1) {
cout << dimes << " dime" << endl;
}
else {
cout << dimes << " dimes" << endl;
}
}
//
// Output nickels as for dollars.
//
if (nickels != 0) {
if (nickels == 1) {
cout << nickels << " nickel" << endl;
}
else {
cout << nickels << " nickels" << endl;
}
}
//
// Output pennies as for dollars.
//
if (pennies != 0) {
if (pennies == 1) {
cout << pennies << " penny" << endl << endl;
}
else {
cout << pennies << " pennies" << endl << endl;
}
}
////
//
// Function OutputDenomination outputs the amount of a particular denomination
// to the terminal, if the amount is non-zero. The inputs are the integer
// amount of the denomination, a string for the singular spelling of the
// denomination name, and a string for the plural spelling of the denomination
// name.
//
////
void OutputDenomination(
int amount, // Total amount of change due
char* singular_name, // Singular spelling of denomination
char* plural_name // Plural spelling of denomination
);
//
// Output the results of the pieces of change computations.
//
OutputDenomination(dollars, "dollar", "dollars");
OutputDenomination(quarters, "quarter", "quarters");
OutputDenomination(dimes, "dime", "dimes");
OutputDenomination(nickels, "nickel", "nickels");
OutputDenomination(pennies, "penny", "pennies");
void OutputDenomination(int amount, char* singular_name, char* plural_name) {
if (amount != 0) {
if (amount == 1) {
cout << amount << " " << singular_name << endl;
}
else {
cout << amount << " " << plural_name << 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);
////
//
// Function ComputeDenomination computes the amount of change due in a
// particular denomination and updates the change due by subtracting the
// computed amount. The inputs are an empty integer amount variable, an
// integer total amount variable, and the integer value of the denomination.
// The outputs are the computed amount in the first parameter and the updated
// change in the second parameter.
//
////
void ComputeDenomination(
int& amount, // Amount of denomination
int& change, // Total change due
int denomination_value // Value in cents of the denomination
);
//
// Compute the number of each denomination of change and decrement the
// change due by the amount computed each time.
//
ComputeDenomination(dollars, change, 100);
ComputeDenomination(quarters, change, 25);
ComputeDenomination(dimes, change, 10);
ComputeDenomination(nickels, change, 5);
pennies = change;
void ComputeDenomination(int& amount, int& change, int denomination_value) {
amount = change / denomination_value;
change = change - (amount * denomination_value);
}
//// // // Given an old document, make the following changes and produce a new // document. The changes are ... . // //// Document UpdateDocument(Document old_doc);
//// // // Given two old document, make the following editorial changes and return the // marked up documents. The changes are ... . // //// void MarkupDocuments(Document& old_doc1, Document& old_doc2);
//// // // Produce a new document, containing the following information ... . // //// Document ProduceNewDocument();
//// // // Give an oral presentation on the following material ... . // //// void GiveOralPresentation();
"Sometimes it's useful for documentation purposes to supply names for the parameters [in a function prototype}."