Thread: in need of a little assistance

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    in need of a little assistance

    so i am given this code

    insert
    Code:
    #include <stdio.h>
     
    int function2(int a, int b);
     
    int main() {   
        int a = 1, b = 2;
        b = function2(a,b);
        printf("a = %d, b = %d\n", a, b);
        a = function2(b,a);
        printf("a = %d, b = %d\n", a, b);
       
        return 0;
    }
     
    int function2(int a, int b) {
        int temp = a+b;
        a = 3*temp - (2*b)%15;
        b = a%temp;
        printf("a = %d, b = %d\n", a, b);
        return 2*a - b;
    }

    and then this question

    3) What is the first line of output produced by this program?

    A) a = 2, b = 5
    B) a = 5, b = 2
    C) a = 5, b = 1
    D) a = 5, b =
    E) a = 3, b = 5


    I know the answer is B but what confuses me is how to get that answers?

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    The first thing that will be printed is the printf-statement in function2(...). You know the parameter values passed to function2, so just follow the statements in each row, writing down the contents of each variable as you go along. What exactly is it that confuses you?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    this is probably a dumb question but i just dont see how "a" can = 5? im pretty new to this... and having a hard time...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    I commented the code below for you:

    Code:
    int function2(int a, int b) {      /* called with a = 1, b = 2    */    
        int temp = a+b;                /* temp = a + b = 1 + 2  = 3   */        
        a = 3*temp - (2*b)%15;         /* a = 3 * temp - (2 * b) % 15 
                                        *   = 3 * 3 - ( 2 * 2 ) % 15
                                        *   = 9 - 4 % 15 = 
                                        *   = 9 - 4 = 5                */    
        b = a%temp;                    /* b = a % temp = 5 % 3 = 2     */    
        printf("a = %d, b = %d\n", a, b);
        return 2*a - b;
    }

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    so how would i go about finding the 2nd line of output? it says the answer is a=1 and b=8 but i keep getting a different number...

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by unknown2007 View Post
    so how would i go about finding the 2nd line of output? it says the answer is a=1 and b=8 but i keep getting a different number...
    Try to do what I just did. Comment the code, line by line (or do it on paper) to get a feeling of what happens. The second line to be printed is the call to printf just after the call to function2().

    How are you calculating the output?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    well i thought i would have to change the values from a=1 b=2 to a=5 b=2 and then just run the calculations again just like the first time...
    sooo i tried temp=5+2
    a=3*7-(2*2)%15=17
    b=17%7=3...
    im assuming that is wrong...

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by unknown2007 View Post
    well i thought i would have to change the values from a=1 b=2 to a=5 b=2 and then just run the calculations again just like the first time...
    sooo i tried temp=5+2
    a=3*7-(2*2)%15=17
    b=17%7=3...
    im assuming that is wrong...
    You already know what a and b is at the end of function2, 5 and 2. function2 returns 2*a - b, which would be 10 - 2 = 8. The return value from function2 is assigned to variable b in main. Since function2 does not modify its arguments, a still remains 1 as it was defined in the beginning of main.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    thanks so much! it finally clicked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a little assistance.
    By drakenathaniel in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2009, 05:07 PM
  2. Need Assistance?
    By shewj in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 07:57 AM
  3. Hello! May I get some assistance?
    By Flipz in forum C Programming
    Replies: 3
    Last Post: 12-02-2007, 12:20 PM
  4. need ur assistance
    By baby_me in forum C Programming
    Replies: 3
    Last Post: 06-21-2006, 01:47 PM
  5. Need some assistance
    By gcpatton in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2006, 08:50 PM