We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I was on the dark side of pointers too but I just figured it out. For this problem think it as this:
int sum = *a +*b; //it stores the value of *a + *b into sum but leaves the value of *a and *b the same
int dif = abs(*a - *b); //same as before
at this point, *a = *a && *b = *b && sum = *a + *b
&& dif = *a - *b (positive because of abs)
now, to chage the value of the address of *a and *b you declare
*a = sum;
b = dif;
now a & *b have new values
Did it helped???
sivaraam's solution is the best so far. There is no need of if statements or nothing. Just add cmath, iostream, and std. This will make your code cleaner which is the best practice!!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pointer
You are viewing a single comment's thread. Return to all comments →
I was on the dark side of pointers too but I just figured it out. For this problem think it as this: int sum = *a +*b; //it stores the value of *a + *b into sum but leaves the value of *a and *b the same int dif = abs(*a - *b); //same as before at this point, *a = *a && *b = *b && sum = *a + *b && dif = *a - *b (positive because of abs) now, to chage the value of the address of *a and *b you declare *a = sum; b = dif; now a & *b have new values Did it helped??? sivaraam's solution is the best so far. There is no need of if statements or nothing. Just add cmath, iostream, and std. This will make your code cleaner which is the best practice!!