Ask Question
5 August, 10:56

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.

+4
Answers (1)
  1. 5 August, 13:01
    0
    public static void init (int[] arr, int n) {

    if (n==0)

    arr[0] = 0;

    else {

    arr[n-1] = n - 1;

    init (arr, n-1);

    }

    }

    Explanation:

    Create a method called init that takes two parameters, an array and the number of elements in the array

    When n reaches 0, set the first element to 0 (This is a base for our recursive method)

    Otherwise, set the element in index i to i

    Call the init inside the init, this is the recursion part, with same array but decrease the number of elements by 1 (We decrease the number of element by 1 in each time so that it goes through all the elements in the array)
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “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 ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers