# Thread: Insert and Remove functions

1. ## Insert and Remove functions

I have a program for my Computer Science class that dealt with adding and subtracting coins from a pot. But, apparantely the program is isn't what was looked for.
Here is the original program:
Code:
```#include <stdio.h>

void main()
{
int pennies, nickels, dimes, quarters, p,n,d,q;

pennies=0;
nickels=0;
dimes=0;
quarters=0;

float x = pennies + 5*nickels + 10*dimes + 25*quarters;
x =  (float)x/100;

printf( " &#37;d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, x);

printf("insert");
scanf(“%d%d%d%d“, &p, &n, &d, &q);

pennies += p;
nickels += n;
dimes += d;
quarters += q;

x = pennies + 5*nickels + 10*dimes + 25*quarters;
x =  (float)x/100;

printf( " %d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, x);

printf("remove");
scanf(“%d%d%d%d“, &p, &n, &d, &q);

pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;

x = pennies + 5*nickels + 10*dimes + 25*quarters;
x =  (float)x/100;

printf( " %d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, x);

}```
I talked with my prof, and he said that a user defined function needs to be added to perform insert and remove, and also a functions to display a new string that shows the new content of the pot.

Any hints or guidance would be greatly appreciated!

Thanks,

~AB

2. use &#37;f for float instead of %d

He wants you to create functions like this
Code:
```int insert()
{
codes here...
}```
btw the double quotes in scanf is ........ed up

3. Am I on the right track?

I need to create two separate functions (addCoins, and removeCoins)
and when I need to use them, I can just call them into my main function by scripting

These separate functions would have all of the code I need to execute the command,
so, addCoins would look similar to this:
Code:
```addCoins( )
{
printf( " %d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, x);

printf("insert");
scanf(“%d%d%d%d“, &p, &n, &d, &q);

pennies += p;
nickels += n;
dimes += d;
quarters += q;

x = pennies + 5*nickels + 10*dimes + 25*quarters;
x =  (float)x/100;
}```
If this is correct, where would this subroutine be added in the programming code?

4. Code:
```#include<stdio.h>

void main()
{
...
}

{
...
}```

5. This is prototyping correct?

I remember reading in one of my textbooks that you can avoid this by writing your program upsidedown, listing the subroutine functions before the main function.

Is this a common practice for those of you who are programming gurus, or does it make the program unconventional?

6. Yes that's prototyping: the function header without the body is put at the top so that the compiler can see that you're calling the function correctly with the same number of arguments, argument types, and return value is OK (if any).

I don't know if it's considered unconventional. I do it that way all the time for small self contained programs... putting the functions at the top. It means I don't have to maintain the prototypes whenever I change the way a function looks.

But in practice with any larger program which has multiple source files, I end up putting prototypes into common header files just like the #include for standard things.

7. It's difficult to say exactly what is wanted without knowing more about where you are in the learning process.

I can see something like this, assuming the pot is a global variable:
Code:
```void addCoins(const int quarters, const int dimes, const int nickels, const int pennies)
{
pot += (quarters*25 + dimes*10 + nickels*5 + pennies*1);
}
void removeCoins(const int quarters, const int dimes, const int nickels, const int pennies)
{
pot -= (quarters*25 + dimes*10 + nickels*5 + pennies*1);
}```
or be clever and avoid code duplication

Code:
```#include <stdio.h>

int pot = 0;
enum operation { op_add = 1, op_remove = -1 };

void changePotContents(const int quarters, const int dimes, const int nickels, const int pennies, const enum operation op)
{
pot += (quarters*25 + dimes*10 + nickels*5 + pennies*1) * op;
}

int main(void)
{

printf("Pot is \$%.2f\n", (float)pot / 100);

changePotContents(2, 3, 1, 7, op_remove);

printf("Pot is \$%.2f\n", (float)pot / 100);

return 0;
}```

8. So on what i could see so far and the information gives us so far. So you got the job done, he jiust wants to see some function doing the same job. But you have done it all in the same function main.

You need three function ADD, REMOVE and DISPLAY.

now then, ADD function should be declared with quite some parameters ent to it. I again assume that you pot variable is global. But using global for this purpose is not safe. OR Perhaps are adviced. Try sending the pot as a parameter to the function and then try return the pot variable.

Code:
`float ADD(int, int, int, int, int);`
But note thats just the function prototype.

And same should apply for the other few functions as well.

ssharish

9. I just cannot get this one down.

Code:
```#include <stdio.h>
int pennies, nickels, dimes, quarters, p, n, d, q;
float pot;
void lessChange();

void main()
{

printf( " %d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, pot);

lessChange();

system("pause");
}

{
printf("insert coins:\n");
scanf("%d%d%d%d", &p, &n, &d, &q);
float pot = (pennies + 5*nickels + 10*dimes + 25*quarters)/100;
pennies += p;
nickels += n;
dimes += d;
quarters += q;

printf(" %d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, pot);
}

void lessChange()
{
printf("remove coins:\n");
scanf("%d%d%d%d", &p, &n, &d, &q);
float pot = (pennies + 5*nickels + 10*dimes + 25*quarters)/100;
pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;

printf(" %d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, pot);
}```
the numbers returned are not good, and once the add function runs, it does not ask for the input for the remove function.

10. Well, the output for me seems to be good. What output are you getting??

Perhaps, you should clear the input buffer before you lesschange. This is not necessary, its working fine on my machine without that! But best try and see of you can get called lesschange function.

ssharish

11. OK, I have the inputs and outputs working correctly.

and I have created a new function to display the pot contents

Code:
```#include <stdio.h>
int pennies, nickels, dimes, quarters, p, n, d, q;
float pot;
void lessChange();
void displayChange();

void main()
{

displayChange();
displayChange();
lessChange();
displayChange();

system("pause");
}

void displayChange()
{
float pot = (pennies + 5*nickels + 10*dimes + 25*quarters)/100;
printf( " &#37;d quarters + %d dimes + %d nickels + %d pennies = \$%d \n", quarters, dimes, nickels, pennies, pot);
}

{
printf("insert coins:\n");
scanf("%d%d%d%d", &p, &n, &d, &q);
float pot = (pennies + 5*nickels + 10*dimes + 25*quarters)/100;
pennies += p;
nickels += n;
dimes += d;
quarters += q;

}

void lessChange()
{
printf("remove coins:\n");
scanf("%d%d%d%d", &p, &n, &d, &q);
float pot = (pennies + 5*nickels + 10*dimes + 25*quarters)/100;
pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;

}```
But I am still getting an incorrect total when it displays:

ie
"1 quarter + 2 dimes + 3 nickels + 4 pennies = \$0."

Where does my error occur?

Thanks everybody for all of their help

12. Code:
```void displayChange()
{
float pot = (pennies + 5*nickels + 10*dimes + 25*quarters)/(float)100;
printf( " %d quarters + %d dimes + %d nickels + %d pennies = \$%.2f \n", quarters, dimes, nickels, pennies, pot);
}```

13. Thanks, it works perfectly!