Advertisement
smatskevich

min_max

Apr 10th, 2021
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3.  
  4. void find_min_and_max(int* v, int n, int& min, int& max) {
  5.   assert(n > 0);
  6.   max = min = v[0];
  7.  
  8.   for (int i = 1; i < n; ++i) {
  9.     if (v[i] > max) {
  10.       max = v[i];
  11.     }
  12.     if (v[i] < min) {
  13.       min = v[i];
  14.     }
  15.   }
  16. }
  17.  
  18. int main() {
  19.   int n = 0;
  20.   std::cin >> n;
  21.   int* p = new int[n];
  22.   for (int i = 0; i < n; ++i) {
  23.     std::cin >> p[i];
  24.   }
  25.   int min = 0, max = 0;
  26.   find_min_and_max(p, n, min, max);
  27.  
  28.   std::cout << min << " " << max << std::endl;
  29.  
  30.   delete[] p;
  31.   return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement