# Thread: Help, home work code provided. Totally Lost.

1. ## Help, home work code provided. Totally Lost.

Okay first of all thank you for looking at this I know HW questions can be pain staking.

Here is the question posted and i did searches in the forum and with no results to help.

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

Here is what I have.
Code:
```#include <stdio.h>
#include <math.h>
int main()
{
float quarters, nickels, dimes, pennies, total;
int final;
char stopprogram;

void change (quarters, nickels, dimes, pennies, total);
total=1.88;
printf("The Total Entered: %f\n",total);
printf("%d quarters\n", quarters);
printf("%d nickels\n", nickels);
printf("%d dimes\n", dimes);
printf("%d pennies\n\n", pennies);

void change (quarters, nickels, dimes, pennies, total);
total=.32;
printf("The Total Entered: %f\n",total);
printf("%d quarters\n", quarters);
printf("%d nickels\n", nickels);
printf("%d dimes\n", dimes);
printf("%d pennies\n\n", pennies);

scanf("%f", &total);
void change (quarters, nickels, dimes, pennies, total);
printf("The Total Entered: %f\n",total);
printf("%d quarters\n", quarters);
printf("%d nickels\n", nickels);
printf("%d dimes\n", dimes);
printf("%d pennies\n", pennies);
scanf("%c", &stopprogram);

return 0;
}
float change (*quarters, *nickels, *dimes, *pennies, *total);
{
valuequarter=.25
valuedime=.10
valuenickel=.05
valuepenny=.01

*quarters= truncf(*total/valuequarter);
*total=*total-*quarters;
*dimes=truncf(*total/valuedime);
*total=*total-*dimes;
*nickels=truncf(*total/valuenickel);
*total=*total-*nickels;
*pennies=truncf(*total/valuepenny);
*total=*total-*pennies;

return 0;

}```

compiler errors.

C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp: In function `int main()':
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: variable or field `change' declared void
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: initializer expression list treated as compound expression
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: warning: converting to `int' from `float'

C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:17: error: variable or field `change' declared void
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:17: error: redeclaration of `int change'
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: `int change' previously declared here
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:28: error: variable or field `change' declared void
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:28: error: redeclaration of `int change'
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: `int change' previously declared here
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp: At global scope:
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `quarters' was not declared in this scope
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `nickels' was not declared in this scope
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `dimes' was not declared in this scope
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `pennies' was not declared in this scope
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `total' was not declared in this scope
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: initializer expression list treated as compound expression
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:39: error: expected unqualified-id before '{' token
C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:39: error: expected `,' or `;' before '{' token

Execution terminated

3. You got a lot of nasty stuff in there, dude. Here's what I found.

1) You prototyped your function saying that it returns void, but then you make it return float. Bad news.
2) You tell the compiler that change should accept pennies, quarters and such without explaining what these things are. (You probably actually want something like :
float change(float *pennies, float* quarters, etc...)
3) You prototype your change function a whopping 3 times ! Only one is necessary, and it is sufficient to declare it outside main (which is good coding practice as well)
4) You don't send total to your change function. So how is it supposed to know what to do when you say *total ?
5) You forgot semi-colons after all those valuepennies = 0.01, valuequarters = 0.25 and such.

There is more. But fix those first.

4. You also pass the objects rather than the addresses to the function change(). Oh, and you defined change() twice... Oh, and you have an extra semi-colon after the second declaration of change(). While we're at it, your change() function *always* returns 0... you can make that a void function and never return anything. There's still a lot more to correct but fix all these for now.

5. Originally Posted by Happy_Reaper
You got a lot of nasty stuff in there, dude. Here's what I found.

1) You prototyped your function saying that it returns void, but then you make it return float. Bad news.
2) You tell the compiler that change should accept pennies, quarters and such without explaining what these things are. (You probably actually want something like :
float change(float *pennies, float* quarters, etc...)
3) You prototype your change function a whopping 3 times ! Only one is necessary, and it is sufficient to declare it outside main (which is good coding practice as well)
4) You don't send total to your change function. So how is it supposed to know what to do when you say *total ?
5) You forgot semi-colons after all those valuepennies = 0.01, valuequarters = 0.25 and such.

There is more. But fix those first.
#1 Changed them all to float
#3 I thought by it making a first call second call third call i had to direct to the function redeclaring the total values
#4 not sure what you are saying the *total was to be a pointer to the total value
#5 fixed that please let me know what else here is updated code I changed the names of the *quarters *dimes and so on.

Code:
```#include <stdio.h>
#include <math.h>
int main()
{
float quarters, nickels, dimes, pennies, total;
char stopprogram;

float change (quarters, dimes, nickels, pennies, total);
total=1.88;
printf("The Total Entered: %f\n",total);
printf("%d quarters\n", quarters);
printf("%d nickels\n", nickels);
printf("%d dimes\n", dimes);
printf("%d pennies\n\n", pennies);

float change (quarters, dimes, nickels, pennies, total);
total=.32;
printf("The Total Entered: %f\n",total);
printf("%d quarters\n", quarters);
printf("%d nickels\n", nickels);
printf("%d dimes\n", dimes);
printf("%d pennies\n\n", pennies);

scanf("%f", &total);
float change (quarters, dimes, nickels, pennies, total);
printf("The Total Entered: %f\n",total);
printf("%d quarters\n", quarters);
printf("%d nickels\n", nickels);
printf("%d dimes\n", dimes);
printf("%d pennies\n", pennies);
scanf("%c", &stopprogram);

return 0;
}
float change (float *aq,float *bd,float *cn,float *dp,float *total);
{
valuequarter=.25;
valuedime=.10;
valuenickel=.05;
valuepenny=.01;

*aq= truncf(*total/valuequarter);
*total=*total-*aq;
*bd=truncf(*total/valuedime);
*total=*total-*bd;
*cn=truncf(*total/valuenickel);
*total=*total-*cn;
*dp=truncf(*total/valuepenny);
*total=*total-*dp;

}```

And again, you don't have to prototype 3 times. One time will suffice.

ex :

Code:
```float foo(float * bar);

int main(void)
{
float b;
b = 0.6;
foo(&b);
return 0;
}```
After you've prototyped it, you can just call it by its name any time you want.

7. ## Okay so I updated it :)

Here is a new program more organized.

Let me know where I went wrong I may not be sending the info to the change function I am unsure where my information breakdown is.

Code:
```#include <stdio.h>
#include <math.h>
#define quarter 25
#define dime 10
#define nickel 5
#define penny 1

float change(float *total);

int main()
{
float total;
total=1.88;
printf("The Total Entered: %4.2f\n\n",total);
total=total*100;
change(&total);

total=0.32;
printf("The Total Entered: %4.2f\n\n",total);
total=total*100;
change(&total);

printf("Please enter a monetary value: ");
scanf("%f",&total);
printf("The Total Entered: %8.2f\n\n",total);
total=total*100;
change(&total);

return 0;
}
float change(float *total)
{
float quarters, dimes, nickels, pennies;
float amt;
quarters=truncf(amt/quarter);
amt=amt-(quarters*quarter);
dimes=truncf(amt/dime);
amt=amt-(dimes*dime);
nickels=truncf(amt/nickel);
amt=amt-(nickels*nickel);
pennies=truncf(amt/penny);
amt=amt-(pennies*penny);

printf("%d quarters\n",quarters);
printf("%d dimes\n",dimes);
printf("%d nickels\n",nickels);
printf("%d pennies\n\n",pennies);

}```

8. truncf() is C99. Could this be the problem.

9. the question wants the values to be sent to this function by address
Code:
`float change(float *total);`
should be

Code:
`float change(int *quarters, int *nickels, int *dimes, int *pennies, float total);`
total doesn't have to be a pointer...we're not changing it in change

now in main

Code:
```   int quarters, nickels, dimes, pennies;

...
...
...
change(&quarters, &nickels, &dimes, &pennies, total);

printf("%d quarters\n", quarters);
printf("%d dimes\n", dimes);
printf("%d nickels\n", nickels);
printf("%d pennies\n\n", pennies);```

10. Mabe this will help to geter done.
Code:
```void change(float *total)
{
int quarters, dimes, nickels, pennies;
int amt = (int) *total;
quarters = amt / quarter;
amt -= quarters * quarter;
dimes = amt / dime;
amt -= dimes * dime;
nickels = amt / nickel;
amt -= nickels * nickel;
pennies =  amt / penny;
amt -= pennies * penny;

printf("%d quarters\n",quarters);
printf("%d dimes\n",dimes);
printf("%d nickels\n",nickels);
printf("%d pennies\n\n",pennies);
}```

11. here is the final code that worked. I appreciate the help. You guys got a good place to learn pointers?

if i understand in the prototype of the function and the actual header line i am pointing the *total to total and sending it to the change program to be manipulated.
if i am correct maybe an explanation i got kind of lost on whree info was going as I wrote it.

I do PLC and programming and Robotics and C is a lot different than what i use .

Code:
```#include <stdio.h>
#include <math.h>
#define quarter 25
#define dime 10
#define nickel 5
#define penny 1

void change(float *total);

int main()
{
float total;
total=1.88;
printf("The Total Entered: %4.2f\n\n",total);
total=total*100;
change(&total);

total=0.32;
printf("The Total Entered: %4.2f\n\n",total);
total=total*100;
change(&total);

printf("Please enter a monetary value: ");
scanf("%f",&total);
printf("The Total Entered: %8.2f\n\n",total);
total=total*100;
change(&total);

return 0;
}
void change(float *total)
{
int quarters, dimes, nickels, pennies;
int amt = (int) *total;
quarters = amt / quarter;
amt -= quarters * quarter;
dimes = amt / dime;
amt -= dimes * dime;
nickels = amt / nickel;
amt -= nickels * nickel;
pennies =  amt / penny;
amt -= pennies * penny;

printf("%d quarters\n",quarters);
printf("%d dimes\n",dimes);
printf("%d nickels\n",nickels);
printf("%d pennies\n\n",pennies);
}```

12. total does not have to be sent as a pointer since it is not being changed by the change function, then used by the main function with the new value...if you read my post you'll see the correct way to impliment the change function, based on the specifications you provided.

when implimented this way, the variable amt will also not be needed...since total can just be used.

looking at it again, it should be void change, not float change, since your not returning and don't need to return a value