Beginner Level Pointer

#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
Is this supposed to be C or C++ code? If C++ why use C I/O functions?

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>

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;
}

Why is it 2 * *b? You added *b to *a earlier so you need to subtract *b from *a twice to get the difference.
@furry guy

Suppose be C++. I was doing some C++ practices for some reason the int main() used C codes. Thank you!
Topic archived. No new replies allowed.