← const Correctness with Pointers

Distance Between Two Points

medium

distance takes two const int * parameters and returns how far apart the two values are – always nonnegative, regardless of which one is larger.

The ternary operator from Module 1 fits this exactly: (*a > *b) ? (*a - *b) : (*b - *a) subtracts the smaller from the larger either way, so the result can never come out negative. Both parameters are const for the same reason as the previous exercise – this function reads two values and produces a new one; it never needs to write through either pointer.

Your solution

"Run" uses the sample stdin ("10 3"). "Submit" checks your code against all 4 test cases.

Test results

Submit your solution to run it against all test cases.

Reference solution