1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
using namespace std;
void instructions();
void inputSelf(int[], int);
void autoInput(int[], int);
void sortArray(int[], int);
int main()
{
int size;
char decision;
int array[size];
instructions();
cout << "Would you like to input your own integers? (Y for yes): ";
cin >> decision;
if(decision == 'Y' || decision == 'y')
{
cout << "Enter amount of integers to be sorted: ";
cin >> size;
inputSelf(array, size);
}
else
{
size = 20;
autoInput(array, size);
}
cout << "Unsorted array:" << endl;
for(int i=0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
sortArray(array, size);
cout << "Sorted array:" << endl;
for(int i=0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
return 0;
}
void instructions()
{
cout << "This program will sort integers in order for you." << endl
<< "You may enter your own numbers or use predesignated integers." << endl;
}
void sortArray(int array[], int size)
{
int newValue;
for(int i=1; i < size; i++)
{
newValue = array[i];
int j;
for(j=i-1; array[j]>newValue; j--)
{
array[j+1] = array[j];
}
array[j+1] = newValue;
}
}
void inputSelf(int array[], int size)
{
cout << "Please input " << size << " integers: ";
for (int i=0; i < size; i++)
{
cin >> array[i];
}
}
void autoInput(int array[], int size)
{
int const preset[] = {3, 47, 45, 27, 21, 9, 36, 33, 8, 5,
41, 26, 20, 37, 6, 11, 44, 42, 32, 28};
for (int i=0; i < size; i++)
{
array[i] = preset[i];
}
}
|