Pointer | HackerRank
Learn how to declare pointers and use them.
www.hackerrank.com
Goal
By using pointer in C++, save a + b value into a and | a - b | into b.
Solution
This problem makes us to pratice pointer.
You can save sum and abs_min of a and b with * sign in a and b, respectively.
// Pointer
#include <stdio.h>
#include <cstdlib>
void update(int *a,int *b) {
// Complete this function
int sum = *a + * b;
int abs_min = (*a) - (*b);
*a = sum;
*b = abs(abs_min);
}
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;
}
'software Engineering > (C++)HackerRank' 카테고리의 다른 글
[C++] HackerRank - Variable Sized Arrays (0) | 2023.02.10 |
---|---|
[C++] HackerRank - Arrays Introduction (0) | 2023.02.09 |
[C++] HackerRank - Functions (0) | 2023.02.04 |
[C++] HackerRank - For Loop (0) | 2023.01.24 |
[C++] HackerRank - Conditional Statements (0) | 2023.01.24 |