Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fily

macrumors regular
Original poster
May 12, 2008
162
0
Thought I'd ask you fine gentlemen and women another question, since my last one was answered to perfectly.

I basically just need to make a program with C that asks the USER to input a cost, and subtract it from one hundred.

Enter the price of the purchase: $66.78
The change from $100.00 is: $33.22
Number of $20 bills is: 1
Number of $10 bills is: 1
Number of $2 coins is: 1
Number of $1 coins is: 1
Number of dimes is: 2
Number of pennies is: 2

It's also suppose to have 5 dollars, nickels and quarters. It's easy enough I know, but I, for the life of me, can't get passed 2 dollars. I can have it tell me how many Twenties, Tens and Fives they need to give back, but after that, I'm just incredibly stumped.

This is what I have so far, this is my 3rd attempt, writing the code more clearly from my other attempts.

Code:
#include <stdio.h>
                                     
int main(void)
{
	
	int num;
	int change;
	int rea20;							/*Remaining after Twenty*/
	int rea10;							/*Reamining after Ten   */
	int rea5;							/*Reamining after five  */
	
	printf("Enter the price of the purchase in dollars ($100 or less): \n");
		scanf("%d", &num);
		
		
	printf("The change from $100 is: %d\n", 100 - num);
		change = 100 - num;
	
	printf("The number of $20 bills is: %d\n", change / 20);
		rea20 = change % 20;
		
	printf("The number of $10 bills is: %d\n", rea20 / 10);
		rea10 = rea20 % 10;
		
	printf("The number of $5 bills is: %d\n", rea10 / 5);

If someone could show me how to just get the toonie, but nothing else. I just need a push in the proper direction, so I can than figure out the rest on my own.

Thanks in advance.

EDIT: Actually if I could get help for the cents instead, as I just figured out the toonie and loonie.. go figure.
 

Cromulent

macrumors 604
Oct 2, 2006
6,812
1,100
The Land of Hope and Glory
Just do all your working out in cents not dollars. It will be much easier that way.

Edit : Just work it out on a piece of paper. You are not having problems programming, you are having problems with the maths. Just do it on paper and then just transfer the sums to your program and voila!

Edit 2 : Initialise your variables as well :).
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
As Cromulent said, working in cents is much easier. if you must take input as a floating point value, do so (use the %lf for a double or %f for a float, rather than %d), then multiply times 100 to get the input in cents. Subtract that value from 10000, and you know how many cents you need to give as change. From there you just have to divide and mod away.

20s: Divide by 2000, mod by 2000 and reassign.
10s: Divide by 1000, mod by 1000 and reassign.
...
Quarters: Divide by 25, mod by 25 and reassign.
...

-Lee

P.S. This assumes no input validation at all. Input validation does muck things up, but you should only read into very large character arrays using a "safe" read of only the length of the buffer. After doing so, you have to make sure if you want a number, you got one, and if you have an upper and lower bound (0.00 and 100.00) that the number is in that range, etc.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
If you insist on using "whole dollar arithmetic" instead of cents, you probably ran into the problem that you tried to use the modulo operator on floating point values, by trying

amount % 0.25

for the quarters, and got an error saying % only works for integers.

In that case, look up the "fmod()" function.

Good luck,
Sander
 

enkidu

macrumors newbie
Apr 20, 2008
16
0
If you insist on using "whole dollar arithmetic" instead of cents, you probably ran into the problem that you tried to use the modulo operator on floating point values, by trying

amount % 0.25

for the quarters, and got an error saying % only works for integers.

In that case, look up the "fmod()" function.

Good luck,
Sander
Or alternatively, multiply the amounts by 100 to do all calculations in cents. Same div for number of each currency, mod for remainder algorithm should work.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,095
8,366
Los Angeles
Rather than using rea20, rea10, rea5, and so on, I'd keep one variable "change" for the amount of change left to be given, because that's how you'd think of the process in your head.

To make change for $53.22, you'd think how many $20s? Two. Print that fact. Subtract $20 times 2.

Now you have $13.22. How many $10s? One. Print that fact. Subtract $10.

Now you have $3.22. How many $5s? Zero. Nothing to print. Subtract $5.

You still have $3.22. How many $1s? And so on.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
Now you have $3.22. How many $5s? Zero. Nothing to print. Subtract $5 times zero.

Obviously just a typo, but just in case someone types this in, bases their multi-million multinational business on it, and turns around to sue you. ;-)
 

fily

macrumors regular
Original poster
May 12, 2008
162
0
Rather than using rea20, rea10, rea5, and so on, I'd keep one variable "change" for the amount of change left to be given, because that's how you'd think of the process in your head.

To make change for $53.22, you'd think how many $20s? Two. Print that fact. Subtract $20 times 2.

Now you have $13.22. How many $10s? One. Print that fact. Subtract $10.

Now you have $3.22. How many $5s? Zero. Nothing to print. Subtract $5.

You still have $3.22. How many $1s? And so on.

Yeah, I ended up figuring that out the other day on my own, I just kept reassigning my remainder into Change, which worked out much, much better. It got much too confusing using all these different Integers. But yeah, my problem now is the problem I get with trying to mod floating points, but I noticed the post above, mentioning the fmod(), definitely need to look that up, since this stupid lab is due tomorrow.

As of right now, this is what my code looks like.

#include<stdio.h>

int main(void)
{

int num = 0; /*Number to be inputed by user*/
int change = 0; /*The change the user will receive when subtraced from 100*/

printf("Enter the price of the purchase in dollars ($100 or less): \n");
scanf("%d", &num);

printf("The change from $100 is: %d\n", change = 100 - num);

printf("The number of $20 bills is: %d\n", change / 20);
change = change % 20; /*Change now equals the remainder of change divided by 20*/

printf("The number of $10 bills is: %d\n", change / 10);
change = change % 10; /*Change now equals the remainder of change divided by 10*/

printf("The number of $5 bills is: %d\n", change / 5);
change = change % 5; /*Change now equals the remainder of change divided by 5*/

printf("The number of $2 coins is: %d\n", change / 2);
change = change % 2; /*Change now equals the remainder of change divided by 2*/

printf("The number of $1 coins is: %d\n\n", change);

}
 

fily

macrumors regular
Original poster
May 12, 2008
162
0
Yeah, I can't figure out this fmod() thing, and my teacher is no help, says he's never used fmod before.. some teacher he is..

Could anyone please use fmod in an example, possibly using some of my code?
 

cmaier

Suspended
Jul 25, 2007
25,405
33,474
California
Yeah, I can't figure out this fmod() thing, and my teacher is no help, says he's never used fmod before.. some teacher he is..

Could anyone please use fmod in an example, possibly using some of my code?

fmod(a,b) returns the remainder of a/b. So, if a is 0.25, and b is 1.37, fmod(a,b) would return 0.12.

So if you have quarters, dimes, nickles, you do each in turn.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Multiplying by 100 is so much easier! Money is not floating point in the vast majority of uses (gas stations that quote prices as $3.49 9/10 are just *******s, and otherwise only banks that deal with interest and the fractions of a cent it produces care about floating point math when it comes to money)!

The smallest unit of currency in the EU, US, etc. is one cent or eurocent or whatever you want to call it. Everything else is multiples of that. So a cent is 1 unit of currency, a nickel is 5, a dollar is 100, 20 dollars is 2000, etc.

Floating point math on a computer, especially using a 4-byte IEEE-734 number is inherently inexact. There's no reason to introduce losses of precision when you are dealing with fixed point math.

As I stated above, if you must read the value as $xxx.yy, that's fine. But that's the only operation you need to use a floating point value for. After that, multiply by 100, then compare to (int)10000 to get the change, and work with an int from then on. Then you can use the plain, built-in / and % operators to do your calculations.

It's great to know fmod is out there and how to use it, but in this case it's totally beyond the scope of the lab/project. Once you start dealing with floats you have to worry about rounding, and lose the ability to use == to test equivalence due to machine delta, etc. Just stick with ints, and save yourself the headache.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You can Google any function name and it normally gives you a good reference page for that specific function.

fmod()

Even better, you can just type:
man fmod

at the terminal and get the handy man page.

And I don't think not knowing every function in the standard libraries makes you a bad teacher. Not knowing how to find the information out, might, though.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.