Thread: C-another Pointer program.

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    8

    C-another Pointer program.

    Code:
    #include <stdio.h>
    
    int f(int c, int b, int a);
    
    
    int main() {
        int a=2, b=3, c=5;
        printf("a=%d b=%d c=%d\n",a,b,c);
        
        a = f(b, a, b+c);
        printf("a=%d b=%d c=%d\n",a,b,c);
        
    
    
        system("pause");
        return 0;
    }
    
    
    int f(int c, int b, int a) {
        int sum;
        sum = a + b + c;
        if(sum > a*c)
            return a*c;
        if(sum <= b*c)
            return b*c;
            
        return a*b;
    }
    Output:
    a=2 b=3 c=5
    a=16 b=3 c=5

    I am starting to understand pointer but still confused.
    Does sum equal to 13? If so, what is a,b,c and where to obtain the variables for a,b,c: a+b+c=13?
    Code:
        if(sum > a*c)        return a*c;
        if(sum <= b*c)
            return b*c;
            
        return a*b;
    Why are there two returns assuming one of the "if" statement is true?

    Lastly, what does
    Code:
        a = f(b, a, b+c);
    implies?

    Thank You.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 5360
    I am starting to understand pointer but still confused.
    I note that you do not directly use any pointers in your program.

    Quote Originally Posted by 5360
    Does sum equal to 13?
    Use a debugger to find out. If one is not available to you, then print the value of sum at the point where you want to know what it is equal to.

    Quote Originally Posted by 5360
    If so, what is a,b,c and where to obtain the variables for a,b,c: a+b+c=13?
    It looks like your assignment is designed for confusion. I suggest that you consider this program, which is exactly the same as the program you posted except for variable names:
    Code:
    #include <stdio.h>
    
    int f(int x, int y, int z);
    
    
    int main() {
        int a=2, b=3, c=5;
        printf("a=%d b=%d c=%d\n",a,b,c);
    
        a = f(b, a, b+c);
        printf("a=%d b=%d c=%d\n",a,b,c);
    
    
    
        system("pause");
        return 0;
    }
    
    
    int f(int x, int y, int z) {
        int sum;
        sum = z + y + x;
        if(sum > z * x)
            return z*x;
        if(sum <= y * x)
            return y * x;
    
        return z * y;
    }
    Quote Originally Posted by 5360
    Why are there two returns assuming one of the "if" statement is true?
    The first of the two for which the if statement's condition evaluates to true will be used.

    Quote Originally Posted by 5360
    Lastly, what does
    Code:
    a = f(b, a, b+c);
    implies?
    That is just a function call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    8
    Ok using int x, y z clears up some of my questions and sum does equal to 13.

    One last question, how do I find the variables for x,y, and z that will equal to sum?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 5360
    One last question, how do I find the variables for x,y, and z that will equal to sum?
    What do you mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    8
    Quote Originally Posted by laserlight View Post
    What do you mean?
    what is the value of x? or another way to ask: how do I find the value of x?
    what is the value of y?
    what is the value of z?

    I just cannot get my head wrap around it...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider a simpler example:
    Code:
    #include <stdio.h>
    
    void print_number(int n)
    {
        printf("%d\n");
    }
    
    int main(void)
    {
        print_number(10 + 20);
        return 0;
    }
    So, the value of the formal parameter n is the value of the actual argument passed when calling print_number, i.e., 10 + 20, hence the value of n is 30.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Consider a simpler example:
    Code:
    #include <stdio.h>
    
    void print_number(int n)
    {
        printf("%d\n");
    }
    
    int main(void)
    {
        print_number(10 + 20);
        return 0;
    }
    So, the value of the formal parameter n is the value of the actual argument passed when calling print_number, i.e., 10 + 20, hence the value of n is 30.
    I finally see it, thank you very much!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help On Pointer Sieve of E program
    By KrazySocoKid in forum C Programming
    Replies: 8
    Last Post: 10-27-2011, 03:36 PM
  2. Help on simple pointer program
    By KrazySocoKid in forum C Programming
    Replies: 9
    Last Post: 10-21-2011, 10:24 AM
  3. Help on another pointer program
    By KrazySocoKid in forum C Programming
    Replies: 7
    Last Post: 10-21-2011, 08:49 AM
  4. Need help with this error in pointer program
    By camel-man in forum C Programming
    Replies: 20
    Last Post: 09-09-2011, 10:51 PM
  5. Help with a pointer program
    By steve568 in forum C Programming
    Replies: 1
    Last Post: 11-14-2008, 06:51 AM