4 Replies - 5236 Views - Last Post: 25 April 2012 - 06:53 PM Rate Topic: -----

#1 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • 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;
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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 :))
Was This Post Helpful? 0
  • +
  • -

#4 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Reversing an array only using two variables.

Posted 25 April 2012 - 06:39 PM

View Postpbl, 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 :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1