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

Puts the contents of arr in reverse order into rev_arr For example the first ent

ID: 3715365 • Letter: P

Question

Puts the contents of arr in reverse order into rev_arr For example the first entry of arr will correspond to the last entry of rev_arr Code is already written to print out the results of both arrays and their corresponding entries. The values in them should match once you have completed the proper code.

for (int i = 0; i < arr.length; i++) {

/* Fill in your code here

* To reverse the arr into rev_arr

*/

}

// Printing out the results

System.out.println(" Printing out both array contents and they should be the same");

for (int i = arr.length; i > 0; i--) {

System.out.print("Arr[" + (arr.length-i) + "]=" + arr[arr.length-i]);

System.out.println(" same as Rev_Arr[" + (i-1) + "]=" + rev_arr[i-1]);

}

Explanation / Answer

ReverseArray.java

public class ReverseArray {

public static void main(String[] args) {

int arr[] = {1,2,3,4,5,6,7,8,9,10};

int rev_arr[]=new int[arr.length];

for (int i = 0,j=arr.length-1; i < arr.length; i++,j--) {

/* Fill in your code here

* To reverse the arr into rev_arr

*/

rev_arr[i]=arr[j];

}

// Printing out the results

System.out.println(" Printing out both array contents and they should be the same");

for (int i = arr.length; i > 0; i--) {

System.out.print("Arr[" + (arr.length-i) + "]=" + arr[arr.length-i]);

System.out.println(" same as Rev_Arr[" + (i-1) + "]=" + rev_arr[i-1]);

}

}

}

Output:

Printing out both array contents and they should be the same

Arr[0]=1 same as Rev_Arr[9]=1

Arr[1]=2 same as Rev_Arr[8]=2

Arr[2]=3 same as Rev_Arr[7]=3

Arr[3]=4 same as Rev_Arr[6]=4

Arr[4]=5 same as Rev_Arr[5]=5

Arr[5]=6 same as Rev_Arr[4]=6

Arr[6]=7 same as Rev_Arr[3]=7

Arr[7]=8 same as Rev_Arr[2]=8

Arr[8]=9 same as Rev_Arr[1]=9

Arr[9]=10 same as Rev_Arr[0]=10