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?