Question: The goal of the program is to reverse an array. Complete that program, by defining a reverse function, that reverses the contents of an array
The goal of the program is to reverse an array. Complete that program, by defining a reverse function, that reverses the contents of an array given as an argument. Hint: first try to swap elements at 2 specific positions (e.g. swap element at position 0 with the one at position 2). You will need an extra variable. See Section 6.3.8 (page 262) from the book. Sample run (after you implement and call the reverse method):
public class hw5_task5
{ public static void main(String[] args)
{ double[] a = {3.2, 2.1, 5.3, 8.0, 4.9, 5.7};
double[] b = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
printDoubleArray("a", a); // TO DO: call the reverse method on the array a printDoubleArray("a (after reverse)", a);
System.out.printf(" ");
printDoubleArray("b", b); // TO DO: call the reverse method on the array b printDoubleArray("b (after reverse)", b);
} public static void printDoubleArray(String name, double[] a)
{ System.out.printf("%20s: ", name);
if (a == null)
{ System.out.printf("Null array! "); return; }
for (int i = 0; i < a.length; i++) {
System.out.printf("%7.2f", a[i]); }
System.out.printf(" ");
} }
a: 3.20 2.10 5.30 8.00 4.90 5.70
a (after reverse): 5.70 4.90 8.00 5.30 2.10 3.20
b: 1.10 2.20 3.30 4.40 5.50 6.60
b (after reverse): 6.60 5.50 4.40 3.30 2.20 1.10
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
