• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reversing Arrays

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, what does he initialize "int temp"?  Why does he set "temp = arr[start]" and "arr[end] = temp" for this reversal of array values?

Code:
 
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This swaps the values held in arr[start] and arr[end]. It uses temp as a temporary holder of values as they are being swapped.
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need a temporary place holder to reverse an array?  Can't you reverse the array right away without holding it somewhere?
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without temp you'd end up with this

Think about it. What would happen to the contents of arr[start]?

When in doubt, write a little 10 line program to see if you are correct.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps if you do the operation on a whiteboard. Draw a square to represent the start element of the array. Draw another square to represent the end element of the array. Inside the squares, write the value held by that element. It would look something like this:

Then do the operations as the computer would do them. What happens when you assign the value of the end element to the start element? What happened to the old value of the start element? Then, what happens now when you assign the value of the start element to the end element?

Try it again, only this time, add another box for temp. What happened?
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry I still don't get why you need a temporary place holder for the array.  I know by setting arr[start] = arr[end], or arr[3] = arr[8] doesn't make any logical sense.  But isn't this saying the same thing when you have:



What's the difference?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant that 3 and 8 were the values of the start and end array elements respectively, not index values. Don't think of index values at all, just think of them as single-item capacity holders of things. If one element can hold only one thing at a time then you can't just simply assign a new value without first saving the old value. The temp variable is the holder of that saved old value.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Timothy Han wrote:I'm sorry I still don't get why you need a temporary place holder for the array.  



There are actually a few ways to swap two variables' values without a temporary variable. Most of the time, it is not done, because it may be confusing to understand (and developers tend to avoid confusing code). I am assuming that you are asking this question, because you know of a way to do it? If so, can you just show us -- and we can either confirm or deny whether it will work?

Henry
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Junilu!  That is very helpful.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic