Advertisement
Garey

Kontrolno2_BP2_zad3

Mar 24th, 2018
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. auto input_array(int *, size_t, size_t &, size_t &) -> void;
  7. auto output_array(int *, size_t) -> void;
  8. auto multiply_by_7(int *, size_t) -> void;
  9. auto divide_by_8(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)), odd_count = 0, even_count = 0, choice;
  18.  
  19.     int *array = new int[count], *odd = new int[odd_count], *even = new int[even_count];;
  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 7 in the new array\n";
  29.         cout << "[4] Divide by 8 each even element that is divisable by 8\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(array, count, odd_count, even_count);
  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.             odd = new int[odd_count];
  47.             even = new int[even_count];
  48.  
  49.             even_count = odd_count = 0;
  50.  
  51.             for (size_t i = 0; i < count; i++)
  52.                 array[i] & 0x1 ? odd[odd_count++] = array[i] : even[even_count++] = 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_by_7(odd, odd_count);
  60.  
  61.             cout << "Array #1 is multiplied by 7 successfully!\n\nPress any key to get back to the menu!";
  62.             _getch();
  63.             break;
  64.         case 4:
  65.             system("cls");
  66.             divide_by_8(even, even_count);
  67.  
  68.             cout << "Array #2 is divided by 8 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(array, count);
  76.  
  77.             cout << "\nNew array #1: (Multiplied by 7)\n\n";
  78.             output_array(odd, odd_count);
  79.  
  80.             cout << "\nNew array #2: (Divided by 8)\n\n";
  81.             output_array(even, even_count);
  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.     return 0;
  94. }
  95.  
  96. auto output_array(int *array, size_t size) -> void {
  97.     if (!size || size == 0)
  98.         cout << "Error: invalid size!\n";
  99.     else
  100.         for (size_t i = 0; i < size; i++)
  101.             if (i == (size - 1))
  102.                 cout << "#: " << i << " | Element: " << array[i] << endl;
  103.             else
  104.                 cout << "#: " << i << " | Element: " << array[i] << " | ";
  105. }
  106.  
  107. auto multiply_by_7(int *array, size_t size) -> void {
  108.     if (!size || size == 0)
  109.         cout << "Error: invalid size!\n";
  110.     else
  111.         for (size_t i = 0; i < size; i++)
  112.             array[i] = (array[i] << 3) - array[i];
  113. }
  114.  
  115. auto divide_by_8(int *array, size_t size) -> void {
  116.  
  117.     if (!size || size == 0)
  118.         cout << "Error: invalid size!\n";
  119.     else
  120.         for (size_t i = 0; i < size; i++)
  121.             if (array[i] & 0x8)
  122.                 array[i] >>= 3;
  123. }
  124.  
  125. auto input_array(int *array, size_t size, size_t &odd_count, size_t &even_count) -> void {
  126.  
  127.     random_device rd;
  128.     mt19937 gen(rd());
  129.     uniform_int_distribution<> dist(10, 66);
  130.  
  131.     for (size_t i = 0; i < size; i++) {
  132.         array[i] = dist(gen);
  133.  
  134.         array[i] & 0x1 ? odd_count++ : even_count++;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement