Skip to content
Exercise · References and Pointers · lesson 7 of 8

Exercise: min and max through references

Write a function void min_max(const int* data, int n, int& lo, int& hi). The pointer data points to the first of n numbers that lie in memory one after another. The function writes the smallest of these numbers to lo and the largest to hi.

  • numbers 4 -2 9 0 7 → lo == -2, hi == 9
  • a single number 5 → lo == 5, hi == 5
  • numbers -3 -1 -8 → lo == -8, hi == -1

Constraints: n is at least 1, numbers may be negative and unsorted, and lo and hi may hold anything beforehand.

min_max.cpp — cpp
cpp · 3 lines
Ctrl+Enter run · Esc leave editor