Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# loops

winny dhar
Greenhorn
Posts: 13
hello everyone..
i am getting a problem in writing a program to reverse the element of an array using one loop only..so can any one help me out???

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Look at this . Rob explained well there

Clivant Yeo
Ranch Hand
Posts: 124
Compute the midpoint of the array.
Loop number of times equal to midpoint + 1.

Maintain two counters:
1) One counting up from 0 to midpoint.
2) One counting down from last index to midpoint.

Swap the element at the first counter with the element at the second counter

If first counter == second counter, break out of the loop.

winny dhar
Greenhorn
Posts: 13
thanks ..

Campbell Ritchie
Sheriff
Posts: 50216
79
Clivant Yeo wrote:. . .
Loop number of times equal to midpoint + 1.

Maintain two counters:
. . .

If first counter == second counter, break out of the loop.

Why + 1? That looks like a mistake.
You don’t need two counters. Only one.
If you use the correct values to count to, there is no need for the break. In fact, using break might introduce an out-by-one error, but I am not certain on that point. Depends how you use it.

sagar kumar nerella
Greenhorn
Posts: 18
hello
if i understood your problem correctly
you want some code demo showing the how to reverse the elements of an array using only one loop.

here i am using a temporary array also.

assume your main array of type int and referenced with name mainarray

i hope it will help u.

if i understood your problem wrongly ignore this

Jeff Verdegan
Bartender
Posts: 6109
6
sagar kumar nerella wrote:hello
if i understood your problem correctly
you want some code demo showing the how to reverse the elements of an array using only one loop.

Sagar, please remember, this site is NotACodeMill. Simply handing somebody the code does not help that person to learn.

sagar kumar nerella
Greenhorn
Posts: 18
sorry sir
for doing that i will not do it again

Campbell Ritchie
Sheriff
Posts: 50216
79
Apology accepted, but in line with what it says on the title page:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers
... I shall delete the answer. There are better solutions, I believe.

Rob Spoor
Sheriff
Posts: 20665
65
Campbell Ritchie wrote:You don’t need two counters. Only one.

You don't need two, but with one you need to calculate the second index each time. With a counter you don't. In the end this is micro optimization, but I too prefer the two counters. The loop then ends when the two are equal.

If you use the correct values to count to, there is no need for the break. In fact, using break might introduce an out-by-one error, but I am not certain on that point. Depends how you use it.

If you break at the exact center that doesn't matter. If you swap the center element with itself or not will give the same result.

Campbell Ritchie
Sheriff
Posts: 50216
79
I like to count from 0 to < length / 2. If it has an odd number of elements, you miss the middle element. If it has an even number, you stop at the middle. You do have to swap with array[length - i - 1], however.

If you find that difficult to envisage, work it out with a pencil and paper.
Start by writing a swapElements method. Something like thisYou should have that sort of method somewhere anyway, for general use for swapping pairs in arrays.

Campbell Ritchie
Sheriff
Posts: 50216
79
• 1
What I meant about break; is that you don’t need it. In the format of loop I usually write, it readsand if you use two indicesSo you don’t need the break; statement at all.

Rob Spoor
Sheriff
Posts: 20665
65
You're right about not needing the break. I usually use the second form in that exact same format.

Clivant Yeo
Ranch Hand
Posts: 124
Campbell Ritchie wrote:
Clivant Yeo wrote:. . .
Loop number of times equal to midpoint + 1.

Maintain two counters:
. . .

If first counter == second counter, break out of the loop.

Why + 1? That looks like a mistake.
You don’t need two counters. Only one.
If you use the correct values to count to, there is no need for the break. In fact, using break might introduce an out-by-one error, but I am not certain on that point. Depends how you use it.

Hi Campbell,

You are right about the midpoint, didn't gave much thoughts. Hmm, I am suggesting to use two counters because I am thinking in the direction of an in-place reverse algorithm.

Thanks for highlighting the mistakes!