# C program

This is a discussion on C program within the C Programming forums, part of the General Programming Boards category; Hello, I am learning C right now...anyway I do try to understand it but I got a problem with the ...

1. ## C program

Hello,

I am learning C right now...anyway I do try to understand it but I got a problem with the following program. It's a C program that calculates the exact change. So, since only int arithmetic is exact, I l need to break up your double into two ints, the one before the decimal point and the one after the decimal point. It gotta look like this here

Enter the sales price: 5.66
Enter the cash paid: 10.75
5 dollars, 1 nickel and 4 pennies
Want to try again? [y/n] n

I have like no idea now how do do this one. Can someone help me, please? Very much appreciated!!

2. You have no need to do that in the first place. Use double or float instead of int.

3. Originally Posted by Elysia
You have no need to do that in the first place. Use double or float instead of int.
Yes, I thought the same. But I am still not able to do it

4. char buf[50];
double price;
fgets(buf, sizeof(buf), stdin);
price = strtod(buf, NULL, 10);

5. If you insist upon using the two int approach, this is a somewhat elaborate way to do it:

Create a struct to represent an amount, containing two ints, one for dollars and one for cents. Create functions to do the necessary arithmetic on such structs. When you want to read an amount, you can use scanf() or fgets()/sscanf() with the format specificer of something along the lines of "&#37;d.%d".

If you do that, the rest of the program might be trivial.

6. Another way to think of it is to change all your amounts into pennies by multiplying them X 100.

Now 10.57 becomes 1057, and 5.95 becomes 595. Everything becomes an int since there are no tenths or hundred's of a penny.

To change it back to normal change, use a while loop:

Code:
```dollars = quarters = dimes = nickels = pennies = 0;

while(amount > 0)   {
if(amount > 100)
amount = amount - 100;
dollars++;
}
else if(amount > 24)   {
amount = amount - 25;
quarters++;
}
//continue with this logic with dimes, nickels, and pennies, until you have diminished
//amount to zero.

}  //end of while loop```

7. Note that if you want the highest change, you'd want to use something like this:
Code:
```while(amount >= 100) {
amount -= 100;
dollars ++;
}

/* ... */```
(Note the >= instead of >, as well.)

Another way to think of it is to change all your amounts into pennies by multiplying them X 100.

Now 10.57 becomes 1057, and 5.95 becomes 595. Everything becomes an int since there are no tenths or hundred's of a penny.

To change it back to normal change, use a while loop:

Code:
```dollars = quarters = dimes = nickels = pennies = 0;

while(amount > 0)   {
if(amount > 100)
amount = amount - 100;
dollars++;
}
else if(amount > 24)   {
amount = amount - 25;
quarters++;
}
//continue with this logic with dimes, nickels, and pennies, until you have diminished
//amount to zero.

}  //end of while loop```
So basically now I have to add the fields for the input and output and Im done?

9. Do you know how to write a number in binary? You start with the highest (most significant) digit. For example, to write 10 in binary, you'd see that there was an 8, and so put a 1 in the 8s column.
Code:
`1???`
Now you're left with 10-8 = 2. Consider the next column, the fours column: there is no 4 in 2.
Code:
`10??`
But there is a 2 in 2.
Code:
`101?`
Which leaves you with 0. There aren't any ones in 0.
Code:
`1010`
It's the same basic idea to convert a numerical amount into change. Start with the largest denomination -- say, whole dollars. Use as many of those as you can -- you'll eventually end up with a number less than a dollar. Now go on to the next smallest change -- say, quarters. Use as many quarters as you can, until you have less than 25 cents, and so on.

It's just like the code I posted. I'll extend it a little.
Code:
```while(amount >= 100) {
amount -= 100;
dollars ++;
}

while(amount >= 25) {
amount -= 25;
quarters ++;
}

/* ... */```
Of course, you could make your life easier and use / and &#37;.
Code:
```dollars = amount / 100;
amount %= 100;
quarters = amount / 25;
amount %= 25;
/* ... */```

10. Little tidbit to consider... If you need to worry about printing plural or not, you should try the tertiary command.

Code:
```:
:
if(dollars)
(dollars > 1) ? printf("%d dollars ", dollars) : printf("%d dollar ", dollars);
:
:
if(quarter)
...
if(dime)
...```
And so on till you reach your pennies.

11. And many such examples can be contrived:

Code:
`printf("&#37;d dollar%s", dollars, dollars == 1 ? "" : "s");`

12. Originally Posted by XodoX
So basically now I have to add the fields for the input and output and Im done?
No, you're a student, and I wouldn't let you go through this and be dumbed down.

You need to add your input block of code, extend the coin logic I showed before, all the way down to pennies, and add your output block of code.

As is mentioned above, and shown most eloquently by MacGyver, the tertiary operator is a great little addition to your code. That way "1 pennies" is never printed, becoming "1 penny", and "1 dollars" becomes "1 dollar", etc.

As much as anything else though, understand how the logic works here - especially WHY you have to start with the dollars in the while loop, instead of starting with the dimes, or quarters, etc. Also, why you need to use else if in the if statement, instead of a simpler else.

Notice also, that there are a myriad of ways to solve this problem. Some are simple and easy for nearly anyone to understand, others are complex and leave your imagination panting to catch it's breath. Others are somewhere in between.

Any of the solutions might be "the best", depending on the exact situation. Your goal imo is to find the solution that is most clear when you look at the problem to be solved, and at the code.

13. All right then...thank you.

14. okay at least I think I got a part of it now...

Code:
```  double SalesPrice = 5.66;
double CashPaid = 10.75;

int SPDollars; // Sales price dollars
int SPCents;   // Sales price cents
int CPDollars; // Cash paid dollars
int CPCents;   // Cash paid cents

int Dollars;   // Number of \$1 bills
int Quarters;  // Number of Quarters
int Dimes;     // Number of Dimes
int Nickels;   // Number of Nickels
int Pennies;   // Number of Pennies
int Remaining;

SPDollars = (int)SalesPrice;                         // SPDollars = 5
SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents
CPDollars = (int)CashPaid;
CPCents = (int)(CashPaid * 100.00) % 100;

Dollars = CPDollars - SPDollars;             // 5
Remaining = CPCents - SPCents;           // 9

if (Remaining < 0) {
// We had to borrow a dollar
Dollars--;
Remaining += 100;
};

Quarters = Remaining / 25; // How many quarters?  0
Remaining = Remaining % 25; // How much left?  9
Dimes = Remaining / 10; // How many dimes?  0
Remaining = Remaining % 10; // How much left? 9
Nickels = Remaining / 5; // How many nickels?   1
Pennies = Remaining % 5; // How many pennies? 4```
Does that look good so far ? I know I still need to add the other part.

15. Overall it looks - a little over complicated (what do you care about sales price dollars and cents? Or cash price dollars and cash price cents?). You seem to have the math all set - good!

Half those variables should wing their way to the variable graveyard. They just muck up the program with unneeded junk, imo.

Run-time and Clarity are the clarion call for programs, and your program has no problem with run-time.

Page 1 of 2 12 Last