Question

In: Computer Science

Design two function templates as follows: - First function template will be used to sort arrays...

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.

Solutions

Expert Solution

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.


Related Solutions

A template (generic) function for insertion sort is given to you. Part-1: design a function to...
A template (generic) function for insertion sort is given to you. Part-1: design a function to compare 2 strings using case-insensitive comparison, a function to test if 2 records are not equal (using case-insensitive string comparison). Part-2: define your own compare function required by the template insertionSort function. #include <iostream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> #define BASE 1000000000LL // long long int using namespace std; struct record { long long value; string str; }; // Part-1: case-insensitive...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
a. What is/are the specific template/templates used on the replication, transciption, and translation of the prokaryotes...
a. What is/are the specific template/templates used on the replication, transciption, and translation of the prokaryotes vs. eukaryotes? b. What is the direction of synthesis with respect to new strands/product on the replication, transciption, and translation of the prokaryotes vs. eukaryotes? c. What are the enzymes involved in the Initiation, elongation, termination products on the replication, transciption, and translation of the prokaryotes vs. eukaryotes? d. What is the site of occurrence on the replication, transciption, and translation of the prokaryotes...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
Function Template and Exception Handling In part one, you are going to implement the selection sort...
Function Template and Exception Handling In part one, you are going to implement the selection sort function that is capable of sorting vectors of int, double or string. In part two you will be writing a try catch block to catch the out-of-range exception. You are to write three functions and manipulate main() function for this lab all of which should be written in one main.cpp file: Part one: unsigned min_index(const vector<T> &vals, unsigned index): Passes in an index of...
Create an Excel workbook containing two depreciation schedule templates, each on a separate spreadsheet. One template...
Create an Excel workbook containing two depreciation schedule templates, each on a separate spreadsheet. One template should be a SL depreciation schedule and the other template should be a DDB depreciation schedule. You should construct the spreadsheets using the formulas and cell referencing so that when the value of input variables are altered the calculations which automatically adjust. The spreadsheet columns should include depreciation expense, accumulated depreciation, and book value end of year (see textbook 575-576 for examples). Please note,...
Create an Excel workbook containing two depreciation schedule templates, each on a separate spreadsheet. One template...
Create an Excel workbook containing two depreciation schedule templates, each on a separate spreadsheet. One template should be a SL depreciation schedule and the other template should be a DDB depreciation schedule. You should construct the spreadsheets using the formulas and cell referencing so that when the value of input variables are altered the calculations which automatically adjust. The spreadsheet columns should include depreciation expense, accumulated depreciation, and book value end of year. Please note, you can not depreciate assets...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT