HackerRank C++ Pointer




You have to complete the function void update(int *a,int *b), which reads two integers as argument, and sets a with the sum of them, and b with the absolute difference of them.



  1. #include <iostream>
  2. using namespace std;
  3. void update(int *a,int *b)
  4. {
  5. int temp = *a;
  6. *a = *a + *b;
  7. *b = temp - *b;
  8. if(*b < 0)
  9. {
  10. *b = -(*b);
  11. }
  12. }
  13. int main()
  14. {
  15. int a, b;
  16. int *pa = &a, *pb = &b;
  17. cin>>a>>b;
  18. update(pa, pb);
  19. cout<<a<<"\n"<<b;
  20. return 0;
  21. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube