• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

loops

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this . Rob explained well there
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ..
 
Marshal
Posts: 80745
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry sir
for doing that i will not do it again
 
Campbell Ritchie
Marshal
Posts: 80745
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 80745
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 80745
486
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic