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.
public static void reverse(int[] a, int index) {
if (index == (a.length - 1))
System.out.printf("%d%n", a[index]);
else {
reverse(a, index);
public static void main(String[] args) {
int [] array = { 1, 2, 3, 4, 5 };
//Part c: call reverse to print array 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"};
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);