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 93 94 95 96
|
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void myOrder(int numb, int random);
void myOrder(int numb, int random)
{
int asc, des;
int list[numb];
qsort(list, numb, sizeof(int), compare);
cout<<"\n\n\nThe "<<numb<<" numbers in Ascending Order ";
for(int i = 0; i < numb; i++)
{
cout<<list[i]<<" ";
}
cout<<"\nThe "<<numb<<" numbers in Descending Order ";
for(int i = numb; i >= 0; i--)
{
cout<<list[i-1]<<" ";
}
}
void mySort(int numb, int random);
void mySort(int numb, int random)
{
int list[numb];
qsort(list, numb, sizeof(int), compare);
cout<<"\n\n\nThe highest number is "<<list[(numb-1)];
cout<<"\nThe lowest number is "<<list[0];
}
int getNumb();
int getNumb()
{
bool restart = false;
int numb;
do
{
cout<<"How many numbers would you like to be generated(2-20)? ";
cin>>numb;
if ((numb >= 2 )&&(numb <= 20))
{
cout<<"\n\nThe "<<numb <<" numbers generated are:\n";
return numb;
}
else
{
cout<<"Sorry, that is invalid try again.\n";
restart = true;
}
}
while (restart == true);
}
int myRandom(int numb);
int myRandom(int numb)
{
int random[numb];
for (int i = 0; i < numb ; i++)
{
random[i] = rand() % 20 + 2;
cout<<random[i]<<" ";
}
return random[0];
}
int main()
{
int numb, random;
srand (time(NULL));
cout<<"Data Searching and Sorting!!\n\n";
numb = getNumb();
random = myRandom(numb);
mySort(numb, random);
myOrder(numb, random);
cin.get();
cin.get();
return 0;
}
|