Advertisement
AlexandrTalchuk

4.2.11

Dec 29th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <math.h>
  5. #include <ctime>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main()
  12. {
  13.     setlocale(LC_ALL, "RUS");
  14.     srand( time_t (0));
  15.     int i, i0=-1, f, n, min, max,sum=0;
  16.  
  17.     cout << "Введите количество элементов массива" << endl;
  18.     cin >> n;
  19.     int *arr = new int[n];
  20.  
  21.     if (n < 1 || n>20) {
  22.         cout << "Ошибка,n не должен быть больше 20" << endl;
  23.         system("pause");
  24.         return 0;}
  25.  
  26.     cout << "1.Ввести самостоятельно" << endl;
  27.     cout << "2.Рандомные числа" << endl;
  28.     cin >> f;
  29.     switch (f){
  30.         case 1:
  31.             for (i = 0; i < n; ++i) {
  32.                 cout << "a[" << i + 1 << "]=";
  33.                 cin >> arr[i];}
  34.                 break;
  35.         case 2:
  36.             for (i = 0; i < n; ++i) {
  37.                 arr[i] = rand() % 22 -0;
  38.                 cout<<arr[i]<<endl;}
  39.             break;}
  40.  
  41.    
  42.     int nom_max;
  43.    min = max = arr[0];
  44.     for (i = 0; i < n; ++i) {
  45.         if (min > arr[i]) min = arr[i];
  46.         else if (max < arr[i]) { max = arr[i]; nom_max = i; }
  47.     }
  48.  
  49.     cout << "Min: " << min << endl;
  50.     cout << "Max: " << max << endl;
  51.  
  52.  
  53.     for (i = n - 1; i >= 0; --i)
  54.         if (arr[i] == 0) {
  55.             i0 = i;
  56.             break;}
  57.  
  58.  
  59.     if (i0 < 0) { cout << "Нулей нет" << endl;
  60.     return 0;}
  61.  
  62.     if (i0 == n - 1) {
  63.         cout << "Ноль последний" << endl;
  64.         return 0;}
  65.  
  66.     if (nom_max > i0)
  67.     {
  68.         min = i0;
  69.         max = nom_max;
  70.     }
  71.     else
  72.     {
  73.         max = i0;
  74.         min = nom_max;
  75.     }
  76.     for (i = min+1; i <max; ++i) {
  77.         sum += arr[i];}
  78.     cout << "sum=" << sum << endl;
  79.  
  80.     delete[] arr;
  81.  
  82.     system("pause");
  83.     return 0;
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement