Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of the program is to reverse an array. Complete that program, by defini

ID: 3684657 • Letter: T

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

Explanation / Answer

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

printDoubleArray("a (after reverse)", reverseArray(a));

System.out.printf(" ");

printDoubleArray("b", b);

// TO DO: call the reverse method on the array b printDoubleArray("b (after reverse)", b);

printDoubleArray("b (after reverse)", reverseArray(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(" ");

}

}

public static double[] reverseArray(double[] data)

{

    int arr_length = data.length;

    double[] reversedData = new double[arr_length];

    int j = arr_length - 1;

    for(int i=0; i < arr_length; i++);

    {

        reversedData[i] = data[j--];

    }

    return reversedData;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote