k x * PROBLEM 2: Do the same translation for this in-place reverse function * in
ID: 3587577 • Letter: K
Question
k x * PROBLEM 2: Do the same translation for this in-place reverse function * in-place means: you may not create an extra array * You should write a helper method. You may not use any "fields" to solve * this problem (a field is a variable that is declared "outside" of the * function declaration either before or after). * Your helper function must be parameterized to allow a smaller problem to k be specified. How do you reverse an array of size N? k (the answer is NOT: reverse an array of size N-1) public static void reverseIterative (double[] a)f int hi a.length 1 int lo = 0; while (loExplanation / Answer
ReverseArray.java
import java.util.Arrays;
public class ReverseArray {
public static void main(String[] args) {
double[] a = new double[]{111.1,22.2,3.2,1,5.1};
reverseRecursive(a);
System.out.println(Arrays.toString(a));
}
public static void reverseRecursive(double[] a) {
reverse(a, 0, a.length - 1);
}
public static void reverse(double[] x, int i, int j) {
if (i < j) {
double tmp = x[i];
x[i] = x[j];
x[j] = tmp;
reverse(x, ++i, --j);
}
}
}
Output:
[5.1, 1.0, 3.2, 22.2, 111.1]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.