Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value

So far I got:

``````public void reverse(int[] backward)
{
for(int i = (backward.length -1); i >=0;i--){
}
}``````

I think I'm suppose to swap forward array and reverse arrays some how but I don't how to go about doing that like in this one

Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , k , and temp .

so far i got:

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

Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value

So far I got:

``````public void reverse(int[] backward)
{
for(int i = (backward.length -1); i …``````

## All 2 Replies

Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value

So far I got:

``````public void reverse(int[] backward)
{
for(int i = (backward.length -1); i >=0;i--){
}
}
``````

I think I'm suppose to swap forward array and reverse arrays some how but I don't how to go about doing that like in this one

Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , k , and temp .

so far i got:

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

Don't forget to add "=java" without quotes, when declaring a code block.

This isn't too hard of an assignment. If you think about the commands of an array and the hint the Instructor gave you, you should be capable of doing this.

Keep in mind that you have to reverse values (the first and the last) until you reach the middle of the array.

By deduction, you can determine that it should take half as many iterations to reverse the values than to iterate through the entire array--

``````for(int i = 0; i < a.length/2 ; i++) // iterating through the array array.length/2 times
``````

As for swapping, you are always reversing the value from the front to the back, then the 2nd one with the 2nd to last, then the third one from the third to last...

``````// displays the ith element and the ith-to-last element in the array
for(int i = 0; i < a.length; i++)
System.out.println("Value at ["+ i + "] is " + a[i] + ". Value at [" + ((a.length - 1) - i)
+ "] is " + a[((a.length - 1) - i)]);
``````

Put these two concepts together and you should be able to solve the problem.

and for tags it is /code at the end, not \code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.