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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. C++
  3. Introduction
  4. Pointer
  5. Discussions

Pointer

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 895 Discussions, By:

votes

Please Login in order to post a comment

  • omike1900
    6 years ago+ 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; 
    }
    
    373|
    Permalink
    View more Comments..
  • giantmalik
    7 years ago+ 4 comments

    Can i get more problems related to pointers? i want some more to improve pointers.

    47|
    Permalink
    View more Comments..
  • sivaraam
    5 years ago+ 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;
    }
    
    43|
    Permalink
    View more Comments..
  • Ethan13310
    5 years ago+ 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;
    }
    
    29|
    Permalink
    View more Comments..
  • jpverma
    5 years ago+ 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;
    

    }

    6|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature