In this HackerRank Pointer problem in the c++ programming language, you need to Complete the update function in the editor below. and the update has the following parameters.
- int *a: an integer
- int *b: an integer
HackerRank Pointer problem solution in c++ programming.
#include <stdio.h> void update(int *a,int *b) { // Complete this function int sum = *a + *b; int absDifference = *a - *b > 0 ? *a - *b : -(*a - *b); *a = sum; *b = absDifference; } 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; }
Second solution
#include <cstdio> #include <cstdlib> // required to use the built-in abs() function void update(int *a,int *b) { int temp = *a; *a = *a + *b; *b = abs(temp - *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; }
Third solution
#include <stdio.h>
int abs(int a) {
return (a>0 ? a : -a);
}
void update(int *a,int *b) {
*a = *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;
}
0 Comments