Advertisement
Axeer

pz5_7

Apr 4th, 2022 (edited)
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <time.h>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7.  
  8. #define VARIANT 1
  9. #define ARRSIZE 10*VARIANT
  10.  
  11. void swap(float* a, float* b) {
  12.     float* temp = a;
  13.     a = b;
  14.     b = temp;
  15. }
  16.  
  17. float** minmax(float arr[], float* min, float* max, int n) {
  18.     min = max = arr;
  19.  
  20.     for (float* cur = arr; n; ++cur, --n) {
  21.         min = *min > *cur ? min = cur : min;
  22.         max = *max < *cur ? max = cur : max;
  23.     }
  24.     std::swap(*min, *max);
  25.    
  26.  
  27.     float** x = new float*[2]{ min,max };
  28.     return x;
  29. }
  30.  
  31.  
  32. int main() {
  33.     srand(time(NULL));
  34.     float arr[ARRSIZE];
  35.     float *min = nullptr, *max = nullptr;
  36.  
  37.     for (int i = 0; i < ARRSIZE;) {
  38.         for (int ii = 0; ii < 10; ++i, ++ii) {
  39.             arr[i] = rand() % 99 + 1;
  40.             cout << setw(4) << arr[i] << ' ';
  41.         }
  42.         cout << setw(0) << endl;
  43.     }
  44.  
  45.     float** result = minmax(arr, min, max, sizeof(arr) / sizeof(float));
  46.     cout << "min: " << *result[0] << endl;
  47.     cout << "max: " << *result[1] << endl;
  48.     for (int i = 0; i < ARRSIZE;++i) {
  49.         cout << setw(4) << arr[i] << ' ';
  50.     }
  51.     cout << setw(0) << endl;
  52.     delete[] result;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement