Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Kontrolno.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- auto input_array(int *array, size_t) -> decltype(array);
- void print_array(int *, size_t);
- void remap_arrays(int *array, int *, int *, size_t, size_t &, size_t &);
- int main()
- {
- size_t choice, array_size, evens_t, odds_t;
- int *array = new int[20], *evens_array = new int[20], *odds_array = new int[20];
- do {
- system("cls");
- cout << "Menu:\n";
- cout << "1. Input array\n";
- cout << "2. Output array\n";
- cout << "3. Remap array into even and odd arrays\n";
- cout << "0. Exit\n\n";
- cout << "Enter your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- cout << "Enter array size: ";
- cin >> array_size;
- if (!cin || array_size < 0 || array_size > 20) {
- cout << "Invalid input";
- break;
- }
- array = new int[array_size];
- input_array(array, array_size);
- cout << "Array generated successfully!";
- cout << "\n\nPress any key to get back to the menu\n\n";
- _getch();
- break;
- case 2:
- system("cls");
- print_array(array, array_size);
- cout << "\n\nPress any key to get back to the menu\n\n";
- _getch();
- break;
- case 3:
- system("cls");
- remap_arrays(array, evens_array, odds_array, array_size, evens_t, odds_t);
- cout << "Arrays remapped!\n\n";
- cout << "Array #1: \n";
- print_array(evens_array, evens_t);
- cout << "\nArray #2: \n";
- print_array(odds_array, odds_t);
- cout << "\n\nPress any key to get back to the menu\n\n";
- _getch();
- break;
- }
- } while (choice != 0);
- delete[] array;
- return 0;
- }
- auto input_array(int *array, size_t array_size) -> decltype(array) {
- random_device rd;
- mt19937 gen(rd());
- uniform_int_distribution<int> dist(10, 99);
- for (size_t i = 0; i < array_size; i++)
- array[i] = dist(gen);
- return array;
- }
- void print_array(int *array, size_t array_size) {
- for (size_t i = 0; i < array_size; i++)
- cout << "Index #" << i << " | Value: " << array[i] << " | Address: " << &array[i] << endl;
- }
- void remap_arrays(int *array, int *rearray1, int *rearray2, size_t array_size ,size_t &evens_t, size_t &odds_t) {
- evens_t = 0, odds_t = 0;
- for (size_t i = 0; i < array_size; i++) {
- if (array[i] % 2 == 0) {
- rearray1[evens_t] = array[i];
- evens_t++;
- }
- else {
- rearray2[odds_t] = array[i];
- odds_t++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement