Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <windows.h>
- #include <conio.h>
- using namespace std;
- int input_array(int [], size_t);
- int make_second_array(int [], size_t, int []);
- int make_third_array(int [], size_t , int []);
- void output_array(int [], size_t);
- int main() {
- int choice;
- size_t n;
- cout << "Enter array size: ";
- cin >> n;
- if(!cin || n > 30 || n < 0)
- return 0;
- int array[n];
- int array2[n];
- int array3[n];
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "1. Input array\n";
- cout << "2. Make second array and reverse it\n";
- cout << "3. Make third array and sort it\n";
- cout << "4. Print arrays\n";
- cout << "5. Exit\n\n";
- cout << "Make your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- input_array(array, n);
- Sleep(1000);
- break;
- case 2:
- system("cls");
- make_second_array(array, n, array2);
- cout << "Second array created and reversed!";
- Sleep(1250);
- break;
- case 3:
- system("cls");
- make_third_array(array, n, array3);
- cout << "Third array created and sorted in ascending order!";
- Sleep(1500);
- break;
- case 4:
- system("cls");
- output_array(array, n);
- output_array(array2, n);
- output_array(array3, n);
- cout << "\n\n Press any key to get back to the menu...";
- getch();
- break;
- }
- } while(choice != 5);
- return 0;
- }
- int input_array(int array[], size_t array_size) {
- for (size_t i = 0; i < array_size; i++) {
- cout << "Enter value for element #" << i << ": ";
- cin >> array[i];
- }
- return *array;
- }
- int make_second_array(int array[], size_t array_size, int array2[]) {
- size_t i, j;
- for (i = 0; i < array_size; i++) {
- array2[i] = array[i];
- }
- j = i - 1; // last element of the array
- i = 0; // null i to reuse
- while(i < j) {
- int temp;
- temp = array2[i];
- array2[i] = array2[j];
- array2[j] = temp;
- i++;
- j--;
- }
- return *array2;
- }
- int make_third_array(int array[], size_t array_size, int array2[]) {
- size_t i, j;
- for (i = 0; i < array_size; i++) {
- array2[i] = array[i];
- }
- for (i = 0; i < array_size; i++) {
- for (j = 0; j < array_size - i; j++) {
- if(array2[j] > array2[j + 1]) {
- int temp = array2[j];
- array2[j] = array2[j + 1];
- array2[j + 1] = temp;
- }
- }
- }
- return *array2;
- }
- void output_array(int array[], size_t array_size) {
- for (size_t i = 0; i < array_size; i++) {
- cout << array[i] << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement