1. Change function

Below is the the assignment. Now I wrote some code I thought would work, but recieving so many errors that I can't understand, I reduced the code to just find the amount of quarters needed. Once I can clear that up, I should able to finalize the assignment. I think I am not adressing quarters properly. I have searched the forums and read the text required for the class and other tuturiols.

1. 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

Code:
```#include <stdio.h>
int change (float, int*);  /* function prototype */
int main()
{
int *quarters;
float total;

total = 1.88;

change(total, quarters);

printf("TOTAL VALUE ENTERED: \$%6.2f\n", total);
printf("%d quarters", quarters);

return 0;
}
int change(float total, int *quarters)
{
if( total >= 0.25 )
*quarters = (total/0.25);

return;
}```
Here is the window pop-up error I get.
Run-Time Check Failure #3 - The variable 'quarters' is being used without being initialized.

2. There are surprisingly many problems with this code.

1) Don't use int main() in C use either, int main(void) or int main(int argc, char *argv[]) if you need to use command line arguments.

2) Function change is supposed to return an int value yet it doesn't.

3) Avoid doing floating point arithmetic division and assigning the result to an integer.

4) I don't understand why you are complicating this for no reason and using pointers, when you clearly don't know to use them, and don't design your function change to return a value that you can grab in another variable in main:

chg = change(total);

5) If you DO use pointers don't printf the address the pointer change is pointing to but rather the value contained at that address.

3. To elaborate on what claudiu said about the pointers, your pointer is not pointing to anything, so when you try to change its value you are resulting in undefined behavior. That is what your compiler is warning you about, as you asked.

4. Updated code/ with one problem

Ok so I finally got my head almost wrapped around this. The last computation for the pennies doesn't produce the correct results. I know the problem has to do with the function but not sure why. Any ideas?
Code:
```#include <stdio.h>
int change (float total, int *quarters, int *dimes, int *nickels, int *pennies);  /* function prototype */
int main()
{
int quarters, dimes, nickels, pennies;
float total;
{
total = 1.88;

change (total, &quarters, &dimes, &nickels, &pennies);
printf("TOTAL VALUE ENTERED: \$%6.2f", total);
printf("\n%d quarters\n", quarters);
printf("\n%3d dimes\n", dimes);
printf("\n%3d nickels\n", nickels);
printf("\n%3d pennies\n", pennies);
}
{
total=0.32;

change(total, &quarters, &dimes, &nickels, &pennies);
printf("\nTOTAL VALUE ENTERED: %6.2f",total);
printf("\n%d quarters\n",quarters);
printf("\n%d dimes\n", dimes);
printf("\n%d nickles\n", nickels);
printf("\n%d pennies\n",pennies);
}
{
printf("\nPlease enter an amount of money: \n");
scanf("%f", &total);

change(total, &quarters, &dimes, &nickels, &pennies);
printf("TOTAL VALUE ENTERED: %6.2f",total);
printf("\n%d quarters\n",quarters);
printf("\n%d dimes\n",dimes);
printf("\n%d nickles\n",nickels);
printf("\n%d pennies\n",pennies);
}

return 0;
}
int 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.04 >0)
*pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01;

return;```

5. What is the meaning of "if( total <= 0.04 >0)"?

6. Originally Posted by cody.carter
Ok so I finally got my head almost wrapped around this. The last computation for the pennies doesn't produce the correct results. I know the problem has to do with the function but not sure why. Any ideas?
Code:
```#include <stdio.h>
int change (float total, int *quarters, int *dimes, int *nickels, int *pennies);  /* function prototype */
int main()
{
int quarters, dimes, nickels, pennies;
float total;
{
total = 1.88;

change (total, &quarters, &dimes, &nickels, &pennies);
printf("TOTAL VALUE ENTERED: \$%6.2f", total);
printf("\n%d quarters\n", quarters);
printf("\n%3d dimes\n", dimes);
printf("\n%3d nickels\n", nickels);
printf("\n%3d pennies\n", pennies);
}
{
total=0.32;

change(total, &quarters, &dimes, &nickels, &pennies);
printf("\nTOTAL VALUE ENTERED: %6.2f",total);
printf("\n%d quarters\n",quarters);
printf("\n%d dimes\n", dimes);
printf("\n%d nickles\n", nickels);
printf("\n%d pennies\n",pennies);
}
{
printf("\nPlease enter an amount of money: \n");
scanf("%f", &total);

change(total, &quarters, &dimes, &nickels, &pennies);
printf("TOTAL VALUE ENTERED: %6.2f",total);
printf("\n%d quarters\n",quarters);
printf("\n%d dimes\n",dimes);
printf("\n%d nickles\n",nickels);
printf("\n%d pennies\n",pennies);
}

return 0;
}
int 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.04 >0)
*pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01;

return;```

7. Originally Posted by claudiu
If you don't want to answers why do you insist on instigating conflict.

I am student just trying to follow the assignment I was given. While your previous advice was somewhat helpful, most of it was and still is over my head right now. So maybe you could bring you advice down to a beginners level.

Thank you.

8. Originally Posted by claudiu
There are surprisingly many problems with this code.

1) Don't use int main() in C use either, int main(void) or int main(int argc, char *argv[]) if you need to use command line arguments.

2) Function change is supposed to return an int value yet it doesn't.

3) Avoid doing floating point arithmetic division and assigning the result to an integer.

4) I don't understand why you are complicating this for no reason and using pointers, when you clearly don't know to use them, and don't design your function change to return a value that you can grab in another variable in main:

chg = change(total);

5) If you DO use pointers don't printf the address the pointer change is pointing to but rather the value contained at that address.
What is over your level in this? Perhaps you can elaborate.

9. Originally Posted by tabstop
What is the meaning of "if( total <= 0.04 >0)"?
Is the total less than or equal to 4 or greater than 0. But as I was typing I realized it should say greater than or equal 4 like below.

"if (total >= 0.04)", but this returned one penny less than what the results should be. Back to the drawing board.

Thanks for the prompt.

10. Its funny how from time to time we get 40 people in here all asking about how to create a function to generate change, do airline ticketing/routing, etc and all at the same time. I just left a thread in the C++ forum asking the same exact question and I swear there was someone else asking this about two days ago...and Cody if you feel put-upon, remember that we are human too and sometimes we have bad weeks or whatever and get tired of answering the same exact questions over and over. If you need some simpler logic to solving this, look at what I posted in the C++ forum within the past 24 hours. The solution is basic C even though it was asked in that area...not that it is a superior solution, just that it is simpler than what you posted here (and thus easier to understand) and it has the benefit of working...

11. Originally Posted by claudiu
What is over your level in this? Perhaps you can elaborate.
I'm still not quite familar will all the terms and functions, yet.

Also the assignment call for pointers and you suggested not to use them.

How do you avoid doing floating point arithmetic division and assigning the result to an integer, when the assignment calls for it?

Just trying to learn, dude.

12. Originally Posted by cody.carter
I'm still not quite familar will all the terms and functions, yet.

Also the assignment call for pointers and you suggested not to use them.

How do you avoid doing floating point arithmetic division and assigning the result to an integer, when the assignment calls for it?

Just trying to learn, dude.
There is no floating point arithmetic division involved in monetary transactions, as we have tried (and apparently failed) to point out. There are only whole numbers of pennies.

13. Originally Posted by jeffcobb
Its funny how from time to time we get 40 people in here all asking about how to create a function to generate change, do airline ticketing/routing, etc and all at the same time. I just left a thread in the C++ forum asking the same exact question and I swear there was someone else asking this about two days ago...and Cody if you feel put-upon, remember that we are human too and sometimes we have bad weeks or whatever and get tired of answering the same exact questions over and over. If you need some simpler logic to solving this, look at what I posted in the C++ forum within the past 24 hours. The solution is basic C even though it was asked in that area...not that it is a superior solution, just that it is simpler than what you posted here (and thus easier to understand) and it has the benefit of working...
Thanks, its just a struggle to learn the terminology, plus it seems the book is written greek. It's phenominal what you guys do with this stuff. Once, I make it through this class, I'll leave this stuff for the pros. I rather be back in Afghanistan than to be doing this, but its a degree requirement. Thanks for putting up with students like myself.

14. I would do this something like this, to avoid floating points. Not sure if you still need help, but this might make things clearer if you are still stuck.

First, convert the change you are given to pennies (ie, an integer) by multiplying by 100 and just casting it to integer. Ie, if you are given 1.88, change it to 188.

To get the number of quarters, do the amount / 25

ie, 188/25 = 7

to get the remaining amount of change, do 188%25 = 13

to get the number of dimes, take the remainder and div by 10

13/10 = 1

to get the remainder, 13%10 = 3

repeat for nickels and pennies.

This should be a 10 line program, really, I think you are just overcomplicating the problem by using pointers and such.

15. Originally Posted by KBriggs
This should be a 10 line program, really, I think you are just overcomplicating the problem by using pointers and such.
It's not necessarily overcomplicated if he has to use a function to calculate the change. In that case, it would best to use a function that returns all four numbers at once rather than four functions to return the number of each coin separately.

So, Cody, do you have to use functions (other than main) in your program?