Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By laserlight

C-another Pointer program.

This is a discussion on C-another Pointer program. within the C Programming forums, part of the General Programming Boards category; 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); ...

  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
    22,271
    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.
    5360 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,271
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,271
    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.
    5360 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:36 PM
  2. Help on simple pointer program
    By KrazySocoKid in forum C Programming
    Replies: 9
    Last Post: 10-21-2011, 11:24 AM
  3. Help on another pointer program
    By KrazySocoKid in forum C Programming
    Replies: 7
    Last Post: 10-21-2011, 09: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, 11:51 PM
  5. Help with a pointer program
    By steve568 in forum C Programming
    Replies: 1
    Last Post: 11-14-2008, 06:51 AM

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