# Thread: in need of a little assistance

1. ## 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. 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. 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. 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. 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. Originally Posted by unknown2007
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. 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. Originally Posted by unknown2007
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. thanks so much! it finally clicked!