Question: Hi, I need help for this C++ programming problem. I really appreciate your time and work I am clueless on how to make the maxArray
Hi, I need help for this C++ programming problem. I really appreciate your time and work
I am clueless on how to make the maxArray function a recursive template function. Is the template function in a separate header file and I basically call it from tha main file? Confused, please help. I will paste my current solution down below. I am not sure if I followed the instructions correctly but it does compile. So if I did it wrong please correct and update my code.(leave comments so I can better understand what is happening)
Instructions from professor:
Name the program maxarray.cpp. Make sure the following requirements are met.
- Program must compile and run.
- maxArray function must be a recursive template function.
- Add a main function to test the maxArray function so that you have a complete program.
- Test the maxArray function on two arrays of different types
maxarray.cpp
#include
using namespace std;
int maxArray(int anArray[], int len); //function prototype
//function to find the maximum value
int maxArray(int anArray[], int len)
{
if (len == 1)
return anArray[0];
return max(maxArray(anArray, len - 1), anArray[len - 1]);
}
int max(int n1, int n2)
{
return n1 > n2 ? n1 : n2;
}
int main()
{
int anArray[] = {5,2,10,3,30,21}; //declare a variable
int n = sizeof(anArray)/sizeof(anArray[0]); //calculate array size
cout << "The maximum element in the given array is: " << maxArray(anArray, n) << endl; //print the max array by recursive calling
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
