tags:

views:

144

answers:

3

Question : Program that asks the user to enter an item price value and then show how to pay that amount using the smallest number of $ 50,$20, $10,$5, and $1 bills consist.

Example Output:

Enter Price: 187
Enter Amount to pay: 500
Change is : 313

(6)$50 (1)$10 (3)$1
(0)$20 (0)$5

Here's my code: hope you help me , I am having a hard to in knowing the right formula for it..

#include <stdio.h>
#include <conio.h>
#define p printf
#define s scanf
#define g gotoxy

main()
{
    clrscr();
    int c1,c2,c3,c4,c5;
    int price,amount;
    float change;
    p("Enter Price: ");s("%d",&price);
    p("Enter amount: ");s("%d",&amount);
    change=amount-price;
    p("Change is : %f ",change);
    c1=(change/50);
    c2=(0);
    c3=(change/change);
    c4=(0);
    c5=(change/change)+2;
    g(5,5);p("(%d) Php 50",c1);
    g(5,6);p("(%d)  Php 20",c2);
    g(18,5);p("(%d)Php 10 \t",c3);p("(%d)Php 1",c5);
    g(18,6);p("(%d) Php  5 ",c4);


    getch();
    return 0;
}
+3  A: 

You're on the right track:

change should be a int too (that means you should change %f to %d). You would then correctly determine the number of 50's (note that integer division in C truncates). You should look at % (modulus operator) to get the remaining amount of changes after the 50's are dealt with:

Using your example:

change = 313
fifties = 313/50 (6)
change %= 50 (13)

That means set change to the remainder after dividing itself by 50 (change = change % 50)

twenties = change / 20 (0)
change %= 20 (13)

tens = change / 10 (1)
change %= 10 (3)

This should give you the basic idea of the code you need. You just continue this pattern in order of decreasing denomination.

As noted, use better variable names, don't use those defines, and generally stick to one statement per line (add a newline after semi-colons). This will make your code more readable. You're also using more parentheses than needed, but that's not a big deal.

Matthew Flaschen
What if i change my Price to 500? and amount to 1000; should i change my formula too?.. sorry newbie.. //Wrong Output (o_O) got a $13 over..Enter Price: 500Enter Amount to pay: 1000Change is : 500(10)$50 (1)$10 (3)$1(0)$20 (0)$5
Lionell Paquera
No, the formula is the same for all prices and amounts. This is just an example.
Matthew Flaschen
+2  A: 

I would suggest define an array which holds the bill denominations, and an initially empty array of bill counts:

 int denoms[5] = {50, 20, 10, 5, 1};
 int bills[5] =  {0, 0, 0, 0, 0};

 for(int i =0; i < 5; ++i)
 {
     bills[i] = /* do something interesting with denoms[i] here */
     change = /* more work for you here */
 }

 /* output answer */
 for(int i =0; i < 5; ++i)
 {
     if (bills[i] > 0)
          p("{%d)$%d", bills[i], denoms[i]);
 }
 p("\n");
 for(int i =0; i < 5; ++i)
 {
     if (bills[i] == 0)
          p("{%d)$%d", bills[i], denoms[i]);
 }
 p("\n");
James Curran
i guess using for loop is not intended in this exercise cause my professor is still not teaching us conditional statements..
Lionell Paquera
A: 
void changeloop(int* change, int* counter, int amount) {
    while (*change > amount) {
        (*counter)++;
        (*change) -= amount;
    }
}
int main() {
    clrscr();
    int price; printf("Enter Price: "); scanf("%d", &input);
    int amount; printf("Enter Amount: "); scanf("%d", &amount);
    int change = amount - price;
    int fifties, twenties, tens, fives, ones;
    fifties = twenties = tens = fives = ones = 0;
    changeloop(&change, &fifties, 50);
    changeloop(&change, &twenties, 20);
    changeloop(&change, &tens, 10);
    changeloop(&change, &fives, 5);
    changeloop(&change, &ones, 1);
    printf("Fifties: %d\n", fifties);
    printf("Twenties: %d\n", twenties);
    printf("Tens: %d\n", tens);
    printf("Fives: %d\n", fives);
    printf("Ones: %d\n", ones);
    getch();
    return;
}

There's work to do, like input validation and error handling. But the basics are here. The code could be refactored to be much more extensible... but meh.

DeadMG
here's the Output :Example:Enter Price : 187Enter Amount: 500fifties =584 <--- 500+ fifties? hehehTwenties: 1tens : 0fives : 1Ones : 3
Lionell Paquera
Then you screwed up. Mine gives 6/0/1/0/2, which is obviously not quite right. I guess that you might actually have to try to solve the problem.
DeadMG