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.
- Prepare
- C++
- Introduction
- Pointer
- Discussions
Pointer
Pointer
+ 43 comments you can not include any library, just like this
void update(int *a,int *b) { int sum = *a + *b; int absDifference = *a - *b > 0 ? *a - *b : -(*a - *b); *a = sum; *b = absDifference; }
+ 4 comments Can i get more problems related to pointers? i want some more to improve pointers.
+ 12 comments My Solution
#include <stdio.h> #include <stdlib.h> void update(int *a,int *b) { // Complete this function *a = *a + *b; *b =abs(*a - *b -*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; }
+ 5 comments We don't want to use raw pointers in C++. We almost never use them, exept for interfacing with a C-written library. In this exercise, we should use references instead. But here's the solution:
#include <cmath> void update(int *a, int *b) { int tmp{*a + *b}; *b = std::abs(*a - *b); *a = tmp; }
And in real C++:
#include <cmath> void update(int &a, int &b) { int tmp{a + b}; b = std::abs(a - b); a = tmp; }
+ 3 comments include
include
using namespace std;
void update(int *a,int *b) { // Complete this function
*a = *a + *b; *b = abs(*b - (*a-*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;
}
Load more conversations
Sort 895 Discussions, By:
Please Login in order to post a comment