# Reversing an array only using two variables.

Page 1 of 1

## 4 Replies - 15531 Views - Last Post: 25 April 2012 - 06:53 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=276831&amp;s=1ba4d35ec06cb9a2eb4273b5be349fe0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shango11

Reputation: 3
• Posts: 78
• Joined: 06-July 11

# Reversing an array only using two variables.

Posted 25 April 2012 - 03:05 PM

Problem: Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , k , and temp .

```temp = 1;
for(k = 0; k <= a.length-1; k++)
{
a[k] = a[a.length-temp];
temp++;
}

```

I can only get the code to work for half the array, and I'm stumped since I'm not allowed to use anything else other than the two variables and the one array.

This post has been edited by shango11: 25 April 2012 - 03:09 PM

Is This A Good Question/Topic? 0

## Replies To: Reversing an array only using two variables.

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Reversing an array only using two variables.

Posted 25 April 2012 - 03:10 PM

No, no, no. You use the temp variable to store one of the array values during the swap.

For example, here's one swap. You figure out the rest using the variable k as an index control:

temp = a[0];
a[0] = a[a.length - 1];
a[a.length - 1] = temp;

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Reversing an array only using two variables.

Posted 25 April 2012 - 06:04 PM

Actually you should do it using

for(k = 0; k < a.length / 2; k++)

if you don't do so
1) you will first reverse the first half of the array with the second half
2) then reverse the second half with the first half, reversing what you have just done

P.S.
if it is an array of int[] I can even do it without the temp variable (if you want to impress your teacher )

### #4 floppyspace

• D.I.C Regular

Reputation: 48
• Posts: 256
• Joined: 04-February 10

## Re: Reversing an array only using two variables.

Posted 25 April 2012 - 06:39 PM

pbl, on 25 April 2012 - 06:04 PM, said:

P.S.
if it is an array of int[] I can even do it without the temp variable (if you want to impress your teacher )

I would be interested in seeing this

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Reversing an array only using two variables.

Posted 25 April 2012 - 06:53 PM

```public void reverse(int[] array) {

for(int i = 0; i < array.length / 2; ++i) {
array[i] ^= array[array.length - 1 - i];
array[array.length - 1 - i] ^= array[i];
array[i] ^= array[array.length - 1 - i];
}
}

```

Old assembler programmers trick. In the early 70's, when the machine, like the CDC Cyber 74, were systems without stack (yes the stack concept was invented later on, it didn't always exist, appears on the Z80 or something like that) performing 3 XOR operations like the ones showed here, was the fastest way to exchange to registers without:
- using another register
- using a temp variable (not allocatable on the stack that didn't exist)
This can be applied to any long, int, short, char, byte variables

Performing it twice
R1 XOR R2
R2 XOR R1
R1 XOR R2
R1 XOR R2
R2 XOR R1
R1 XOR R2
was the easiest way to waste just the nano seconds required to wait for the I/O subsystem on the Z80.

XOR are also used a lot in encryption algorithm

http://www.dreaminco...e-xor-operator/

This post has been edited by pbl: 25 April 2012 - 07:34 PM