• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reversing Arrays  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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:
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Junilu!  That is very helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!