Insert and Remove functions

This is a discussion on Insert and Remove functions within the C Programming forums, part of the General Programming Boards category; I have a program for my Computer Science class that dealt with adding and subtracting coins from a pot. But, ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    18

    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
    Last edited by arctic_blizzard; 09-27-2008 at 10:59 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    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. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    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
    addCoins( );

    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. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    Code:
    #include<stdio.h>
    void addCoins();
    
    void main()
    {
    ...
    }
    
    
    void addCoins()
    {
    ...
    }

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    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. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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)
    {
       changePotContents(3, 6, 3, 1, op_add);
    
       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. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    I just cannot get this one down.

    Code:
    #include <stdio.h>
    int pennies, nickels, dimes, quarters, p, n, d, q;
    float pot;
    void addChange();
    void lessChange();
    
    void main()
    {   
    
    
    printf( " %d quarters + %d dimes + %d nickels + %d pennies = $%d \n", quarters, dimes, nickels, pennies, pot);
    
    addChange();
    lessChange();
    
    system("pause");
     }
     
     void addChange()
     {
          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. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    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 addChange();
    void lessChange();
    void displayChange();
    
    void main()
     {     
    
           displayChange();
           addChange();
           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);
     }
    
     void addChange()
     {
          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
    Last edited by arctic_blizzard; 09-28-2008 at 10:10 AM. Reason: Figured out inputs/outputs

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    Thanks, it works perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to take a string input and remove whitespace?
    By Jasonx521 in forum C Programming
    Replies: 5
    Last Post: 10-06-2006, 11:24 PM
  2. Question about references
    By azncoolj2000 in forum C++ Programming
    Replies: 6
    Last Post: 11-18-2005, 04:27 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Replies: 4
    Last Post: 10-21-2003, 05:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21