tags:

views:

425

answers:

6

Im taking C programming at class and Im doing practice questions in the book.

One of the questions is "Write a program that asks the user to enter a U.S. dollar amount and then shows how to pay that amount using the smallest number of $20, $10, $5, %1 bills:

The sample is as follows, then what I came up with, followed by my dilemna. You may notice something right away that I may have overlooked when trying my code.

Enter a dollar amount: 93 (user inputted)

$20 bills: 4 $10 bills: 1 $5 bills: 0 $1 bills: 3

what I have so far is:


#include <stdio.h>

int main (void) 
{
 int cash;

 printf("Enter a dollar amount: ");
 scanf("%d", &cash);

 printf("$20 bills = %d\n", cash / 20);
 printf("$10 bills = %d\n", cash / 10);
 printf("$5 bills = %d\n", cash / 5);
 printf("$1 bills = %d\n", cash / 1);
 return 0;
}


Now the dilemma, the book suggests dividing the input number (93) by 20, and since im using "int" instead of "float" that leaves 4 instead of 4.65. Then it suggests reducing the result of that times 20, from 93. leaving 13, and repeating that for each one. so it would be 93/20=4 13/10=1 3/1=3

I cant figure out for the life of me how to get this line printf("$10 bills = %d\n", cash / 10); to recognize the value left by the previous line printf("$20 bills = %d\n", cash / 20);

I had originally tried to put most of it on the 20 bill line. like cash / 20 *20 but it just displayed 80 on that line instead of the next line.

Thanks in advance for any help.

+1  A: 

You may have to use the modulo operator (%) which returns the remainder of the division of two numbers (for example, 93 % 20 equals 13).

For example :

int cash = 93;
int twenty, ten, five, one;

twenty = cash / 20;
cash = cash % 20;

ten = cash / 10;
cash = cash % 10;

/* etc. */
Pierre Bourdon
+9  A: 

I think you are mistaken a bit here. As you said, values are not left by the previous printf. You need to update the value of the variable cash. Here is what could help:

#include <stdio.h>

int main (void) 
{
    int cash;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    printf("$20 bills = %d\n", cash / 20);
    cash = cash % 20;
    printf("$10 bills = %d\n", cash / 10);
    cash = cash % 10;
    printf("$5 bills = %d\n", cash / 5);
    cash = cash % 5;
    printf("$1 bills = %d\n", cash);
    return 0;
}

The modulo (%) operator gives you back the remainder after performing the division. Hence:

93 % 20 = 13
13 % 10 = 3

and so on.

The module (%) operator gives you back the remainder after performing the division. Hence, 93 % 20 = 13, 13 % 10 = 3 and so on..
@FooBar, added that to the answer (where it belongs).
paxdiablo
To pick a nit: if the amount entered is 7, you'll report $20 bills = 0, $10 bills = 0, ...?
Jonathan Leffler
Of course, to counter my nitpicking, you can point to the '$5 bills: 0' in the question. I'm not sure it is all that elegant, but...we can also debate whether each amount should be on a line on its own, and various other details.
Jonathan Leffler
+5  A: 

cash is a variable meaning that it can vary in value. In other words, you can change the value of cash.

The % operator returns the remainder from a division. So if you do cash = 93 % 20; then cash should contain 13.

Remember that you can also set cash in relation to itself. So cash = cash % 20; would set cash to the value of the remainder of cash / 20.

Imagist
A: 

I would put another integer at the top to represent the number of bills for the current denomination:

int billCount;

then replace each section (except the $1 section) with:

billCount = cash / 20;                  // billCount <- 93/20 = 4.
printf("$20 bills = %d\n", billCount); 
cash -= (billCount * 20);               // cash <- cash - (4*20) = 13.

You can do it with modulo operators with less lines of code but the above code is what the suggestion from the book is pointing you toward.

Try to work it out using just the information above, if possible (it'll make you a better programmer). If you still can't do it, here's my full solution.

#include <stdio.h>

int main (void) {
    int cash, count;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    count = cash / 20;
    printf("$20 bills = %d\n", count);
    cash -= (count * 20);

    count = cash / 10;
    printf("$10 bills = %d\n", count);
    cash -= (count * 10);

    count = cash / 5;
    printf(" $5 bills = %d\n", count);
    cash -= (count * 5);

    printf(" $1 bills = %d\n", cash);

    return 0;
}
paxdiablo
The code works, but it somehow feels wrong to teach a beginning programmer to rely on implicit rounding.
Wouter van Nifterick
Not at all in my opinion. The sooner they learn that, the quicker they'll stop asking why 3/2 doesn't give them 1.5. Rounding (truncating really) of integer types is a basic feature of the language.
paxdiablo
A: 

Thanks for all the help. Delroth & Imagist (well FooBar too, I dont know how I missed his comment while I was testing the theory since its exactly what I did) reminded me by simply adding another value for cash after each line would produce the result I was looking for.

#include <stdio.h>

int main (void) 
{
    int cash;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    printf("$20 bills = %d\n", cash / 20);
    cash = cash % 20;

    printf("$10 bills = %d\n", cash / 10);
    cash = cash % 10;

    printf("$5 bills = %d\n", cash / 5);
    cash = cash % 5;

    printf("$1 bills = %d\n", cash / 1);
    cash = cash % 1;
    return 0;
}

thats using the modulo % which hasnt been covered in the book yet, I'm going to try it again the way Pax suggested. Thanks again for the help

Matchz Malone
You don't need `cash = cash % 1;` on that last line; in fact, I don't think you *ever* need `x = x % 1` since it should always return `x`.
Mark Rushakoff
Well, you also don't need "cash / 1" in the printf but it adds a nice symmetry, don't you think :-) And there's an excellent chance both will be optimized away.
paxdiablo
+4  A: 

Also, for the record, you may want to put the bill values in a table and iterate on this to avoid copy-pasted code:

int main()
{
    int billval[] = { 20, 10, 5, 1, 0 };
    int cash, i;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    for(i = 0; billval[i]; i++) {
        printf("$%d bills = %d\n", billval[i], cash / billval[i]);
        cash = cash % billval[i];
    }

    return 0;
}

Here, the billval table is zero-terminated so we know when to stop iterating<

Jérémie Koenig
So, if I enter 80, you will tell me 4 * $20, 0 * $10, 0 * $5, and 0 * $1? Shouldn't you be iterating on `cash != 0` rather than `billval[i] != 0`?
Jonathan Leffler
Of course, to counter my nitpicking, you can point to the '$5 bills: 0' in the question.
Jonathan Leffler