Advertisement
Qellex

5.1

Dec 22nd, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include "stdio.h" // подключение ввода, вывода
  2. #include "locale.h" // подключение русского языка
  3. #include "stdlib.h" // подключение рандома
  4. #include "time.h"
  5.  
  6.  
  7. void input(int* a, int n) {
  8.     printf("Введите числа (%d): ", n);
  9.     for (int i = 0; i < n; i++)
  10.         scanf_s("%d", &a[i]);
  11. }
  12.  
  13. void inputrand(int* a, int N) {
  14.     for (int i = 0; i < N; i++) {
  15.         a[i] = rand() % 100;
  16.     }
  17. }
  18.  
  19. void output(int* a, int N) {
  20.     for (int i = 0; i < N; i++) {
  21.         printf("%3d", a[i]);
  22.     }
  23. }
  24.  
  25.  
  26. void main() {
  27.  
  28.     setlocale(LC_ALL, "rus"); // подключение русского языка
  29.  
  30.     int x = 0, N; // запоминает элементы массива, размер массива
  31.     srand(clock()); // датчик случайных чисел
  32.  
  33.     printf("Введите количество элементов массива: ");
  34.     do {
  35.         scanf_s("%d", &N);
  36.         if (N <= 0)
  37.             printf("Повторите ввод, введите положительное число: ");
  38.     } while (N <= 0);
  39.    
  40.     int* a = (int*)malloc(sizeof(int*) * N);
  41.     int* b = (int*)malloc(sizeof(int*) * N);
  42.  
  43.     printf("Выберите способ ввода массива:\n1-ввод с клавиатуры\n2-заполнение массива случайными числами\n");
  44.  
  45.     int f;
  46.     do {
  47.         scanf_s("%d", &f);
  48.         if ((f != 1) && (f != 2))
  49.             printf("Введите 1 или 2: ");
  50.     } while ((f != 1) && (f != 2));
  51.     if (f == 1)
  52.         input(a, N);
  53.     else
  54.         inputrand(a, N);
  55.  
  56.  
  57.     /*вывод исходного массива*/
  58.     printf("Исходный массив: ");
  59.     output(a, N);
  60.  
  61.     // добавим первый элемент
  62.     b[0] = a[0];
  63.  
  64.     // делаем возрастающую последовательность
  65.     for (int i = 1; i < N; i++) {
  66.         if (a[i] > b[x]) {
  67.             x++;
  68.             b[x] = a[i];
  69.         }
  70.     }
  71.     x++;
  72.  
  73.     printf("\nИзмененный массив: ");
  74.     output(b, x);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement