views:

517

answers:

3

write a program that enters an array of characters from the user and reverses the sequence without creating a new array

+1  A: 

Swap the ends constantly, using a single variable as a temporary buffer. In pseudo-code:

temp = a[0]
a[0] = a[size - 1]
a[size - 1] = temp

and so on.

mathepic
Think that should be `size - 1`, not `size - 2`.
Thomas
If you use a compiler written in the last couple of decades, it will optimize it better for the architecture it's on ( which may be XOR, or may use a register temporary )
Pete Kirkham
If you use the xor swap method, you can eliminate the temporary. In pseudocode:a[curr] = a[curr] xor a[size - curr] a[curr] = a[curr] xor a[size - curr] a[curr] = a[curr] xor a[size - curr] ++curr
Jeff Paquette
Oops, not sure why I put size - 2. Thanks, Thomas.
mathepic
@flyfishr64 - **Bad idea. Do not** use the XOR swap method here. If there is an odd number of array elements (or 1 element), XOR swapping an element with itself will result in 0. If you have to check this condition, you might as well just use the temp variable
Robert Cartaino
Compiler will probably turn temp into a register anyway.
mathepic
@Robert if you don't check that condition in `for (int start = 0, end = length-1; start < end; ++start,--end) swap(array,start,end);` , how will the loop terminate?
Pete Kirkham
@Pete - No, I was talking about *not* using the XOR swap method because there are special conditions you have to check for. Read this article (and responses) for details about why this is a bad idea: http://www.codeproject.com/Feature/WickedCode.aspx?msg=2565905#xx2565905xx. Even if the code works, the XOR swap has been a **terrible optimization** for a very long time anyway.
Robert Cartaino
A: 

Dont bother reversing the array in memory, just iterate over it backwards!

Rasmus Kaj
If you were to pass the array to a library function that iterates forwards but you need it reversed, then you would have to reverse the array in memory to do so.But, in some cases, you are correct.
mathepic
+1  A: 

Homework means pseudo-code only from me, which you've made relatively easy by not specifying what language you want anyway :-)

Turn this into your language of choice:

Set i1 to index of first element in array
Set i2 to index of last element in array
while i1 < i2:
    Set temporary variable to element number i1
    Set element number i1 to element number i2
    Set element number i2 to temporary value
    Add 1 to i1
    Subtract 1 from i2

An ideal thing to do is to actually run that algorithm in your head, using a piece of paper to keep track of variables:

  • each the elements in the array.
  • i1 and i2.
  • temporary variable.

I tend to do that for simpler algorithms. Harder ones I insert debug statement into so that the computer can do that grunt work for me. Start with a piece of paper thus:

i1 | i2 | tempvar | el[0] | el[1] | el[2] | el[3] | el[4] | el[5]
---+----+---------+-------+-------+-------+-------+-------+------
                      H       e       l       l       o       !

and just run through the steps one by one, checking and/or changing each column as you go. That will result in you understanding how it works far better than just being given some code.

paxdiablo