Please follow all directions indicated in the document on the picture. Thank you
ID: 3802060 • Letter: P
Question
Please follow all directions indicated in the document on the picture. Thank you in advance!
Let A be an array of n elements. Write a template function. maxfunc(..), which takes an unsorted array of type as an input parameter and if you want the minimum or maximum value. Using TWO stacks to determine the minimum or maximum value and return that element (stack I holds values in ascending/descending order, stack 2 is a "temporary" work stack as you insert values to keep them in order. Once all values have been process you must also print the values in the stack. Assume the operators are defined for the class T. In this question, you may hardcode n = 5. You must write all functions you call, and also need to write a main() to test the following data arrays stored in a data file (data.dat): 4 1 13 3 2 1.1 4.1 8.1 5.2 2.3 the student is in class//> is judged on the basis of alphabetical order In addition, the results should be printed out in main() routine to the screen and to an output file (out.txt)., , Reminder of how templates are defined/work: template void functionMethod('F inData) {cout inData ' ';} int mainQ functionMethod(l.3);//instantiates and calls t(doubLe) functionMethod(7); I I instantiates and calls f(int)Explanation / Answer
Okay, so we will define the template:
void MaxMin (T[] arr)
{
std::stack<T> stack1,stack2;
for (int i=0; i<n /* which is 5 */; ++i)
stack1.push(arr[i]);
T max = stack1.top();
while(!stack1.empty()){
stack2.push(a.top());
stack1.pop()
if(stack2.top() > max){
max = stack2.top(); // find the maximum among the values of a.
}
}
while (!stack2.empty())
{
stack1.push(stack2.top()); // you can print the array elements also here.
stack2.pop();
}
std::cout << max;
// for min just replace max with min and > with < in condition:
//that is chnge: if(stack2.top() > max) to if(stack2.top() < min)
}