Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <conio.h>
- using namespace std;
- auto input_array(int *, size_t, size_t &, size_t &) -> void;
- auto output_array(int *, size_t) -> void;
- auto multiply_by_7(int *, size_t) -> void;
- auto divide_by_8(int *, size_t) -> void;
- int main() {
- random_device rd;
- mt19937 gen(rd());
- uniform_int_distribution<> dist(1, 16);
- size_t count(dist(gen)), odd_count = 0, even_count = 0, choice;
- int *array = new int[count], *odd = new int[odd_count], *even = new int[even_count];;
- do {
- system("cls");
- cout << "Menu:\n\n";
- cout << "[1] Input array\n";
- cout << "[2] Create two new arrays\n";
- cout << "[3] Multiply by 7 in the new array\n";
- cout << "[4] Divide by 8 each even element that is divisable by 8\n";
- cout << "[5] Print all arrays\n\n";
- cout << "[0] Exit\n";
- cout << "Your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- input_array(array, count, odd_count, even_count);
- cout << "The array is generated successfully!\nCount of the elements: " << count << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 2:
- system("cls");
- odd = new int[odd_count];
- even = new int[even_count];
- even_count = odd_count = 0;
- for (size_t i = 0; i < count; i++)
- array[i] & 0x1 ? odd[odd_count++] = array[i] : even[even_count++] = array[i];
- cout << "The arrays are created successfully!\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 3:
- system("cls");
- multiply_by_7(odd, odd_count);
- cout << "Array #1 is multiplied by 7 successfully!\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 4:
- system("cls");
- divide_by_8(even, even_count);
- cout << "Array #2 is divided by 8 successfully!\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 5:
- system("cls");
- cout << "Original array: \n\n";
- output_array(array, count);
- cout << "\nNew array #1: (Multiplied by 7)\n\n";
- output_array(odd, odd_count);
- cout << "\nNew array #2: (Divided by 8)\n\n";
- output_array(even, even_count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- default:
- break;
- }
- } while (choice != 0);
- return 0;
- }
- auto output_array(int *array, size_t size) -> void {
- if (!size || size == 0)
- cout << "Error: invalid size!\n";
- else
- for (size_t i = 0; i < size; i++)
- if (i == (size - 1))
- cout << "#: " << i << " | Element: " << array[i] << endl;
- else
- cout << "#: " << i << " | Element: " << array[i] << " | ";
- }
- auto multiply_by_7(int *array, size_t size) -> void {
- if (!size || size == 0)
- cout << "Error: invalid size!\n";
- else
- for (size_t i = 0; i < size; i++)
- array[i] = (array[i] << 3) - array[i];
- }
- auto divide_by_8(int *array, size_t size) -> void {
- if (!size || size == 0)
- cout << "Error: invalid size!\n";
- else
- for (size_t i = 0; i < size; i++)
- if (array[i] & 0x8)
- array[i] >>= 3;
- }
- auto input_array(int *array, size_t size, size_t &odd_count, size_t &even_count) -> void {
- random_device rd;
- mt19937 gen(rd());
- uniform_int_distribution<> dist(10, 66);
- for (size_t i = 0; i < size; i++) {
- array[i] = dist(gen);
- array[i] & 0x1 ? odd_count++ : even_count++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement