Advertisement
Garey

variant_25

Nov 7th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. int input_array(int [], size_t);
  9. int make_second_array(int [], size_t, int []);
  10. int make_third_array(int [], size_t , int []);
  11. void output_array(int [], size_t);
  12.  
  13. int main() {
  14.   int choice;
  15.   size_t n;
  16.  
  17.   cout << "Enter array size: ";
  18.   cin >> n;
  19.  
  20.   if(!cin || n > 30 || n < 0)
  21.     return 0;
  22.  
  23.   int array[n];
  24.   int array2[n];
  25.   int array3[n];
  26.  
  27.   do {
  28.  
  29.     system("cls");
  30.  
  31.     cout << "Menu: \n";
  32.     cout << "1. Input array\n";
  33.     cout << "2. Make second array and reverse it\n";
  34.     cout << "3. Make third array and sort it\n";
  35.     cout << "4. Print arrays\n";
  36.     cout << "5. Exit\n\n";
  37.  
  38.     cout << "Make your choice: ";
  39.     cin >> choice;
  40.  
  41.     switch (choice) {
  42.       case 1:
  43.         system("cls");
  44.         input_array(array, n);
  45.         Sleep(1000);
  46.         break;
  47.  
  48.       case 2:
  49.         system("cls");
  50.         make_second_array(array, n, array2);
  51.         cout << "Second array created and reversed!";
  52.         Sleep(1250);
  53.         break;
  54.  
  55.       case 3:
  56.         system("cls");
  57.         make_third_array(array, n, array3);
  58.         cout << "Third array created and sorted in ascending order!";
  59.         Sleep(1500);
  60.         break;
  61.  
  62.       case 4:
  63.         system("cls");
  64.         output_array(array, n);
  65.         output_array(array2, n);
  66.         output_array(array3, n);
  67.         cout << "\n\n Press any key to get back to the menu...";
  68.         getch();
  69.         break;
  70.  
  71.     }
  72.   } while(choice != 5);
  73.   return 0;
  74. }
  75.  
  76. int input_array(int array[], size_t array_size) {
  77.   for (size_t i = 0; i < array_size; i++) {
  78.     cout << "Enter value for element #" << i << ": ";
  79.     cin >> array[i];
  80.   }
  81.  
  82.   return *array;
  83. }
  84.  
  85. int make_second_array(int array[], size_t array_size, int array2[]) {
  86.   size_t i, j;
  87.  
  88.   for (i = 0; i < array_size; i++) {
  89.     array2[i] = array[i];
  90.   }
  91.  
  92.   j = i - 1; // last element of the array
  93.   i = 0; // null i to reuse
  94.  
  95.   while(i < j) {
  96.     int temp;
  97.  
  98.     temp = array2[i];
  99.     array2[i] = array2[j];
  100.     array2[j] = temp;
  101.  
  102.     i++;
  103.     j--;
  104.   }
  105.  
  106.   return *array2;
  107. }
  108.  
  109. int make_third_array(int array[], size_t array_size, int array2[]) {
  110.  
  111.   size_t i, j;
  112.  
  113.   for (i = 0; i < array_size; i++) {
  114.     array2[i] = array[i];
  115.   }
  116.  
  117.   for (i = 0; i < array_size; i++) {
  118.     for (j = 0; j < array_size - i; j++) {
  119.  
  120.       if(array2[j] > array2[j + 1]) {
  121.         int temp = array2[j];
  122.         array2[j] = array2[j + 1];
  123.         array2[j + 1] = temp;
  124.       }
  125.     }
  126.   }
  127.   return *array2;
  128. }
  129.  
  130. void output_array(int array[], size_t array_size) {
  131.   for (size_t i = 0; i < array_size; i++) {
  132.     cout << array[i] << "   ";
  133.   }
  134.   cout << endl;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement