# An array of ints, arr, has been declared and initialized. Write the statements needed to reverse the elements in the array. So, if the elements were originally 5, 13, 4, 97 then after your code executes they would be 97, 4, 13, 5.

3

### CHALLENGE:

An array of ints, arr, has been declared and initialized. Write the statements needed to reverse the elements in the array. So, if the elements were originally 5, 13, 4, 97 then after your code executes they would be 97, 4, 13, 5.

### SOLUTION:

```int k, temp;
for (k = 0; k < arr.length/2; k++){
temp = arr[k];
arr[k] = arr[arr.length-1-k];
arr[arr.length-1-k] = temp;
}
```

• underdog says:

for(int i = 0; i < arr.length / 2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.length – i – 1];
arr[arr.length – i – 1] = temp;
}

• AL says:

int[] tempArr = new int[arr.length];
for(int i = 0; i < arr.length; i++) {
tempArr[i] = arr[arr.length – i – 1]; }
arr = tempArr;

• BAT says:

int k, temp;
for (k = 0; k < arr.length/2; k++){
temp = arr[k];
arr[k] = arr[arr.length-1-k];
arr[arr.length-1-k] = temp;
}