The Change Machine Project


My mother was not above using a change machine to distract cranky or mischievous young grandchildren. The youngsters poured hundreds of pennies into the top of the machine, and watched with fascination (fortunately, youngsters are easily fascinated) as the machine sorted the pennies into amounts of change that could be taken to the bank and exchanged for dollars, quarters, and bigger loot. The youngsters were motivated as well as fascinated, since guess who got to keep the quarters ?

Program Description

This program will ask the user to input the number of pennies. You may assume the user will input a positive whole number. The code then will output the number of dollars, quarters, dimes, nickels, and pennies. The input and output could be

 Enter number of pennies to make change for: 387 Dollars: 3 Quarters: 3 Dimes: 1 Nickels: 0 Pennies: 2 

The next section will reproduce the code, and the section following will explain the code. However, as a programming challenge, first try to write the code yourself. If you can, great! If not, no problem; you still will learn more from the code and the explanation if you first try to write this program.

As a hint (you dont have to look), here are the first three lines of code in main:

 int total, dollars, quarters, dimes, nickels, leftover;  cout << "Enter number of pennies to make change for: ";  cin >> total; 

The variable total will be assigned the total number of pennies entered by the user. The variable dollar will be assigned the number of dollars in the pennies, 3 in the preceding sample run for 387 total pennies. The variables quarters, dimes, and nickel s will be assigned the number of quarters, dimes, and nickels in the change, 3, 1, and 0, respectively, in the previous sample run for 387 total pennies. The variable leftover ultimately will be assigned the number of pennies in the change (2 in the prior sample run for 387 total pennies), but also will be used for other purposes. Of course, you could write this program with a few more, or a few less, variables.

The Code

There is more than one way to write this program. Here is how I wrote it:

 #include <iostream> #include <string> using namespace std; int main(void) {  int total, dollars, quarters, dimes, nickels, leftover;  cout << "Enter number of pennies to make change for: ";  cin >> total;  dollars = total / 100;  leftover = total % 100;  quarters = leftover / 25;  leftover %= 25;  dimes = leftover / 10;  leftover %= 10;  nickels = leftover / 5;  leftover %= 5;  cout << "Dollars: " << dollars << endl;  cout << "Quarters: " << quarters << endl;  cout << "Dimes: " << dimes << endl;  cout << "Nickels: " << nickels << endl;  cout << "Pennies: " << leftover << endl;  return 0; } 

The Algorithm

You learned in Chapter 1 that in a computer program a computer programmer gives instructions to a computer. These instructions are in a programming language such as C++. However, before you can write code, you need to formulate the instructions in English or whatever other language you think in.

An algorithm, pronounced Al Gore rhythm, is a step-by-step logical procedure for solving a problem. You frequently will need to create and implement algorithms. Implementing algorithms in your code is computer programming. Creating algorithms is a skill that can be developed from any field that requires analytical thinking, including, but not limited to, mathematics as well as computer programming.

Lets say you were given a number of pennies, such as 387, and you had to determine, in your head, how many dollars, quarters, dimes, nickels, and pennies to give as change. How would you do it?

A logical approach is to start with dollars. There are 100 pennies in a dollar. If 387 is divided by 100, the quotient is the number of dollars in the pennies: 3.

One problem is that 387 divided by 100 could be 3.87, not 3. However, as discussed earlier in this chapter in the Division Operators section, when an integer is divided by an integer, then the result always is an integer unless one of the integer operands first is cast to a float. We want the result of the division to be an integer, so we will not cast either of the integer operands to a float.

Since the beginning number of pennies (387) is stored in the integer variable total, if total is divided by 100, also regarded as an integer, the quotient is the number of dollars in the pennies, 3.

 dollars = total / 100; 

After you take out 300 pennies (3 dollars) from the pile of 387 pennies, 87 pennies are left over. 87 is the remainder of the division total / 100. We obtain this remainder with the modulus operator, and assign it to the integer variable leftover:

 leftover = total % 100; 

Next, you follow the same procedure to determine the number of quarters in the 87 pennies left over. The only differences are that the divisor is 25 instead of 100 and the number of pennies left is represented by leftover instead of total.

 quarters = leftover / 25;  leftover %= 25; 

The same process is followed for determining the number of dimes and nickels:

 dimes = leftover / 10;  leftover %= 10;  nickels = leftover / 5;  leftover %= 5; 

The number of pennies left over after division by 5 cannot be converted into higher change. Accordingly, there is no need for further division. There also is no need for a separate variable for pennies because leftover stores the number of pennies left.

All that remains is to output the values of the variables representing the dollars, quarters, dimes, nickels, and pennies.

This method of solving the problem by dividing the total number of pennies by the number of pennies in a dollar, storing the quotient in a variable holding the number of dollars, and dividing the remainder by the number of pennies in a quarter and so on, is an algorithm. We will be discussing many algorithms in this book.




C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net