# type casting?

• 10-22-2008
greatonesv
type casting?
I'm trying to make a program that you enter the amount something costs and the amount of money you used to pay with, and the program tells you your change in how many 20's 10's 5's 1's quarters and so on, that you would get back. The one problem I'm having is figuring out how to make my double into an int and split the dollars and change up. Any one have any guidence for me? Any help would be appreciated.
• 10-22-2008
tabstop
If you had to give back \$14.78 in change, how many cents is that? How do you know?
• 10-22-2008
khdani
to convert double to int you can cast like this:
Code:

```int integer; double number=5.26; integer = (int)number;```
if you want to get only the fraction (continuing last example)
Code:

```double number=5.26; number = number - integer;```
to split change, let's say your number of coins is N
first divide it by 20 save the result which is the amount of 20 coins you need
then do N-20*result and divide the answer by 10, this will give you the amount of 10 coins
you need, and etc...
• 10-22-2008
greatonesv
you would give back 1 ten, 4 ones , 3 quarters , and 3 pennies. I don't know how to go about that though.
• 10-22-2008
Thantos
US currency has a nice property in that if you two items X, Y such that X < Z and Y < Z then X + Y <= Z. What this really means is that if you go from the largest denomination to the smallest and give that denomination as much as possible then you've given that amount in as few bills/coins as possible.

So using tabstop's amount and going from \$20 to \$0.01. Initialize all counts to 0.

Amount Left: \$14.78
\$20 > \$14.78 - Stop \$20
\$10 < \$14.78 - Increase counter by 1. Subtract \$10. Left with \$4.78
\$10 > \$4.78 - Stop \$20
\$5 > \$4.78 - Stop \$5
\$1 < \$4.78 - Increase counter by 1. Subtract \$1. Left with \$3.78
\$1 < \$3.78 - Increase counter by 1. Subtract \$1. Left with \$2.78
\$1 < \$2.78 - Increase counter by 1. Subtract \$1. Left with \$1.78
\$1 < \$1.78 - Increase counter by 1. Subtract \$1. Left with \$0.78
\$1 > \$0.78 - Stop \$1

And continue until done.
• 10-22-2008
greatonesv
thnx thantos that's what I was looking for .
• 10-22-2008
greatonesv
Little problem
so i wrote up the program but for some reason, its not working and i cant see whats wrong. can any figure it out?

Code:

```#include <stdio.h> #include <math.h> void display_purpose(void); double change_breakdown(double change); int main() {         // q = Quarters, n = Nickels, d = Dimes, p = Pennies.         double purchase, payment, dollar_20, dollar_10, dollar_5, dollar_1, q, d, n, p, change, change2;         display_purpose();         printf("Enter the price of the puchase:  ");         scanf("%lf" , & purchase);         printf("Enter amount of payment:  ");         scanf("%lf" , & payment);                 change = payment - purchase;         change2 = change;         change_breakdown(change);         printf("Your change is: \$%.2lf \n %.lf - 20('s)\n %.lf - 10('s)\n %.lf - 5('s)\n %.lf - 1('s)\n %.lf - quarter(s)\n %.lf - dime(s)\n %.lf - nickels(s)\n %.lf - penny(s)\n ", change2, dollar_20, dollar_10, dollar_5, dollar_1, q, d, n, p);                 return 0; } void display_purpose(void) {         printf("The purpose of this program is to calculate and display change based on purchase price.\n\n"); } double change_breakdown(double change) {         double dollar_20, dollar_10, dollar_5, dollar_1, q, d, n, p;         if(20.00 < change)         {         dollar_20++;         change - 20.00;         }         if(10.00 < change)         {         dollar_10++;         change - 10.00;         }         if(5.00 < change)         {         dollar_5++;         change - 5.00;         }         if(1.00 < change)         {         dollar_1++;         change - 1.00;         }         if(00.25 < change)         {         q++;         change - 00.25;         }         if(00.10 < change)         {         d++;         change - 00.10;         }         if(00.05 < change)         {         n++;         change - 00.05;         }         if(00.01 < change)         {         p++;         change - 00.01;         }         return dollar_20, dollar_10, dollar_5, dollar_1, q, d, n, p; }```
• 10-22-2008
tabstop
You can only return one number from a function.
• 10-22-2008
nonoob
You need to initialize double dollar_20, dollar_10, dollar_5, dollar_1, q, d, n, p to zero before adding things to them.

What happens when an amount has multiple 5\$ bills in it, or multiple any sized coins or bills? You only subtract one at most.
• 10-22-2008
greatonesv
so how would i return multiple values then?
• 10-22-2008
greatonesv
Quote:

Originally Posted by tabstop
You can only return one number from a function.

well i tried to make it return one value but now i get an imcompatible return type error.

Code:

```#include <stdio.h> #include <math.h> void display_purpose(void); double change_breakdown(double change); int main() {         // q = Quarters, n = Nickels, d = Dimes, p = Pennies.         double purchase, payment, dollar[8], change, change2;         display_purpose();         printf("Enter the price of the puchase:  ");         scanf("%lf" , & purchase);         printf("Enter amount of payment:  ");         scanf("%lf" , & payment);                 change = payment - purchase;         change2 = change;         change_breakdown(change);         printf("Your change is: \$%.2lf \n %.lf - 20('s)\n %.lf - 10('s)\n %.lf - 5('s)\n %.lf - 1('s)\n %.lf - quarter(s)\n %.lf - dime(s)\n %.lf - nickels(s)\n %.lf - penny(s)\n ", change2, dollar[1], dollar[2], dollar[3], dollar[4], dollar[5], dollar[6], dollar[7], dollar[8]);                return 0; } void display_purpose(void) {         printf("The purpose of this program is to calculate and display change based on purchase price.\n\n"); } double change_breakdown(double change) {         // \$20 = dollar[1] / \$10 = dollar[2] / \$5 = dollar[3] / \$1 = dollar[4] / \$.25 = dollar[5] / \$.10 = dollar[6] / \$.05 = dollar[7] / \$.01 = dollar[8]         double dollar[8];         if(20.00 < change)         {         dollar[1]++;         change - 20.00;         }         if(10.00 < change)         {         dollar[2]++;         change - 10.00;         }         if(5.00 < change)         {         dollar[3]++;         change - 5.00;         }         if(1.00 < change)         {         dollar[4]++;         change - 1.00;         }         if(00.25 < change)         {         dollar[5]++;         change - 00.25;         }         if(00.10 < change)         {         dollar[6]++;         change - 00.10;         }         if(00.05 < change)         {         dollar[7]++;         change - 00.05;         }         if(00.01 < change)         {         dollar[8]++;         change - 00.01;         }         return dollar[8]; }```
• 10-22-2008
tabstop
Declaring an eight-element array means you can use dollar[0] through dollar[7]. You don't get an incompatible type warning by returning dollar[8], it's just that dollar[8] doesn't exist. (Oh and by the way you can't return an array from a function either.)
• 10-22-2008
greatonesv
I got the code working and everything is correct except, the pennies are off by 1 or 2 occasionally idk why.

Code:

```#include <stdio.h> #include <math.h> void display_purpose(void); double change_breakdown(double change , double dollar[8]); int main() {         // P = Purchase / T = Payment         double P, T, change, change2, dollar[8];         int again;         for( ; ; ) {         display_purpose();         printf("Enter the price of the puchase:\$ ");         scanf("%lf" , & P);         printf("Enter amount of payment:\$ ");         scanf("%lf" , & T);                 change = T - P;         change2 = change;         change_breakdown(change , dollar);         printf("Your change is: \$%.2lf \n %.lf - 20('s)\n %.lf - 10('s)\n %.lf - 5('s)\n %.lf - 1('s)\n %.lf - quarter(s)\n %.lf - dime(s)\n %.lf - nickels(s)\n %.lf - penny(s)\n ", change2, dollar[0], dollar[1], dollar[2], dollar[3], dollar[4], dollar[5], dollar[6], dollar[7]);                printf("\n\nEnter a 0 to end, or a 1 to repeat.");         scanf("%d" , &again);         if ( !again ) break; }         return 0; } void display_purpose(void) {         printf("\n\nThe purpose of this program is to calculate and display change based on purchase price.\n\n"); } double change_breakdown(double change , double dollar[8]) {         /*\$20 = dollar[0] / \$10 = dollar[1] / \$5 = dollar[2] / \$1 = dollar[3] / \$.25 = dollar[4] / \$.10 = dollar[5] / \$.05 = dollar[6] / \$.01 = dollar[7] */         for(20.00 <= change, dollar[0] = 0 ; 20.00 <= change ; dollar[0]++ )         {                 change -= 20.00;         }         for(10.00 <= change, dollar[1] = 0 ; 10.00 <= change ; dollar[1]++ )         {                 change -= 10.00;         }         for(5.00 <= change, dollar[2] = 0 ; 5.00 <= change ; dollar[2]++ )         {                 change -= 5.00;         }         for(1.00 <= change, dollar[3] = 0 ; 1.00 <= change ; dollar[3]++ )         {                 change -= 1.00;         }         for(0.25 <= change, dollar[4] = 0 ; 0.25 <= change ; dollar[4]++ )         {                 change -= 0.25;         }         for(0.10 <= change, dollar[5] = 0 ; 0.10 <= change ; dollar[5]++ )         {                 change -= 0.10;         }         for(0.05 <= change, dollar[6] = 0 ; 0.05 <= change ; dollar[6]++ )         {                 change -= 0.05;         }         for(0.01 <= change, dollar[7] = 0 ; 0.01 <= change ; dollar[7]++ )         {                 change -= 0.01;         }         return 0; }```