|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Reverse Array order ?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
Show quote
Hide quote
On Sun, 9 Oct 2005 17:03:47 +0200, "Jürgen Heyn" <j.h***@gmx.de> Sure. Swap 1 and 20, then 2 and 19, then 3 and 18, and so on.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 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
Show quote
Hide quote
On Sun, 9 Oct 2005 17:03:47 +0200, "Jürgen Heyn" <j.h***@gmx.de> PS: If you really need the array reversed, then you do. But you canwrote: >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 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 > I would like to reverse the order of an array. First, are you aware that> 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 ? 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 "Rick Rothstein [MVP - Visual Basic]" wrote: That depends on whether Option Base is used or not...> .... > First, are you aware that > > Dim AnyArrayName(20) > > declares and array with 21 elements numbered 0 to 20? ... > Dim AnyArrayName(1 To 20) Alternatively, one can set Option Base 1 and get the same behavior> > to declare a 20-element array numbered 1 to 20 as you seem to > want. automagically... Also just in case OP isn't aware of it... "Jürgen Heyn" <j.h***@gmx.de> wrote Why? The array is already in order, it is only> Hi, good evening, > > I would like to reverse the order of an array. 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 "Larry Serflaten" <serfla***@usinternet.com> wrote in message Exactly Larry. I couldn't agree more. In fact a fast "index sort" of a 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? 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
.NOT My Views
Visual Basic 6.0 Working Model Convert hex string to a byte Cannot Load Form! How should one form call another? How to handle data types in custom Max/Min function? Simulation! Point me in the right direction - Mouse utility... vbaccelerator - compile ListView for Unicode? Which Form Has Invoked The Sub! |
|||||||||||||||||||||||