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...
Write code to reverse the order of elements on a stack S.c++ ii. Using one additional...
Write code to reverse the order of elements on a stack S.c++ ii. Using one additional queue. iii. using an additional stack and a non array variable c++ .
Question 2. The following code defines an array size that sums elements of the defined array...
Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++) numbers[i] = i+1; int i =0; while (i<=A){    Total += numbers[i];    i++; }...
/* 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...
Sum all the elements in the two-dimensional array below. Print the // result to the console....
Sum all the elements in the two-dimensional array below. Print the // result to the console. var arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] javascript
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT