Thread: How do I?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    How do I?

    How can I reduce this to having only one function perform the job?

    Code:
    #include <stdio.h>
    
    int i,g,q,p,c;
    int liquid1 (int);
    int liquid2 (int);
    int liquid3 (int);
    int liquid4 (int);
    int main()
    {
    printf("Enter cups to be converted:");
    scanf ("%d\n",&i);
    printf ("gallons = %d",liquid1 (i));
    printf ("quarts = %d",liquid2 (i));
    printf ("pints = %d",liquid3 (i));
    printf ("cups = %d",liquid4 (i));
    
    system ("PAUSE");
    
    return 0;
    }
    
    int liquid1 (int i)
    {
    int i,g;
    g = i/16;
    return (g);
    }
    int liquid2 (int i)
    {
    int i,q;
    q=(i%16)/4;
    return (q);
    }
    int liquid3 (int i)
    {
    int i,p;
    p=((i%16)%4)/2;
    return (p);
    }
    int liquid4 (int i)
    {
    int i,c;
    c=((i%16)%4)%2;
    return (c);
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    
    int liquid1(int, int);  // have a choice as a second parameter
    
    int main()
    {
        int i,g,q,p,c;   // dont use global variables
        
        printf("Enter cups to be converted:");
        scanf ("%d\n",&i);
    
        printf ("gallons = %d",liquid1 (i, 1));
        printf ("quarts = %d",liquid1 (i,2));
        printf ("pints = %d",liquid1 (i,3));
        printf ("cups = %d",liquid1 (i,4));
    
        getchar(); // use getchar rather than system & PAUSE
        return 0;
    }
    
    int liquid1 (int i, int ch)
    {
        int g;
        
        switch(ch)  // use switch to switch to the correspsonding case
        {
            case 1:
                g = i/16;
                break;
            case 2:
                g=(i%16)/4;
                break;
            case 3:
                g=((i%16)%4)/2;
                break;
            case 4:
                g=((i%16)%4)%2;
                break;
        }
        return g;    // return the result
    }
    u need proper indendation

    ssharish2005
    Last edited by ssharish2005; 12-29-2006 at 11:47 AM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    How can I do it using pointers, the bane of my exsistance?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well dint notice one thing in u'r code

    Code:
    scanf ("%d\n",&i);
    u dont need that \n there. Which bascially means u got to enter the \n when every u enter a value for i.

    How can I do it using pointers, the bane of my exsistance?
    So what was your actual problem given to u. Which part need to be in pointer.

    ssharish2005

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Very simple example:

    Code:
    void pointer_function ( int *value )
    {
    	*value /= 10;
    }
    
    int main( void )
    {
        int num = 4235232;
    
    	printf ( "Before Function: %d\n", num );
    	pointer_function ( &num );
    	printf ( "After Function:  %d\n", num );
        
        return 0;
    }
    Just adapt that to your function and it should work.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    but note your actual i value will be altered. so for example if your i value had 10 and when u call i with the liquid1 function the i value becomes 0 ( (int) 10/16 == 0.652). And when u send the i value to liquid2 u are sending i which is now 0

    ssharish2005

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Your code will either fail to compile or produce undefined results, because your liquid* functions all contain a local variable i that either masks or conflicts with the parameter i.

    That problem aside, all functions do different things. There is no sensible way to make them one function. (I do hope what ssharish posted was meant sarcastically.)

    Of course, since all functions also are simply one calculation, you can substitute the calls with the expression. Oh, and (x % 2) is the same thing as ((x % 16) % 2) or stuff like that.
    Last edited by CornedBee; 12-29-2006 at 12:39 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Thanks for the help

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, using system("PAUSE") is a bad idea.


    If you must use it, at least include <stdlib.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed