Link to home
Start Free TrialLog in
Avatar of RyanBank
RyanBank

asked on

array algorithm

Hi,

Please kindly assist how can we do something like this:

arr_req=1,2,3,4

desired output:
arr_req=1,1,2,2,3,3,4,4

Thanks.

    For i = 0 To UBound(Arr_Req)
        ReDim Preserve Arr_Req(UBound(Arr_Req) + 1)
        For x = UBound(Arr_Req) To (i + 1) Step -1
            Arr_Req(x) = Arr_Req(x - 1)
        Next x
       
        If i = 0 Then
            Arr_Req(i + 1) = Arr_Req(i)
        ElseIf i Mod 2 = 0 Then
            Arr_Req(i + 1) = Arr_Req(i + 2)
        End If
        Debug.Print Join(Arr_Req, ",")
    Next i
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

redim preserve arr_req ( lbound(arr_req) to (2*(ubound(arr_req)+1)  - 1  ) as x

for i = ubound ( arr_req ) to lbound(arr_req) + 1 step -1
  arr_req ( i ) = arr_req ( i / 2  - i % 2 )
next
Avatar of RyanBank
RyanBank

ASKER

Error returned: user undefined: pointing to line 1

    ReDim Preserve Arr_Req(LBound(Arr_Req) To (2 * (UBound(Arr_Req) + 1) - 1)) As x   <line 1
    For i = UBound(Arr_Req) To LBound(Arr_Req) + 1 Step -1
        Arr_Req(i) = Arr_Req(i / 2 - i Mod 2)
    Next
    Debug.Print Join(Arr_Req, ",")
by the way, does the code also works on other values?

i.e.
arr_req=41,22,33,54,1,2,3,7,8

desired output:
arr_req=41,41,22,22,33,33,54,54,1,1,2,2,3,3,7,7,8,8
replace the as x obviously with as integer or as string.

>by the way, does the code also works on other values?
there is no limitation in the data, only the array size is limited (to 64 K)
Glad to know that it is unlimited, not sure what I have missed the result out put came out:

1,1,2,1,3,3,4,3


    Debug.Print "--------------original----------"
    Debug.Print Join(Arr_Req, ",")
   
    ReDim Preserve Arr_Req(LBound(Arr_Req) To (2 * (UBound(Arr_Req) + 1) - 1)) As String
    For i = UBound(Arr_Req) To LBound(Arr_Req) + 1 Step -1
        Arr_Req(i) = Arr_Req(i / 2 - i Mod 2)
        Debug.Print Join(Arr_Req, ",")
    Next
   
   
    Debug.Print "--------------new--------------"
    Debug.Print UBound(Arr_Req)
    Debug.Print Join(Arr_Req, ",")
    Exit Sub


--------------original----------
1,2,3,4
1,2,3,4,,,,3
1,2,3,4,,,4,3
1,2,3,4,,3,4,3
1,2,3,4,3,3,4,3
1,2,3,1,3,3,4,3
1,2,2,1,3,3,4,3
1,1,2,1,3,3,4,3
--------------new--------------
1,1,2,1,3,3,4,3
SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
were almost there, sad to say, it missed the last array element "4"
result returned:

--------------original----------
1,2,3,4
1,2,3,4,,3
1,2,3,4,3,3
1,2,3,2,3,3
1,2,2,2,3,3
1,1,2,2,3,3
1,1,2,2,3,3
--------------new--------------
1,1,2,2,3,3
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wanted to say that there is only a minor correction to ArthurWoods code
thanks so much angelIII and ArthurWoods!
thanks angelIII.  Thought I had fully tested the code, but oh well.

Glad to be of assistance.

AW