Home All Groups Group Topic Archive Search About
Author
9 Oct 2005 3:03 PM
Jürgen Heyn
Hi, good evening,

I would like to reverse the order of an array.
Dim Array1(20)
Dim Array2(20)

Dim i as Integer
Dim j as Integer
j = 1
For i = 20 to 1 Step -1
   Array2(j) = Array1(i)
   j = j + 1
Next i

Is there a soloution which does not require the use of Array2 ?
Thank you for any hint.
Best regards.
Juergen Heyn, Wilhelmshaven, Germany

Author
9 Oct 2005 3:13 PM
LurfysMa
Show quote Hide quote
On Sun, 9 Oct 2005 17:03:47 +0200, "Jürgen Heyn" <j.h***@gmx.de>
wrote:

>Hi, good evening,
>
>I would like to reverse the order of an array.
>Dim Array1(20)
>Dim Array2(20)
>
>Dim i as Integer
>Dim j as Integer
>j = 1
>For i = 20 to 1 Step -1
>   Array2(j) = Array1(i)
>   j = j + 1
>Next i
>
>Is there a soloution which does not require the use of Array2 ?
>Thank you for any hint.
>Best regards.
>Juergen Heyn, Wilhelmshaven, Germany

Sure. Swap 1 and 20, then 2 and 19, then 3 and 18, and so on.

If you know it's 20 elements, iterate from 1 to 10. You will need to
calculate the 20-11 indices and you will need one temp variable.

In the general case, you would iterate from 1 to Fix(UBound(Array)).
This will bypass the middle element in an array with an odd numbers of
elements.

--
For email, use Usenet-20031220@spamex.com
Author
9 Oct 2005 3:27 PM
LurfysMa
Show quote Hide quote
On Sun, 9 Oct 2005 17:03:47 +0200, "Jürgen Heyn" <j.h***@gmx.de>
wrote:

>Hi, good evening,
>
>I would like to reverse the order of an array.
>Dim Array1(20)
>Dim Array2(20)
>
>Dim i as Integer
>Dim j as Integer
>j = 1
>For i = 20 to 1 Step -1
>   Array2(j) = Array1(i)
>   j = j + 1
>Next i
>
>Is there a soloution which does not require the use of Array2 ?
>Thank you for any hint.
>Best regards.
>Juergen Heyn, Wilhelmshaven, Germany


PS: If you really need the array reversed, then you do. But you can
index an array in reverse just as easily as forward:

For i = UBound(Array) to LBound(Array) Step -1
   ...whatever processing on Array(i)...
Next i





PPS: The code to reverse your array is:

j=UBound(Array)
For i=1 to Fix(UBound(Array)/2)
   temp=Array(i)
   Array(i)=Array(j)
   Array(j)=temp
Next i

You are trading execution time for storage space.

Using 2 arrays, you have double the storage (2 arrays).

Using the swap method, you have (roughly) 1.5 times the number of
moves (3 moves for half the number of elements).


--
For email, use Usenet-20031220@spamex.com
Author
9 Oct 2005 3:30 PM
Rick Rothstein [MVP - Visual Basic]
> I would like to reverse the order of an array.
> Dim Array1(20)
> Dim Array2(20)
>
> Dim i as Integer
> Dim j as Integer
> j = 1
> For i = 20 to 1 Step -1
>    Array2(j) = Array1(i)
>    j = j + 1
> Next i
>
> Is there a soloution which does not require the use of Array2 ?

First, are you aware that

     Dim AnyArrayName(20)

declares and array with 21 elements numbered 0 to 20? I see in
your loop that you are completely ignoring the zeroeth element of
both arrays. The Dim statement in VB6 (and earlier versions)
allows you to do this

     Dim AnyArrayName(1 To 20)

to declare a 20-element array numbered 1 to 20 as you seem to
want.

Second, you don't need to create the "j" variable as you can do
what you have done above without it. Also, you can iterate your
loop in a forward direction rather than backwards as you have
done. The following code combines all of these ideas...

     Dim i As Integer
     Dim Array1(1 To 20)
     Dim Array2(1 To 20)

     For i = 1 To 20
       Array2(i) = Array1(21 - i)
     Next

Now, to answer your question. There are probably a few ways to do
what you have asked. The first one that comes to mind is this...

     Dim i As Integer
     Dim Temp As Integer
     Dim YourArray(1 To 20)

     For i = 1 To 10
       Temp = YourArray(i)
       YourArray(i) = YourArray(21 - i)
       YourArray(21 - i) = Temp
     Next

Rick
Author
9 Oct 2005 8:22 PM
Duane Bozarth
"Rick Rothstein [MVP - Visual Basic]" wrote:
>
....
> First, are you aware that
>
>      Dim AnyArrayName(20)
>
> declares and array with 21 elements numbered 0 to 20? ...

That depends on whether Option Base is used or not...

>      Dim AnyArrayName(1 To 20)
>
> to declare a 20-element array numbered 1 to 20 as you seem to
> want.

Alternatively, one can set Option Base 1 and get the same behavior
automagically...

Also just in case OP isn't aware of it...
Author
9 Oct 2005 4:36 PM
Larry Serflaten
"Jürgen Heyn" <j.h***@gmx.de> wrote
> Hi, good evening,
>
> I would like to reverse the order of an array.

Why?  The array is already in order, it is only
your perspective that decides 0 is the first element,
and 20 is the last.  Surely the memory doesn't care.
If you need to see the 20th element first, the traverse
through the array in reverse order.  Wouldn't that save
a bit of wasted manipulation?

Just a suggestion.....

LFS
Author
9 Oct 2005 5:52 PM
Mike Williams
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:eNNjR7OzFHA.460@TK2MSFTNGP15.phx.gbl...

> Why?  The array is already in order, it is only
> your perspective that decides 0 is the first element,
> and 20 is the last.  Surely the memory doesn't care.
> If you need to see the 20th element first, the traverse
> through the array in reverse order.  Wouldn't that save
> a bit of wasted manipulation?

Exactly Larry. I couldn't agree more. In fact a fast "index sort" of a
typical array (perhaps containing variable length strings or whatever)
doesn't actually alter the order of the strings in the array (as you
obviously already know). It leaves them exactly where they are and it merely
"tells you" in what order you should display them for them to appear
"sorted". In the OP's case (as you have pointed out) he is already in
possession of that information (he wants them to be displayed backwards) so
there is nothing really that he has to do.

Mike