programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# loops

Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
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???

Ranch Hand
Posts: 5575
• Number of slices to send:
Optional 'thank-you' note:
Look at this . Rob explained well there

Ranch Hand
Posts: 124
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
thanks ..

Marshal
Posts: 79406
377
• Number of slices to send:
Optional 'thank-you' note:

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.

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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

Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
sorry sir
for doing that i will not do it again

Campbell Ritchie
Marshal
Posts: 79406
377
• Number of slices to send:
Optional 'thank-you' note:
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.

Sheriff
Posts: 22784
131
• Number of slices to send:
Optional 'thank-you' note:

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
Marshal
Posts: 79406
377
• Number of slices to send:
Optional 'thank-you' note:
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
Marshal
Posts: 79406
377
• 1
• Number of slices to send:
Optional 'thank-you' note:
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: 22784
131
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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!

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.