Advertisement
Caneq

lb3.1.2

Nov 9th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <time.h>  
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. int main() {
  8.     setlocale(LC_ALL, "rus");
  9.     const int N = 10;
  10.     const int NUMB_MAX = 5;
  11.     int A[N] = { 1, 10, -2, 100, 50, 43, -32, 65, 16, 1 };
  12.     int B[NUMB_MAX];
  13.         cout << "Выберите способ ввода элементов массива\r\n"
  14.             "0 - Инициализация в коде\r\n"
  15.             "1 - Заполнение случайными числами\r\n"
  16.             "2 - Ввод с клавиатуры\r\n";
  17.         int inp_method;
  18.         cin >> inp_method;
  19.         enum
  20.         {
  21.             fill_in_code,
  22.             fill_rand,
  23.             fill_keyboard,
  24.         };
  25.  
  26.         switch (inp_method) {
  27.         case fill_in_code:
  28.             break;
  29.         case fill_rand:
  30.             srand(static_cast<int>(time(0)));
  31.             for (int i = 0; i < N; i++)
  32.                 A[i] = rand() % 101 - rand() % 101;
  33.             break;
  34.         case fill_keyboard:
  35.             for (int i = 0; i < N; i++)
  36.                 cin >> A[i];
  37.             break;
  38.         default:
  39.             return 0;
  40.         }
  41.     cout << "Исходный массив:" << endl;
  42.     for (int i = 0; i < N; i++) {
  43.         cout << setw(5) << A[i];
  44.     }
  45.     cout << endl;
  46.  
  47.     for (char i = 0; i < NUMB_MAX; i++) {
  48.         B[i] = A[i];
  49.     }
  50.  
  51.     for (int i = NUMB_MAX; i < N; i++) {
  52.         int a = A[i];
  53.         int min_ind = 0;
  54.         for (char k = 0; k < NUMB_MAX; k++) {
  55.             if (B[k] < B[min_ind])
  56.                 min_ind = k;
  57.         }
  58.         if (a > B[min_ind]) {
  59.             B[min_ind] = a;
  60.         }
  61.     }
  62.  
  63.  
  64.     cout << "5 Наибольших элементов:" << endl;
  65.     for (int i = 0; i < NUMB_MAX; i++) {
  66.         cout << setw(5) << B[i];
  67.     }
  68.     cout << endl;
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement