Ask Question

java The maximum-valued element of an integer-valued array can be recursively calculated as follows: If the array has a single element, that is its maximum (note that a zero-sized array has no maximum) Otherwise, compare the first element with the maximum of the rest of the array- - whichever is larger is the maximum value. Write an int method named max that accepts an integer array, and the number of elements in the array and returns the largest value in the array. Assume the array has at least one element.

+3
Answers (1)
  1. 3 May, 04:55
    0
    see explaination

    Explanation:

    MaxArray. java

    public class MaxArray{

    public static void main (String[] args) {

    int a[] = {1,2,5,4,3};

    int max = max (a, 5);

    System. out. println ("Max value is "+max);

    }

    public static int max (int a[], int size) {

    if (size > 0) {

    return Math. max (a[size-1], max (a, size-1));

    } else {

    return a[0];

    }

    }

    }

    Output:

    MaxArray
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “java The maximum-valued element of an integer-valued array can be recursively calculated as follows: If the array has a single element, ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers