Question

in java Write a program that uses recursion to find the largest number in an array....

in java

Write a program that uses recursion to find the largest number in an array. Declare and initialize an array of 10 different numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class RecursiveLargest {

    public static int largest(int[] arr) {
        return largest(arr, arr.length);
    }

    public static int largest(int[] arr, int size) {
        if (size == 1) {
            return arr[0];
        } else {
            int large = largest(arr, size - 1);
            if (large < arr[size - 1]) {
                large = arr[size - 1];
            }
            return large;
        }
    }

    public static void main(String[] args) {
        int[] numbers = {3, 8, 1, 4, 9, 2, 6, 7, 5, 0};
        System.out.println("Largest number in array is " + largest(numbers));
    }
}

Add a comment
Know the answer?
Add Answer to:
in java Write a program that uses recursion to find the largest number in an array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in C++ language that finds the largest number in an array of positive...

    Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...

  • Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array...

    Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array of 100 integers. Initialize the array to have all zeros. 2. Puts even numbers starting from 6 (excluding multiples of 12) into the first 50 slots of the array above. So numbers 6,8,10,14,16,18,20,22,26, etc. go into the first 50 positions in the array. Print the array. 3. Puts random numbers between 45 and 55 into the second 50 slots of the array above (second...

  • Write a C program that uses the random number generator rand( ) to create an array...

    Write a C program that uses the random number generator rand( ) to create an array with 20 numbers with value in the range from 1 to 100. The program calculates and displays the difference between the largest array element and the second largest array element in the array.

  • Write a java program that create a 2 dimensional array of type double of size 10...

    Write a java program that create a 2 dimensional array of type double of size 10 by 10. Fill the array with random numbers using a random number generator. Print the array contents. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the numbers 1 to 42 into the file. Close the file. Attach both files. Thank you in advance

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • Write a java Program Initialize Array. Write a Java method initArray() with the following header to...

    Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call

  • Asap Please write a Java program with comments Asap: Please write a java program with comments...

    Asap Please write a Java program with comments Asap: Please write a java program with comments Write a program that uses recursion to display concentric circles, as shown in the figure below. The circles are centered in the pane. The gap between two adjacent circles is 10 pixels, and the gap between the border of the pane and the largest circle is also 10 pixels. res

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT