C bootcamp · Lab 39

Min and max

easyArrays10 minLesson: C Arrays

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

The first number is n, then n integers. Print the smallest, then the largest, separated by a space.

Input. n, then n integers.

Output. Two integers: min then max.

Constraints

  • 1 ≤ n ≤ 100

Examples

Example 1
Input
5
3 1 8 2 4
Output
1 8
Example 2
Input
1
42
Output
42 42
Hint
  1. Start lo and hi at the first value, then walk the rest.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int n, x;
  if (scanf("%d", &n) != 1) return 1;
  scanf("%d", &x);
  int lo = x, hi = x;
  for (int i = 1; i < n; i++) {
    scanf("%d", &x);
    if (x < lo) lo = x;
    if (x > hi) hi = x;
  }
  printf("%d %d\n", lo, hi);
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.