Advertisement
Garey

Kontrolno2_BP2_zad1

Mar 24th, 2018
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. auto input(int *, size_t, size_t &, size_t &) -> void;
  7. auto output(int *, size_t) -> void;
  8. auto multiply_element(int *, size_t) -> void;
  9. auto divide_element(int *, size_t) -> void;
  10.  
  11. int main() {
  12.    
  13.     random_device rd;
  14.     mt19937 gen(rd());
  15.     uniform_int_distribution<> dist(1, 16);
  16.  
  17.     size_t count(dist(gen)), db3 = 0, _else = 0, choice;
  18.  
  19.     int *array = new int[count], *asd = new int[db3], *asdf = new int[_else];;
  20.  
  21.  
  22.     do {
  23.         system("cls");
  24.  
  25.         cout << "Menu:\n\n";
  26.         cout << "[1] Input array\n";
  27.         cout << "[2] Create two new arrays\n";
  28.         cout << "[3] Multiply by 3 in the new array\n";
  29.         cout << "[4] Divide by 4 each element that is divisable by 4\n";
  30.         cout << "[5] Print all arrays\n\n";
  31.         cout << "[0] Exit\n";
  32.         cout << "Your choice: ";
  33.         cin >> choice;
  34.  
  35.         switch (choice) {
  36.             case 1:
  37.                 system("cls");
  38.                 input(array, count, db3, _else);
  39.  
  40.                 cout << "The array is generated successfully!\nCount of the elements: " << count << "\n\nPress any key to get back to the menu!";
  41.                 _getch();
  42.                 break;
  43.             case 2:
  44.                 system("cls");
  45.  
  46.                 asd = new int[db3];
  47.                 asdf = new int[_else];
  48.  
  49.                 _else = db3 = 0;
  50.  
  51.                 for (size_t i = 0; i < count; i++)
  52.                     array[i] & 0x3 ? asd[db3++] = array[i] : asdf[_else++] = array[i];
  53.  
  54.                 cout << "The arrays are created successfully!\n\nPress any key to get back to the menu!";
  55.                 _getch();
  56.                 break;
  57.             case 3:
  58.                 system("cls");
  59.                 multiply_element(asd, db3);
  60.  
  61.                 cout << "Array #1 is multiplied by 3 successfully!\n\nPress any key to get back to the menu!";
  62.                 _getch();
  63.                 break;
  64.             case 4:
  65.                 system("cls");
  66.                 divide_element(asdf, _else);
  67.  
  68.                 cout << "Array #2 is divided by 4 successfully!\n\nPress any key to get back to the menu!";
  69.                 _getch();
  70.                 break;
  71.             case 5:
  72.                 system("cls");
  73.  
  74.                 cout << "Original array: \n\n";
  75.                 output(array, count);
  76.  
  77.                 cout << "\nNew array #1: (Multiplied by 3)\n\n";
  78.                 output(asd, db3);
  79.  
  80.                 cout << "\nNew array #2: (Divided by 4)\n\n";
  81.                 output(asd, db3);
  82.  
  83.                 cout << "\n\nPress any key to get back to the menu!";
  84.                 _getch();
  85.                 break;
  86.  
  87.             default:
  88.                 break;
  89.         }
  90.  
  91.     } while (choice != 0);
  92.  
  93.  
  94.    
  95.     output(asd, db3);
  96.     multiply_element(asd, db3);
  97.     output(asd, db3);
  98.  
  99.     return 0;
  100. }
  101.  
  102. auto output(int *array, size_t size) -> void {
  103.     if (!size || size == 0)
  104.         cout << "Error: invalid size!\n";
  105.     else
  106.         for (size_t i = 0; i < size; i++)
  107.             if(i == (size - 1))
  108.                 cout << "#: " << i << " | Element: " << array[i] << endl;
  109.             else
  110.                 cout << "#: " << i << " | Element: " << array[i] << " | ";
  111. }
  112.  
  113. auto multiply_element(int *array, size_t size) -> void {
  114.     if (!size || size == 0)
  115.         cout << "Error: invalid size!\n";
  116.     else
  117.         for (size_t i = 0; i < size; i++)
  118.             array[i] = (array[i] >> 1) + array[i];
  119. }
  120.  
  121. auto divide_element(int *array, size_t size) -> void {
  122.  
  123.     if (!size || size == 0)
  124.         cout << "Error: invalid size!\n";
  125.     else
  126.         for (size_t i = 0; i < size; i++)
  127.             if (array[i] & 0x4)
  128.                 array[i] <<= 2;
  129. }
  130.  
  131. auto input(int *array, size_t size, size_t &db3, size_t &_else) -> void {
  132.  
  133.     random_device rd;
  134.     mt19937 gen(rd());
  135.     uniform_int_distribution<> dist(10, 66);
  136.  
  137.     for (size_t i = 0; i < size; i++) {
  138.         array[i] = dist(gen);
  139.  
  140.         array[i] & 0x3 ? db3++ : _else++;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement