Java netbeans (recursion) 3- Write a recursive Java method that computes the max
ID: 3729623 • Letter: J
Question
Java netbeans (recursion)3- Write a recursive Java method that computes the maximum element in an array of integers. Test your method. Hint: your method may have three parameters: array of integers, array length, and current max value. Java netbeans (recursion)
3- Write a recursive Java method that computes the maximum element in an array of integers. Test your method. Hint: your method may have three parameters: array of integers, array length, and current max value.
3- Write a recursive Java method that computes the maximum element in an array of integers. Test your method. Hint: your method may have three parameters: array of integers, array length, and current max value.
Explanation / Answer
// Highlighted is the method you asked for
class Main
{
public static void main (String []args)
{
int A[] = {2,5,7,-4,6,3};
System.out.println(FindMax(A,5,A[0])); //n is the index of the last element in the array
}
public static int FindMax(int A[], int n, int max)
//n is the index of the last element in the array
{
// when all numbers are done, returning max
if(n == 0)
return max;
// checking if the n'th number is greater
if(max < A[n-1])
{
// updating maximum value
max = A[n-1];
}
// calling recursively with 1 less
return FindMax(A,n-1,max);
}
}
// Output: 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.