C++ bootcamp · Lab 48

Median of three

easyIf Else10 minLesson: If Else

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

QuestionHint and solution stay closed until you open them

Read three integers and print the median.

Input. Three integers.

Output. Middle value.

Examples

Example 1
Input
3 1 2
Output
2
Example 2
Input
9 9 1
Output
9
Hint
  1. Put them in an array and std::sort.
Show correct code

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

#include <iostream>
#include <algorithm>

int main() {
  int v[3];
  std::cin >> v[0] >> v[1] >> v[2];
  std::sort(v, v + 3);
  std::cout << v[1] << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.