Advertisement
VictoriaLodochkina

lab 10 z1 PERFECT

Feb 8th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. void sortmas(int k, int l, double* mas1, int s);
  7. int imin(int s, double* mas1);
  8. int imax(int s, double* mas1);
  9.  
  10. int main()
  11. {
  12.     int n;
  13.     cout << "Enter n: " << endl;
  14.     cin >> n;
  15.     double* mas = new double[n];
  16.     cout << "Enter mas: " << endl;
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         cin >> mas[i];
  20.     }
  21.     double max = mas[0];
  22.     double min = mas[0];
  23.     int low, high;
  24.     /*for (int i = 0; i < n; i++)
  25.     {
  26.         if (fabs(mas[i]) >= max)
  27.         {
  28.             max = mas[i];
  29.             imax = i;
  30.         }
  31.         if (fabs(mas[i]) <= min)
  32.         {
  33.             min = mas[i];
  34.             imin = i;
  35.         }
  36.     }*/
  37.     low = imin(n, mas);
  38.     high = imax(n, mas);
  39.     sortmas(low, high, mas, n);
  40.     return 0;
  41. }
  42.  
  43. void sortmas(int k, int l, double* mas1, int s)
  44. {
  45.     int b = 0;
  46.     for (int i = k; i <= l - 3; i++)
  47.     {
  48.         for (int j = k + 1; j <= l - 2 - b; j++)
  49.             if (mas1[j] < mas1[j + 1])
  50.             {
  51.                 int t = mas1[j];
  52.                 mas1[j] = mas1[j + 1];
  53.                 mas1[j + 1] = t;
  54.             }
  55.         b++;
  56.     }
  57.     for (int i = 0; i < s; i++)
  58.     {
  59.         cout << mas1[i] << " ";
  60.     }
  61. }
  62. int imin(int s, double* mas1)
  63. {
  64.     int minx = -1;
  65.     double min = mas1[0];
  66.     for (int i = 0; i < s; i++)
  67.     {
  68.         if (fabs(mas1[i]) <= min)
  69.         {
  70.             min = mas1[i];
  71.             minx = i;
  72.         }
  73.     }
  74.     return minx;
  75. }
  76. int imax(int s, double* mas1)
  77. {
  78.     int maxx = -1;
  79.     double max = mas1[0];
  80.     for (int i = 0; i < s; i++)
  81.     {
  82.         if (fabs(mas1[i]) >= max)
  83.         {
  84.             max = mas1[i];
  85.             maxx = i;
  86.         }
  87.     }
  88.     return maxx;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement