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...
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
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; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT