In: Computer Science
Design two function templates as follows: -
First function template will be used to sort arrays of different data types in either descending or ascending order. This function template is required to have the following parameters: an array of a generic type, an integer representing the size of the array, and a character representing the order of sorting (i.e., ascending or descending). The last argument (i.e., the order of sorting) is required to be a default argument. The default order of sorting should be ascending.
- Second function template will be used to display an array. This function template is required to have two parameters: an array of a generic type and an integer representing the size of the array.
Design the main program that can be used to test both function templates above. The program should create the two dynamic arrays one of which will store integer values, while the other array will store character values. The size of both arrays will be determined at run time, i.e. entered by the user. The requested order of sorting (i.e., ascending or descending) for each array will also be obtained from the user at run time. The values stored in both arrays will be generated by the random numbers generator (i.e., srand() and rand() library functions could be used). Please note that the random characters' ASCII values should be in the range from 33 to 126, while the random integer numbers should be in the range from 1 to 100. The program will call both templates to sort arrays in the requested order and display both arrays before and after sorting. It is required to include all necessary error checking which includes the code to prevent any "garbage input" from the user as well as the code to prevent the memory allocation error.
Code
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
template <typename T>
void printArray(T arr[],int size);
template <typename T>
void sortArray(T arr[],int size,char order='a');
int main()
{
srand(time(0));
int *intArray;
char *charArray,sortingOrder;
int size;
cout<<"Enter the size of the array: ";
cin>>size;
cout<<"a) for ascending\nd) for descending \n
Your choice: ";
cin>>sortingOrder;
intArray=new int[size];
charArray=new char[size];
for(int i=0;i<size;i++)
{
intArray[i]=rand() % 100 + 1;
charArray[i]=rand() % 126 +
33;
}
cout<<"\n\nBefore sortig array
are:"<<endl;
cout<<"Integer array is: "<<endl;
printArray(intArray,size);
cout<<"\nChar array is: "<<endl;
printArray(charArray,size);
sortArray(intArray,size,sortingOrder);
sortArray(charArray,size,sortingOrder);
cout<<"\n\nAfter sortig array
are:"<<endl;
cout<<"Integer array is: "<<endl;
printArray(intArray,size);
cout<<"\nChar array is: "<<endl;
printArray(charArray,size);
}
template <typename T>
void printArray(T arr[],int size)
{
for(int i=0;i<size;i++)
cout<<arr[i]<<endl;
}
template <typename T>
void sortArray(T a[],int size,char order)
{
int i,j;
T temp;
if(order=='a')
{
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
temp =a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
else
{
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]<a[j])
{
temp =a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.