Please use the C thank you so much Recursive max Consider a
Please use the C++, thank you so much!
Recursive max Consider a recursive function to find the maximum value in an array of integers. The function declaration is: int maxValue( int vals[], int size, int start ) For this function, we need to know the size of the array arid the starting index of the array (because both will change when a recursive call is made). You may assume that there is at least one value in the array: Describe the base case: Describe the reduction step: Describe why the reduction step will (i) solve the problem and (ii) move closer to the base case: Write the complete function definition for maxValue:Solution
a)Base case: If only one element is present in array.
i.e.start==size
if this case comes then return valus[start] i.e first element;
Base case code:
if (start == size) {
return valus[start];
}
b)Reduction Step:
In this phase,we are actually implementing same reduction as Binary Search Algorithm.
We are comparing elements from start and end.
If element of start is smaller than element of last,then we move our start pointer forward i.e recursively call for maxValue(valus,start+1,end)
else element of start is greater than element of last,then we move our last pointer backword i.e recursively call for maxValue(valus,start,end-1)
c)Reduction step will reduce the number of comparison.It tries to reduce size of array each time when we call recursive function.As we apply reduction,size of array finally comes to 1,which is our base case.if start and last elements are same then it returns as maximim element.
d)
int maxValue(int valus[],int start,int end)
{
if (size == start) {
return valus[start];
} else if (valus[start] < valus[size]) {
return maxValue(valus, start + 1, size);
} else {
return maxValue(valus, start, size-1);
}
}
