Process Sensor Reading

#include <iostream>

// Overload for modification
void process(int& x) {
    x = x * 2;
}

// Overload for read-only use
void process(const int& x) {
    std::cout << "readonly";
}

int main() {
    int x;
    std::cin >> x;

    if (x < 0) {
        process(static_cast<const int&>(x));   // read-only
    } else {
        process(x);                            // modification
        std::cout << x;
    }

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

10