Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java 20865 The maximum-valued element of an integer -valued array can be recursi

ID: 3779036 • Letter: J

Question

Java

20865

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 .

20866

The elements of an integer -valued  array can be initialized so that a[i] == i in a recursive fashion as follows:

An array of size 0 is already  initialized ;

Otherwise

set the last element of the array to n-1 (where n is the number of elements in the array , for example, an array of size 3 will have its last element -- index 2-- set to 2; and

initialize the portion of the array consisting of the first n-1 elements (i.e., the other elements of the array )

Write a void method  named  init that accepts an integer  array , and the number of elements in the array and recursively initializes the array so that a[i] == i.

20870

An array is sorted (in ascending order) if each element of the array is less than or equal to the next element .

An array of size 0 or 1 is sorted

Compare the first two elements of the array ; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted.

Write a boolean -valued  method  named  isSorted that accepts an integer  array , and the number of elements in the array and returns whether the array is sorted.

Explanation / Answer

import java.util.Scanner;

public class ArrayRecursionMax {

   /**
   * @param args
   */

   public static int Max(int[] arr, int length) {
       if (length > 0) {
           return Math.max(arr[length], Max(arr, length - 1));
       } else {
           return arr[0];
       }
   }

   public static void recursiveInitializeArray(int[] a, int size) {

       if (size < 0) {
           System.out.println("the initialization completed");
       } else {
           // System.out.println("the size is"+size);
           a[size] = size;
           System.out.println("the array elements are" + a[size]);

           recursiveInitializeArray(a, size - 1);

       }

   }
   //

   public static boolean isSorted(int[] arr, int sindex, int eindex) {
       if (sindex < eindex) {
           isSorted(arr, sindex + 1, eindex);
           if (arr[sindex] <= arr[eindex])// if array start index less than end
                                           // index it calls recursively is
                                           // sorted method
           {
               isSorted(arr, sindex, eindex - 1);
           } else if (arr[sindex] > arr[eindex]) {
               int temp = arr[sindex];// to swap the elements
               arr[sindex] = arr[eindex];
               arr[eindex] = temp;
               isSorted(arr, sindex, eindex - 1);
           }
       } else if (sindex == eindex)
           return true;
       return false;
   }

   public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);

       System.out.println("enter the array size");
       int[] arr = null;

       int size = scanner.nextInt();
       try {

           if (size < 0) {
               System.out.println(" invalid size");
           }

           if (size <= 0) {
               System.out.println("unable to initialize the array because size is zero");

           } else {

               arr = new int[size];

               recursiveInitializeArray(arr, size - 1);

           }

           if (size < 0)
               System.out.println("no elements to find max");

           if (size == 0) {
               System.out.println("no element in the array");
           } else {

               int a = Max(arr, size - 1);

               System.out.println("the max element in array is:" + a);
           }
       } catch (Exception e) {
           System.out.println("unable to perform the operations");
       }

       isSorted(arr, 0, size - 1);

       for (int i = 0; i < size; i++) {
           System.out.println("the sorted elements are" + arr[i]);
       }

   }
}

output

enter the array size
10
the array elements are9
the array elements are8
the array elements are7
the array elements are6
the array elements are5
the array elements are4
the array elements are3
the array elements are2
the array elements are1
the array elements are0
the initialization completed
the max element in array is:9
the sorted elements are0
the sorted elements are1
the sorted elements are2
the sorted elements are3
the sorted elements are4
the sorted elements are5
the sorted elements are6
the sorted elements are7
the sorted elements are8
the sorted elements are9

2nd iteration

enter the array size
4
the array elements are3
the array elements are2
the array elements are1
the array elements are0
the initialization completed
the max element in array is:3
the sorted elements are0
the sorted elements are1
the sorted elements are2
the sorted elements are3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote