Question

In: Computer Science

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);

  1. What does it do? Explain why it behaves in this way.                                 

 

  1. 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).                                                                                                                               

                                                                                                                               

  1. Complete the following main method to show how you would invoke your corrected reverse method to test that it now works correctly.                                                        (1 mark)

 

 

   public static void main(String[] args) {

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

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

       

    }

  1. Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or arrays of other reference types) in reverse order.

Integer[] integerArray = {1,2,3,4,5,6};

   Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};

   String[] numbers = {"one", "two", "three", "four", "five", "six"};

 

Solutions

Expert Solution

Answer 1:

Here we are not printing reversly as we printing at before resurvice cal only thats it will not print reveser:

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


       // we should check till length

// Answer 2
       if (index == (a.length))
           return;
       reverse(a, index+1);

// Answer 3:
       // we should print after recursive call
       System.out.printf("%d%n", a[index]);

   }

Answer 4:

// we should pass index as 0

reverse(array, 0);

 


Related Solutions

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...
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
45. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements....
45. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. a. takeArray(items[9]) b. takeArray(items[]) c. takeArray(items) d. Arrays cannot be passed to methods – each item must be sent to the method separately. 46. When an argument is passed by reference, ____________. a. a copy of the argument’s value is passed to the called method b. the original value is removed from memory c. changes to the argument do not affect the original variable’s...
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()...
Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT