This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

swapping the data

 
MD SALAHUDDIN
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some values in array like
int a[]={1,2,3,4,5,6,7,8,.....};
i have to swap the elements of array to get the result
a[]={2,1,4,3,6,5,8,7,...};
can anyone help me out.
Thanks in advance
sallu
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specific problems are you having ?
I'm not going to do your homework for you
 
John Brookes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just have to swap each pair within the array, you'd need a dummy variable and a loop.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To illustrate that:

There are some cool mathematical tricks to swap two integers without using a temp variable, but this is the kindest solution to future readers of your code.
Hope that helped. And hope I haven't just stepped in and done somebody's homework.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic