#include <stdio.h>
#include <cmath>
void update(int *a,int *b) {
// Complete this function
*a+=*b;
*b=abs(*a-2**b);
}
To calculate b = |a-b| using pointer. Why is it *b=abs(*a-2**b)? 2**b?
Why didn't it use *b=abs(*a-*b) instead of *b=abs(*a-2**b)?
No idea. Where did you get that code? Did you write *a+=*b;
?
2**b is 2 * (*b)
so
a = a + b
b = |a - 2b|
Here's a fundamental question; what's this function meant to do? update doesn't tell us anything. Is it meant to be swapping the values *a and *b ?
The program is for : Modify the values in memory so that a contains their sum and b contains their absoluted difference. (a' = a+b; b' = |a b|)
Sample Input
4
5
Sample Output
9
1
Explanation
a' = 4+5 =9
b' = |4 5| = 1
Entire code:
void update(int *a,int *b) {
*a+=*b;
*b=abs(*a-2**b);
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Last edited on
@furry guy
Suppose be C++. I was doing some C++ practices for some reason the int main() used C codes. Thank you!