# Thread: Having trouble with simple variable assignment calculations. Beginner Question.

1. ## Having trouble with simple variable assignment calculations. Beginner Question.

Hello, this is a function I dont understand.

This is what I am having trouble with.
suppose that a=4 and b=7

Code:
int f(int a, int b) {
int c;
c = 2*a%b;
a = c + (2*b%a);
b = c - (2*b%a);
printf("a = %d, b = %d\n",a,b);
return c;
}
Once you plug in the initial a and b values, the correct answer is a=3 b=0. I dont get how it is achieved.

This is my logic and why I dont understand it:

c=2*a%b = 2*4%7 =1
a=c+(2*b%a) =1+(2*7%4) =3
b=c - (2*b%a)=1- (2*7%4)= -1

So I got a=3 and b=-1 which is wrong. Can someone please explain the logic behind the correct answer?

2. No. a = 3, b = -1 is the correct answer just like you worked out. I ran the program.

It may not work for other values though depending on how the formulas are supposed to be interpreted. I wonder if the assigned values a and b should be separate from the passed values so that they are not corrupted. Assign to aa and bb, and print those out instead. I would guess.

3. Shouldn't you use the new value of a? (Granted, this doesn't change the end value, since 1 - (2*7%3) is also -1.)

4. Look ....
When I run my entire program, which is :

Code:
#include <stdio.h>

int f(int a, int b);

int main() {

int a=4, b=7;

if (f(b,a))
printf("a = %d, b = %d\n",a,b);
else

b = f(3*a+b, 3*b-a);

printf("a = %d, b = %d\n",a,b);
system("PAUSE");
return 0;
}

int f(int a, int b) {
int c;
c = 2*a%b;
a = c + (2*b%a);
b = c - (2*b%a);
printf("a = %d, b = %d\n",a,b);
return c;
}
The output is different, it is not a=3 and b=-1 like I would expect, can someone explain why?

5. Follow the program to understand why printf prints what it does.

When you get to here
b = f(3*a+b, 3*b-a);

we can work out what arguments f has and what b is as a result of the call to f.

3*4+7 = 19
3*7-4 = 17

so b = f(19 , 17);

Then in f, this is calculated

c = 2*a%b
c = 2 * 19 % 17
c = 38 % 17
c = 4

c is assigned to b...

6. There's a difference between (4, 7) and (7, 4).

7. Thank for the replies everyone i finally get it!

Code:
#include <stdio.h>

int f(int a, int b);

int main() {

int a[5];
int i;
for (i=0; i<5; i++)
a[i] = (2*i+3)%5;
int t = a[0];
a[0] = a[1] + a[2];
a[3] = a[3] + a[4];
a[4] = t;
printf("a[0] = %d\n", a[0]);
printf("a[1] = %d\n", a[1]);
printf("a[3] = %d\n", a[3]);
printf("a[4] = %d\n", a[4]);

system("PAUSE");
return 0;
}
I understand how they got a[0]=2, a[1]= 0, a[3]=5 for the output but I dont understand how they got a[4]=3 ..........I keep getting 2 for my a[4] value.

Thanks again

8. t is assigned the value of a[0] at the time the assignment happens -- later changes to a[0] do not affect t.

9. Code:
#include <stdio.h>

int f(int a, int b);

int main() {

int a=4, b=7;

if (f(b,a))
printf("a = %d, b = %d\n",a,b);
else

b = f(3*a+b, 3*b-a);

printf("a = %d, b = %d\n",a,b);
system("PAUSE");
return 0;
}

int f(int a, int b) {
int c;
c = 2*a%b;
a = c + (2*b%a);
b = c - (2*b%a);
printf("a = %d, b = %d\n",a,b);
return c;
}
thanks for the reply labstop. I have another question reguarding the code above.

1)How do you know how many times it is going to print?? When I run the program, the final values in main are a=4, b=4, why wouldn't I continue to plug those values in for a and b in the function?

2)I understand why all the numbers print out besides why a=4 and b=4 for the final values. Can someone please explain? Also, at the end of the function it says "return c" in order for b=4 as the final value, wouldnt the function need to say "return b"?

10. Execution starts at the beginning of the program and goes to the end. That's how you know how often things happen, and in what order.

11. Originally Posted by matthayzon89
2)I understand why all the numbers print out besides why a=4 and b=4 for the final values. Can someone please explain? Also, at the end of the function it says "return c" in order for b=4 as the final value, wouldnt the function need to say "return b"?
I explained how you got those numbers, so I'm not going over that again. The point of the return is that you want to assign f's c variable to main's b. If you wrote "return b" you would assign f's b variable to main's b variable. By returning, you take a variable from inside one function and store its value in a variable from the calling function. This is the simplest way for functions to share the results of computations without breaking scope rules. Scope rules explain how main's b is separate from f's b, and where each of them are "in scope" and are going to be used. According to C, it doesn't make sense to use just one b everywhere.