Advertisement
mr1302

siaod2

Apr 15th, 2021
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <cmath>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. void print(int(&a)[1000], int n) {
  10.     for (int i = 0; i < n; ++i)
  11.         cout << a[i] << " ";
  12.     cout << '\n';
  13. }
  14.  
  15. bool keyb_fill(int (&a)[1000], int & n) {
  16.     cout << "Enter amout of elements\n";
  17.     cin >> n;
  18.     if (n > 1000) {
  19.         cout << "Incorrect amout of elements\n";
  20.         return false;
  21.     }
  22.     cout << "Enter elements\n";
  23.     for (int i = 0; i < n; ++i)
  24.         cin >> a[i];
  25.     return true;
  26. }
  27.  
  28. bool rand_fill(int(&a)[1000], int& n) {
  29.     cout << "Enter amout of elements\n";
  30.     cin >> n;
  31.     if (n > 1000) {
  32.         cout << "Incorrect amout of elements\n";
  33.         return false;
  34.     }
  35.     for (int i = 0; i < n; ++i)
  36.         a[i] = (int)pow(-1, rand() % 2) * rand() % 1000;
  37.     print(a, n);
  38.     return true;
  39. }
  40.  
  41. bool fill(int(&a)[1000], int & n) {
  42.     int x = 0;
  43.     cout << "Manual fill? (yes - 1, no - 0)\n";
  44.     cin >> x;
  45.     if (x == 1) return keyb_fill(a, n);
  46.     else return rand_fill(a, n);
  47. }
  48.  
  49. int first_enter(int(&a)[1000], int n, int x) {
  50.     for (int i = 0; i < n; ++i)
  51.         if (a[i] == x)
  52.             return i;
  53.     return -1;
  54. }
  55.  
  56. int first_negative(int(&a)[1000], int n, int x) {
  57.     for (int i = 0; i < n; ++i)
  58.         if (a[i] < x)
  59.             return i;
  60.     return -1;
  61. }
  62.  
  63. void enters(int(&a)[1000], int n, int x, int(&b)[1000], int& cnt) {
  64.     cnt = 0;
  65.     for (int i = 0; i < n; ++i)
  66.         if (a[i] == x) {
  67.             b[cnt] = i;
  68.             cnt++;
  69.         }
  70. }
  71.  
  72. void put(int(&a)[1000], int & n, int x, int ptr) {
  73.     for (int i = n - 1; i >= ptr; i--) {
  74.         a[i + 1] = a[i];
  75.     }
  76.     a[ptr] = x;
  77.     n++;
  78. }
  79.  
  80. void del_ptr    (int(&a)[1000], int& n, int ptr) {
  81.     for (int i = ptr; i < n - 1; ++i)
  82.         a[i] = a[i + 1];
  83.     n--;
  84. }
  85.  
  86. void del_n2(int(&a)[1000], int& n, int x) {
  87.     for (int i = 0; i < n; ++i) {
  88.         if (a[i] == x) {
  89.             for (int j = i; j < n - 1; ++j)
  90.                 a[j] = a[j + 1];
  91.             n--;
  92.         }
  93.     }
  94. }
  95.  
  96. void del_n(int(&a)[1000], int& n, int x) {
  97.     int b[1000];
  98.     int cnt = 0;
  99.     for (int i = 0; i < n; ++i)
  100.         if (a[i] != x) {
  101.             b[cnt] = a[i];
  102.             cnt++;
  103.         }
  104.     n = cnt;
  105.     memcpy(a, b, cnt);
  106. }
  107.  
  108. int main() {
  109.     srand(time(NULL));
  110.     int n = 0;
  111.     int a[1000];
  112.     for (int i = 0; i < 1000; ++i)
  113.         a[i] = 0;
  114.     fill(a, n);
  115.     cout << "Array:\n";
  116.     print(a, n);
  117.     n = 0;
  118.     for (int i = 0; i < 1000; ++i)
  119.         a[i] = 0;
  120.     fill(a, n);
  121.     cout << "Array:\n";
  122.     print(a, n);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement