tags:

views:

325

answers:

3

I need to break down the amounts on their respective bill/coin.

The output is somewhat like this:

So far, here is my code: (I made the last few codes a comment one 'cos the errors come from there)

  { 
  int x,y;

  printf("Enter input: ");
  scanf("%d",&x);

  y=x/1000;
  printf("\n# of $1000 bill: %d",y);
  x = x%1000;

  y=x/500;      
  printf("\n# of 4500 bill: %d",y);    
  x = (x%500);

  y=x/200;
  printf("\n#. of $200 bill: %d",y);    
  x = (x%200);

  y=x/100;
  printf("\n# of $100 bill: %d",y);    
  x = (x%100);

  y=x/50;
  printf("\n# of $50 bill: %d",y);    
  x = (x%50);

  y=x/20;
  printf("\n# of $20 bill: %d",y);    
  x = (x%20);

  y=x/10;
  printf("\n#. of $10 coin: %d",y);    
  x = (x%10);

  y=x/5;
  printf("\n#. of $5 coin: %d",y);    
  x = (x%5);

  y=x/1;
  printf("\n# of $1 coin: %d",y);    
  x = (x%1);


  getch();
  return 0;
  }

I hope you'll help me out with this. :/

Thanks!

A: 

.25 and .01 aren't numbers. Read up in C documentation how do you write floating point numbers...

Axarydax
.25 and .01 are so numbers, they are just not integers.
Jason Coco
I tried to make y as float but it doesn't work and all of them wouldgo error.
rob2k8
Also, `x` and `y` are integers, and `"%d"` passed to scanf causes it to read an integer.
ChrisW
-1 That is not an answer!
Betamoo
aw snap, I was wrong. but anyways, writing fractions without leading zero has always looked shady to me:)
Axarydax
It doesn't work. A lot of errors appeared. Here:*** invalid operands of types `float' and `int' to binary `operator%'
rob2k8
I've tried 25/100 instead of 0.25, when compiling, it doesn't show any error. But when I run it, the program crashes and closes.
rob2k8
@Axarydax: So YOU was the one who should read the C documentation...
Betamoo
I think Axarydax meant `0.25` and `0.01` are not `float` s, but `double` s.
Eduardo León
@Eduardo León: read his/her statement: ".25 and .01 aren't numbers." ......
Betamoo
+5  A: 

It's a simple heuristic problem. You already have the right idea. For a start, if you want to use int (and that's probably a good idea in your case to avoid fp-precision headaches and the need to use a separate fmod), you'll have to scale the floating-point input and your modulo/divisors by 100. Also to cut down on some redundancy, consider using a function like:

// returns number of units and subtracts unit_size * result
// from val
int units(int* val, int unit_size)
{
    int num = *val / unit_size;
    *val %= unit_size;
    return num;
}

printf("No. of P1000 bill: %d\n",units(&x, 1000 * 100) );
printf("No. of P500 bill: %d\n",units(&x, 500 * 100) );
printf("No. of P200 bill: %d\n",units(&x, 200 * 100) );
etc.

That should cut down on redundant code a little bit. Maybe not worth getting too fancy for it and I suspect this is homework. Complete solution:

// returns number of units and subtracts unit_size * result
// from val
int units(int* val, int unit_size)
{
    int num = *val / unit_size;
    *val %= unit_size;
    return num;
}

int main()
{
    printf("Enter input: ");

    float amount;
    scanf("%f",&amount);
    int x = (int)(amount * 100.0 + 0.5);

    printf("No. of P1000 bill: %d\n", units(&x, 1000 * 100) );
    printf("No. of P500 bill: %d\n", units(&x, 500 * 100) );
    printf("No. of P200 bill: %d\n", units(&x, 200 * 100) );
    printf("No. of P100 bill: %d\n", units(&x, 100 * 100) );
    printf("No. of P50 bill: %d\n", units(&x, 50 * 100) );
    printf("No. of P20 bill: %d\n", units(&x, 20 * 100) );
    printf("No. of P10 coin: %d\n", units(&x, 10 * 100) );
    printf("No. of P5 coin: %d\n", units(&x, 10 * 100) );
    printf("No. of P1 coin: %d\n", units(&x, 1 * 100) );
    printf("No. of 25 cents: %d\n", units(&x, 25) );
    printf("No. of 1 cent: %d\n", units(&x, 1) );

    return 0;
}

[Edit] If you have trouble understanding pointers, then just do it the way you wrote without using the units function but modify it accordingly to read in a float and multiply by 100 as in the example above.

[Edit] Requested:

int main()
{
    printf("Enter input: ");

    float amount;
    scanf("%f",&amount);
    int x = (int)(amount * 100.0 + 0.5); // x stores the user input in cents

    int y = x / 100000; // 1000 dollars is 100,000 cents
    printf("\nNo. of P1000 bill: %d",y);
    x = x % 100000;

    ...

    y=x / 25; // we're working with cents, so 25 = 25 cents
    printf("\nNo. of 25 cents: %d",y);    
    x = (x % 25);

    ...
}
+1 Good Answer... But change the pointer to reference to make it neat... (or move % out of the function)
Betamoo
The title and code suggests he's using C instead of C++ hence I avoided references in this case.
It has C++ tag... anyway you can just move % out of the function... he/she seems a beginner and may be confused with pointers..
Betamoo
Yeah, I added a comment at the bottom to just do it his way but modify it accordingly to convert to integer and scale by a hundred since there's little purpose using the units function otherwise. If he's using C++, he should probably use iostreams instead of printf/scanf as that doesn't require understanding format specifiers and pointer semantics on scanf.
I'm not sure that it's right to give the complete solution. You might like to read [How to ask and answer homework questions?](http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow).
ChrisW
@ChrisW Normally I'd refrain, but in the current situation, the original poster got it right logically with the exception of a lack of a solution for dealing with floating-point decimal units. Since he already did the bulk of the work, it's difficult to guide him in the right direction without a back-and-forth discussion format and easier to just provide a solution.
If this was a discussion, I'd point him to fmod, scaling the floating-point input to an integral, or reading it as a string and separating cents from dollars. I'd have to see if he needs additional help though; kind of difficult in this kind of format.
Okayy, I appreciate your help but I totally don't understand the functions or codes you've given. We are only required to use modulo and integer division (well, preferably) for now. And I don't get what you really meant on the edit message of yours. I hope you'll help me in solving this. :/
rob2k8
@rob2k8 in that case, you can ignore everything I wrote with the exception of this part: float amount; scanf("%f", int x = (int)(amount * 100.0); Those three lines are required so that the user can type in a floating-point value but so that you can still perform all the division using integer division and modulo. You will have to scale all your unit sizes by 100 (ex: P200 bill should be 200 * 100 or 20000) so that they're all integrals representing cents.
Okay. I have this code. My problem is on how do I change the cents in my modulo. Do I still need to scale it by 100? If so, how? 'Cos I've tried having x=(x*100)%(5*100) won't work on any of the bill/coin/cents. It gives another answer. BTW, here's my code in the last part: y=(100*x)/(1*100); printf("\nNo. of P1 coin: %d",y); x = (x%1); y=(100*x)/(25); printf("\nNo. of 25 cents: %d",y); x = x%(25); //this part. How do I edit this? It gives me errorwhen I try to make it (25/100) or (0.25)
rob2k8
Can you give me the actual coding of it at least 'til 500 or 200 with my code that is added by the 3 lines you said? I have that idea but I'm always lost when I try. Please :/
rob2k8
I added an edit with a couple of examples which should set you on the right track. Just fill in the missing parts.
I now got it but I have a mathematical error on the 1 cent part. When I try to input other numbers, it works well. But when I input the amount "4654.46", I got a result of 20 instead of 21. But when I try on 5 digits (54302.46), it gives the right amount which is 21. So maybe it doesn't work on 4 digits but it is required on our homework to make it work on 4 digits as well. BTW, the edited code is written above.
rob2k8
@rob2k8: fixed in the example above. See the line where we define 'x'.
+2  A: 

Scan the user's value into a float, then separate the dollars and cents into two int values.

The coins required for the cents value should then be very easy to calculate, using the same method you've already used (except dividing/mod by 25 for 25 cents, 1 for 1 cent etc.)

MatthewD
oho, many issues come with code using floats (rounding problems) I would not advice anyone to do that, rather use integers * 100.
jdehaan
I'm only recommending receiving the user's input using a float. From there all calculations are done on ints.The highest rated solution in this thread also receives the user's value into a float. Why is his fine while mine is not?
MatthewD
Matthew, I must confess that I misunderstood the answer by reading it too quickly. But just for your info, I did not downrate you. I will give you an upvote as soon as I get some votes again (means tomorrow). Don't give up, getting downvotes is quite discouraging sometimes, I know it from my own experience, I had to learn not taking it too personal. Go on and you'll succeed! :-) Sometimes people get influenced by the comments below the answer, maybe I am indirectly responsible for the -1, sorry...
jdehaan
"Why is his fine while mine is not?" -- I think your answer is better: because yours clearly and concisely describes the algorithm/method (without giving the code); so +1.
ChrisW
Matthew, I'm sorry. I don't get your point to use the same method except for dividing/mod by 25 for 25 cents? So what am I supposed to do with the 25 cents part?
rob2k8
@rob2k8 - After you've stored the fractional cents in an integer (by multiplying by 100), then change your code to use `25` instead of `0.25`.
ChrisW
Yep, what ChrisW said.
MatthewD
@Matthew: the +1 you deserved! :-)
jdehaan
Thanks jdehaan! :-)
MatthewD