Advertisement
VictoriaLodochkina

lab 6 z2 end

Nov 6th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>>
  3. int main()
  4. {
  5.     using namespace std;
  6.     int n, imin, imax, t;
  7.     cout << "Enter n: " << endl;
  8.     cin >> n;
  9.     double* mas = new double[n];
  10.     for (int i = 0; i < n; i++)
  11.     {
  12.         cin >> mas[i];
  13.     }
  14.     double max = mas[0];
  15.     double min = mas[0];
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         if (mas[i] <= min)
  19.         {
  20.             min = mas[i];
  21.             imin = i;
  22.         }
  23.         if (fabs(mas[i]) >= max)
  24.         {
  25.             max = fabs(mas[i]);
  26.             imax = i;
  27.         }
  28.     }
  29.     if (imax < imin)
  30.     {
  31.         t = imin;
  32.         imin = imax;
  33.         imax = t;
  34.     }
  35.  
  36.     if (imin == imax)
  37.     {
  38.         cout << "mas was not changed " << endl;
  39.         for (int i = 0; i < n; i++)
  40.         {
  41.             cout << mas[i];
  42.         }
  43.     }
  44.     else
  45.     {
  46.         int b;
  47.         for (int i = imin; i <= imax - 3; i++)
  48.         {
  49.             for (int j = imin + 1; j <= imax - 2 - b; j++)
  50.                 if (mas[j] < mas[j + 1])
  51.                 {
  52.                     int k = mas[j];
  53.                     mas[j] = mas[j + 1];
  54.                     mas[j + 1] = k;
  55.                 }
  56.         b++;
  57.         }
  58.         cout << "new: " << endl;
  59.         cout << imin << endl << imax << endl;
  60.         for (int i = 0; i < n; ++i)
  61.         {
  62.             cout << mas[i];
  63.         }
  64.     }
  65.     delete[] mas;
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement