# How should I go about solving this problem?

• 08-05-2011
ARod609
How should I go about solving this problem?
Write a C function named change() that accepts a floating point
number of total coins and the addresses of the integer variables named
quarters, dimes, nickels, and pennies. The function should
determine the number of quarters, dimes, nickels, and pennies in the total
coins number passed to it and write these values directly into the
respective variables declared in its calling function using pointers.
Call the function change() from main() three times and print out
the contents of the variables quarters, dimes, nickels, and
pennies after each function return.

First Call—pass in the total value \$1.88 and on return print the contents of
the variables.

Second Call—pass in the total value .32 and on return print the contents
of the variables.

Third Call— ask for a total value input from the keyboard and on return
print the contents of the variables.
Output should look like:

TOTAL VALUE ENTERED: 1.88
7 quarters
1 dime
0 nickels
3 pennies

and not:

TOTAL VALUE ENTERED: 1.88
7 quarters
18 dimes
37 nickels
188 pennies

The question itself is confusing to me... create a function that accepts floating point numbers and first call, second call and third call... I am not sure on how to go about this
• 08-05-2011
AndrewHunter
Quote:

Originally Posted by ARod609
Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies.

This is telling you what your function prototype should look like. The function should take 5 arguments, a float (for total coin amount), and 4 pointers to integers (for quarters, dimes, nickels, and pennies).

Quote:

Originally Posted by ARod609
The function should determine the number of quarters, dimes, nickels, and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers.

This tells you what the function should do and how it should do it.

Quote:

Originally Posted by ARod609
Call the function change() from main() three times and print out the contents of the variables quarters, dimes, nickels, and pennies after each function return.

First Call—pass in the total value \$1.88 and on return print the contents of
the variables.

Second Call—pass in the total value .32 and on return print the contents
of the variables.

Third Call— ask for a total value input from the keyboard and on return
print the contents of the variables.

This is telling you how to lay out your program, e.g. what your main should look like. Not worded the best, however they want you to make a program that looks something like:
Code:

```//your include files int main(void){     //the variables you need to make     call your function with total coin = 1.88     print your output         call your function with total coin = .32     print your output     obtain user input for total coin amount     call your function with total coin = user inputted value     print your output     return (0); }```
• 08-06-2011
ARod609
1) Math header not sure if i need it...
2) Function I am not sure on how to use it correctly...
3) Output did exactly what I was not supposed to do...

The book I am using has a bunch of exercises but no examples of the way I should format it or use the code. The only way I know if I am wrong is if the compiler tells me... lol
Code:

```#include <stdio.h> #include <math.h> int main (int argc, const char * argv[]) {   int change(float);         float  total;     int quarters, dimes, nickels, pennies;     printf ("Please enter your total value:");     scanf ("%f",&total);         quarters = total/.25;     dimes = total/.10;     nickels = total/.05;     pennies = total/.01;             printf ("Total Value Entered: %f \n",total);     printf ("%9d Quarters\n",quaters);     printf ("%9d Dimes\n",dimes);     printf ("%9d Nickels\n",nickels);     printf ("%9d pennies\n",pennies);         return 0; }```
Output
Code:

```Total Value Entered: 4.000000       16 Quaters       40 Dimes       80 Nickels       400 pennies```
• 08-06-2011
AndrewHunter
Quote:

Originally Posted by ARod609
1) Math header not sure if i need it...

You do not need it for this problem,

Quote:

Originally Posted by ARod609
2) Function I am not sure on how to use it correctly...

They want a function. So the prototype would be something like:

void change(float totalcoin, int* quarters, int* dimes, int* nickels, int* pennies);

Quote:

Originally Posted by ARod609
3) Output did exactly what I was not supposed to do...

A hint for this: most banking software actually stores the amount in a large integer of pennies. So if I were to do something like this I would take the total input and convert it to pennies before I started, e.g. 1.10 would become 110 pennies. After that the operations become pretty simple. Look at what '/' does and what '%' does for math operations.
• 08-06-2011
CommonTater
Quote:

Originally Posted by ARod609
1) Math header not sure if i need it...
2) Function I am not sure on how to use it correctly...
3) Output did exactly what I was not supposed to do...

No offense is intended... but, I'm betting you went straight from reading the assignment to your keyboard and started tapping out code... hence, failure.

Every program, no matter how small, involves the 4 steps in my signature...
1) Analyse the problem until you have a good understanding of what is required.
2) Then and only then, begin planning a solution to the problem... researching as required.
3) Now that you know what your writing... you can sit down and create code.
4) Once the code is in a state where it's at least partially functional you should begin testing.

But take hope... Thomas Edison, inventor of the lightbulb was once in a position to say: "No. I don't know how to make a lightbulb but I have discovered several ways not to make one." (Or very close) ... You are now in the forunate position of having disovered one of a great many ways not to write a computer program.
• 08-06-2011
stahta01
@ARod609:

Your problem is to write a function on how to make change; your function does NOT do that.
Do you know how to make change?
If not, find someone to tell you how to do it.

Then think about it and write down the steps to do it.
Change the steps into a C program.

Quote:

TOTAL VALUE ENTERED: 1.88
7 quarters
1 dime
0 nickels
3 pennies
The above coins add up to 1.88 you answer does NOT add up to the value inputted.

Tim S.
• 08-06-2011
ARod609
@Stahta01 I know I just dont know how to apply it in "C" lol... so I am lost using the call functions with the modular division (%) to get the remainder to break it down in cents. I thought it would be hot to be able to enter any number and get back the change...
• 08-06-2011
stahta01
Quote:

Originally Posted by ARod609
@Stahta01 I know I just dont know how to apply it in "C" lol... so I am lost using the call functions with the modular division (%) to get the remainder to break it down in cents. I thought it would be hot to be able to enter any number and get back the change...

You C code implies you have no idea on how to make change!

We are not a place that writes code for others; try to do some work.
Write out the steps; post the steps.

Tim S.
• 08-06-2011
ARod609
@Stahta01
I am just trying to learn "C" with the help of the board, I only post a question when I am stumped... Your jokes are funny haha, now I appreciate your advice on writing out the steps and thank you. Everything else is really un called for!
• 08-06-2011
CommonTater
Quote:

Originally Posted by ARod609
@Stahta01 I know I just dont know how to apply it in "C" lol... so I am lost using the call functions with the modular division (%) to get the remainder to break it down in cents. I thought it would be hot to be able to enter any number and get back the change...

Screw the code... think about the problem... How does making change work?

Lets say you owe someone \$0.41 in change...
How much of that can you make up in quarters? (1)
Ok, so you give them a quarter.
How much do you still owe them? (\$0.16)
How much of that amount can you make up in dimes? (1)
Ok so you give them a dime.
How much do you still owe them? (\$0.06)
How much of that can you make up in nickles? (1)
Give them a nickel, how much is left? (\$0.01)
So you give them a penny....

See the problem is you're trying to think about C code, when you haven't even understood the problem itself...
• 08-06-2011
CommonTater
Quote:

Originally Posted by ARod609
@Stahta01
I am just trying to learn "C" with the help of the board, I only post a question when I am stumped... Your jokes are funny haha, now I appreciate your advice on writing out the steps and thank you. Everything else is really un called for!

You need to stop defending yourself from your friends and start listening to their advice...
• 08-06-2011
ARod609
Code:

```#include <stdio.h> void change(float, int *, int*, int*, int*); int main() {         float total;         int quarters, dimes, nickels, pennies;         {         total = 1.88;                 change(total, &quarters, &dimes, &nickels, &pennies);                 printf("TOTAL VALUE ENTERED: %.2f\n", total);         printf("%9d quarters\n", quarters);         printf("%9d dimes\n", dimes);         printf("%9d nickels\n", nickels);         printf("%9d pennies\n", pennies);             total = 0.32;                 change(total, &quarters, &dimes, &nickels, &pennies);                 printf("TOTAL VALUE ENTERED: %.2f\n", total);         printf("%9d quarters\n", quarters);         printf("%9d dimes\n", dimes);         printf("%9d nickels\n", nickels);         printf("%9d pennies\n", pennies);             printf("Enter the amount of money in your pocket:  \n");         scanf("%f", &total);                 change(total, &quarters, &dimes, &nickels, &pennies);                 printf("TOTAL VALUE ENTERED: %.2f\n", total);         printf("%9d quarters\n", quarters);         printf("%9d dimes\n", dimes);         printf("%9d nickels\n", nickels);         printf("%9d pennies\n", pennies);     }     return 0; } void change(float total, int *quarters, int *dimes, int *nickels, int *pennies) {                    if(total >= 0.25)         *quarters = (total/0.25);         if(total >= 0.10)         *dimes = (total - (*quarters * 0.25))/0.10;         if(total >= 0.05)         *nickels = (total - (*quarters * 0.25) - (*dimes * 0.10))/0.05;         if(total >= 0.05)         *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01;             return ; }```
this is just one of the ways I am trying it now i am going to try a "for" and a "while" statement with "%" division... wish me luck...