C: Two results through pointers

Return two results from one function.

What to build

A function with output parameters.

Requirements

  • Write void min_max(int a, int b, int c, int *lo, int *hi).
  • It stores the smallest of the three values in *lo, and the largest in *hi.
  • Print min L max H.

Input

One line with three whole numbers.

Output

One line.

Constraints

Each number fits in an int.

Example

Input:

3 9 4

Output:

min 3 max 9

Why it works

A C function returns one value at most. Handing it addresses gives it somewhere to put any further results.

Examples

Example 1 · Input
3 9 4
Expected output
min 3 max 9
Example 2 · Input
5 5 5
Expected output
min 5 max 5

Hints

Hint 1

A function can only return one value, but it can write through pointers.

Hint 2

Inside the function, assign to *lo and *hi.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

void min_max(int a, int b, int c, int *lo, int *hi) {
    *lo = a;
    *hi = a;
    if (b < *lo) *lo = b;
    if (c < *lo) *lo = c;
    if (b > *hi) *hi = b;
    if (c > *hi) *hi = c;
}

int main(void) {
    int a, b, c, lo = 0, hi = 0;
    scanf("%d %d %d", &a, &b, &c);
    min_max(a, b, c, &lo, &hi);
    printf("min %d max %d\n", lo, hi);
    return 0;
}

Lesson: Pointers →