This is an example of how to write a program. The method is called *top-down programming* because you start with the goal (the top), and break it down into a series of steps (computations) that will achieve that goal.

The goal is a statement of what the program is to do.

*Goal*: write a program to determine how many quarters, dimes, nickels, and pennies make up a given amount of change

Next, we say what the inputs and outputs of the program are:

*Inputs*: User enters an amount as an integer

*Outputs*: Program prints number of quarters, dimes, nickels, and pennies that make up the given amount

Finally, we give a high-level approach for doing all this:

*High-level design*:

- read in amount
- figure out how may quarters are in the amount
- determine how much is left over from this
- figure out how many dimes are in what’s left over
- determine how much is left over from this
- figure out how many nickels are in what’s left over
- what’s left is the number of pennies

First, figure out how we want to represent the data on the computer:

*Data Representation*: Represent the amount as an integer

Now work out the structure of the program; in what follows, note how the steps in the structure correlate to the high-level design:

*Program Structure*:

- Read in the input and save it
- Divide by 25 to get the number of quarters
- Get the remainder
- Divide by 10 to get the number of dimes
- Get the remainder
- Divide by 5 to get the number of nickels
- Get the remainder
- Print the original input, number of quarters, dimes, nickels, and pennies

Now, we refine the structure further, and continue to do so
until we are comfortable that there is enough *explicit* detail to
allow us to write the program from the refinement:

*Refine Algorithm*:

- ask the user for the amount, read it and store it in integer variables
*amount*and*leftover* - divide
*leftover*by 25 to get the number of quarters*numquarters* - take the remainder of
*leftover*when divided by 25 to get the new integer*leftover* - divide
*leftover*by 10 to get the number of dimes*numdimes* - take the remainder of
*leftover*when divided by 10 to get the new integer*leftover* - divide
*leftover*by 5 to get the number of nickels*numnickels* - take the remainder of
*leftover*when divided by 5 to get the new integer*leftover* - this is the number of pennies
*numpennies* - print
*amount*"cents is"*numquarters*"quarters,"*numdimes*"dimes,"*numnickels*"nickels, and"*numpennies*"pennies"

There is one very important item missing — the error checking. We will add that near the end,
as it requires an **if** statement, and we have not covered those yet.

scanf("%d", &amount); leftover = amount;

numquarters = leftover / 25;

leftover = leftover % 25;

numdimes = leftover / 10;

leftover = leftover % 10;

numnickels = leftover / 5;

leftover = leftover % 5;

numpennies = leftover;

printf("%d cents is %d quarters, %d dimes, %d nickels, and %d pennies\n",

amount, numquarters, numdimes, numnickels, numpennies);

I omitted the header comments. You can look on SmartSite for the real program and see the comments.

#include <stdio.h>

#include <stdlib.h>

int main()

{

int amount; /* how much change the user has */

int leftover; /* used to hold amount as it decreases */

int numquarters; /* how many quarters */

int numdimes; /* how many dimes */

int numnickels; /* how many nickels */

int numpennies; /* how many pennies */

/* read in the amount and save it */

printf("Enter amount of change: ");

scanf("%d", &amount); leftover = amount;

/* computer the number of quarters and how much is left over */

numquarters = leftover / 25;

leftover = leftover % 25;

/* computer the number of dimes and how much is left over */

numdimes = leftover / 10;

leftover = leftover % 10;

/* computer the number of nickels and how much is left over */

numnickels = leftover / 5;

leftover = leftover % 5;

/* whatever is left over is the number of pennies */

numpennies = leftover;

/* print the result */

printf("%d cents is %d quarters, %d dimes, %d nickels, and %d pennies\n",

amount, numquarters, numdimes, numnickels, numpennies);

/* thank you, and goodnight! */

return(EXIT_SUCCESS);

}

What happens if the user types “a dollar” to the input?
That’s not a number.
So, what happens is *scanf* doesn’t find an integer,
and so it leaves the value of *amount* alone.
When I ran the program, it happened to contain 4195472, so my output was:

The problem is that4195472 cents is 167818 quarters, 2 dimes, 0 nickels, and 2 pennies

That function returns the number of items read, so here *scanf* should return 1.
If it returns anything else, an error occurred. So let’s use an **if** statement to
check for that. Change the line containing *scanf* to:

if (scanf("%d", &amount) != 1){

/* oops ... bad input! */

printf("Amount must be nonnegative integer\n");

return(EXIT_FAILURE);

}

leftover = amount;

Now when I typed the same input, I got:

Amount must be nonnegative integer

Error checking is an integral part of any program. Normally, figure out how to do it when you write a statement that might cause a problem
(here, the *scanf* line). In fact, we haven’t done a thorough job of error checking (for example, what happens if the user enters a negative integer or a floating point number), but this is a good start.

Also, we need to tell the user what to enter, because what is required may not be clear from the name of the program.
So, above the *scanf* line, add this:

/* prompt the user for input */

printf("Amount of change to convert to coins: ");

Finally, we should change the output to handle units of 1 coin (make the coin name singular) and 0 coins (omit the number of coins and coin name entirely), but that’s a good homework or extra credit question!

You can also obtain a PDF version of this. | Version of October 2, 2015 at 9:12AM |