Question

In: Computer Science

The following code was meant to print out the elements in an array in reverse order....

The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.

public static void reverse(int[] a, int index) {

      if (index == (a.length - 1))

        System.out.printf("%d%n", a[index]);

      else {

        reverse(a, index);

What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of the array in reverse order. Highlight your changes clearly (in bold with yellow highlight) and complete the following main method to show how you would invoke your corrected reverse method to test that it now works correctly.

public static void main(String[] args) {

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

        //Part c: call reverse to print array in reverse order

        <corrected code here>

    }

Solutions

Expert Solution

What does it do?

It keeps on printing the last element and finally a stackoverflow error will occur.

Explain why it behaves in this way?

The reason is the index is not decremented. After each print, the index needs to be decremented and call the function recursively. There is no need for the else part. It will never go to the else part as the index is not decremented and hence recursive call will not take place here.

The corrected code in Java pasted below. The corrected function is shown in red color.

class ArrayReverse1{
public static void reverse(int[] a, int index) {
if (index >0)
{
index= index - 1; // Decrementing the index
System.out.printf("%d%n", a[index]);
reverse(a, index); // Recursive call
}
return;
}

public static void main (String args[])
{
int [] array = { 1, 2, 3, 4, 5 };
int n=array.length;
reverse(array,n); // function call
}
}

Output Screen


Related Solutions

The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
/* print_reverse function that prints the nodes in reverse order if the list is empty print...
/* print_reverse function that prints the nodes in reverse order if the list is empty print nothing */ include <bits/stdc++.h> using namespace std; class Node{     public:     int data;     Node *next;     Node *prev;     Node(int d, Node *p=NULL, Node *n=NULL){         data = d;         prev = p;         next = n;     } }; class DLL{     public:     Node *head;     DLL(){head=NULL;}     void push(int d){         Node *pNode = new Node(d,NULL,head);         if (head != NULL)             head->prev = pNode;         head = pNode;     }     void print_forward(){         Node *pCurr = head;...
How can I write a simple MIPS program to print out the following elements of an...
How can I write a simple MIPS program to print out the following elements of an array of the size of 10: 5,10,15,20,25,30,35,40,45,50
Given an array A that contains the following elements in this order 45, 2, 18, 7,...
Given an array A that contains the following elements in this order 45, 2, 18, 7, 23, 47, 3, 15, 32: (a) Build a min-heap using the bottom-up (reverse) method, showing your work. How many comparisons do you make? (b) Build a min-heap using the top-down (forward) method, showing your work. How many comparisons do you make? (c) Are the two heaps that you get identical? Is this a coincidence of this particular instance? Or would the same happen for...
I want to copy all the elements in f1() to f2() but in reverse order (C++)...
I want to copy all the elements in f1() to f2() but in reverse order (C++) this is my code: #include <iostream> #include <iomanip> using namespace std; const int R=10; const int C=10; int Array[R][C] ; ///////////////////////////////////// void f1(){ for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){ Array[i][j]= (rand () %100) + 1; } } for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){    cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<" ";    } cout<<endl; }...
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
write the code that declares an array of booleans, called myarray, with size of 40 elements
write the code that declares an array of booleans, called myarray, with size of 40 elements
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT