Please help with template program for C++ please. Please send correct code and o
ID: 665204 • Letter: P
Question
Please help with template program for C++ please. Please send correct code and out put for less confusion in learning the concepts.
For this program, we're going to have fun with templates. Templates allow us to write generic functions that work with any data type
Write the following functions as templates:
void Sort(dtype Data[], int ArrSize); // sorts Data array, returns nothing. Assumes < operator is defined for whatever data type is being passed in.
void Sort(vector<dtype>& V); // sorts vector, returns nothing
int Lsearch(const dtype Data[], int ArrSize, const dtype& target); // Linear search, assumes == operator is defined for this data type. Returns location of first occurrence of target value if present, -1 if not present.
int Count(const vector<Dtype>& V, const Dtype& target); // returns count of how many times target value appears in vector
Your main program will have 3 vectors and 3 arrays: integers, strings, and doubles. You will demonstrate that your template functions work with all of the appropriate data types.
Development notes:
The main program should have arrays of 3 different data types, and vectors of 3 different types. Use the rand() function to generate values as needed.
The main program should show (print) the before and after states of the arrays that are sorted, and should verify correct functioning of the search and count functions (for example, call the function for a particular value, then do an assignment that changes the expected results and call it again).
Functions that are designed for vectors should make appropriate use of iterators.
Explanation / Answer
this question has lot of subparts. I have answered few. Please post 2 more questions.
#include<iostream.h>
void Sort (int arr[], int length){
int j, temp;
for (int i = i; i < length; i++){
j = i;
while (j > 0 && arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
}
int main()
{
int i;
int arr[]={
1,5,3,2,65,1,5,7
};
Sort(arr,8);
cout<<"sorted array"<<endl;
for(i=0;i<8;i++)
{
cout<<arr[i]<<endl;
}
}