Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Pointer solution in c++ programming

YASH PAL, 31 July 202422 August 2024

In this HackerRank Pointer problem solution in the c++ programming language, A pointer in C++ is used to share a memory address among different contexts (primarily functions). They are used whenever a function needs to modify the content of a variable, but it does not have ownership.

In order to access the memory address of a variable, val, prepend it with & sign. For example, &val returns the memory address of val.

This memory address is assigned to a pointer and can be shared among functions. For example, int* p = &val assigns the memory address of val to pointer p. To access the content of the memory pointed to, prepend the variable name with a *. For example, *p will return the value stored in val and any modification to it will be performed on val.

void increment(int *v) {

    (*v)++;

}

int main() {

    int a;

    scanf(“%d”, &a);

    increment(&a);

    printf(“%d”, a);

    return 0;

}  

Function Description

Complete the update function in the editor below.

update has the following parameters:

  1. int *a: an integer
  2. int *b: an integer
HackerRank Pointer solution in c++ programming

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("%dn%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("%dn%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("%dn%d", a, b);

    return 0;
}
coding problems cpp hackerrank solutions

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes