Advertisement
Garey

Kontrolno_BP2

Mar 8th, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. // Kontrolno.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. auto input_array(int *array, size_t) -> decltype(array);
  7. void print_array(int *, size_t);
  8. void remap_arrays(int *array, int *, int *, size_t, size_t &, size_t &);
  9.  
  10. int main()
  11. {
  12.     size_t choice, array_size, evens_t, odds_t;
  13.     int *array = new int[20], *evens_array = new int[20], *odds_array = new int[20];
  14.  
  15.     do {
  16.         system("cls");
  17.  
  18.         cout << "Menu:\n";
  19.         cout << "1. Input array\n";
  20.         cout << "2. Output array\n";
  21.         cout << "3. Remap array into even and odd arrays\n";
  22.         cout << "0. Exit\n\n";
  23.         cout << "Enter your choice: ";
  24.         cin >> choice;
  25.  
  26.         switch (choice) {
  27.             case 1:
  28.                 system("cls");
  29.                 cout << "Enter array size: ";
  30.                 cin >> array_size;
  31.  
  32.                 if (!cin || array_size < 0 || array_size > 20) {
  33.                     cout << "Invalid input";
  34.                     break;
  35.                 }
  36.  
  37.                 array = new int[array_size];
  38.  
  39.                 input_array(array, array_size);
  40.  
  41.                 cout << "Array generated successfully!";
  42.                 cout << "\n\nPress any key to get back to the menu\n\n";
  43.                 _getch();
  44.                 break;
  45.  
  46.             case 2:
  47.                 system("cls");
  48.                 print_array(array, array_size);
  49.  
  50.                 cout << "\n\nPress any key to get back to the menu\n\n";
  51.                 _getch();
  52.                 break;
  53.             case 3:
  54.                 system("cls");
  55.                 remap_arrays(array, evens_array, odds_array, array_size, evens_t, odds_t);
  56.                 cout << "Arrays remapped!\n\n";
  57.                 cout << "Array #1: \n";
  58.                 print_array(evens_array, evens_t);
  59.  
  60.                 cout << "\nArray #2: \n";
  61.                 print_array(odds_array, odds_t);
  62.  
  63.                 cout << "\n\nPress any key to get back to the menu\n\n";
  64.                 _getch();
  65.                 break;
  66.         }
  67.     } while (choice != 0);
  68.  
  69.     delete[] array;
  70.  
  71.     return 0;
  72. }
  73.  
  74. auto input_array(int *array, size_t array_size) -> decltype(array) {
  75.  
  76.     random_device rd;
  77.     mt19937 gen(rd());
  78.     uniform_int_distribution<int> dist(10, 99);
  79.  
  80.     for (size_t i = 0; i < array_size; i++)
  81.         array[i] = dist(gen);
  82.  
  83.     return array;
  84. }
  85.  
  86. void print_array(int *array, size_t array_size) {
  87.     for (size_t i = 0; i < array_size; i++)
  88.         cout << "Index #" << i << " | Value: " << array[i] << " | Address: " << &array[i] << endl;
  89. }
  90.  
  91. void remap_arrays(int *array, int *rearray1, int *rearray2, size_t array_size ,size_t &evens_t, size_t &odds_t) {
  92.  
  93.     evens_t = 0, odds_t = 0;
  94.  
  95.     for (size_t i = 0; i < array_size; i++) {
  96.         if (array[i] % 2 == 0) {
  97.             rearray1[evens_t] = array[i];
  98.             evens_t++;
  99.         }
  100.         else {
  101.             rearray2[odds_t] = array[i];
  102.             odds_t++;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement