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
RyanBankAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
RyanBankAuthor Commented:
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, ",")
0
RyanBankAuthor Commented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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)
0
RyanBankAuthor Commented:
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
0
Arthur_WoodCommented:
try this:

    Dim arr_req() As Integer
    ReDim arr_req(4)
    Dim i As Integer
    arr_req(0) = 1
    arr_req(1) = 2
    arr_req(2) = 3
    arr_req(3) = 4
   
    ReDim Preserve arr_req(LBound(arr_req) To (2 * (UBound(arr_req)) - 1))
    For i = UBound(arr_req) To LBound(arr_req) Step -1
        arr_req(i) = arr_req(i \ 2)  ' uses INTEGER divide (\ not /)
    Next

AW
0
RyanBankAuthor Commented:
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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:

    Dim arr_req() As Integer
    ReDim arr_req(4)
    Dim i As Integer
    arr_req(0) = 1
    arr_req(1) = 2
    arr_req(2) = 3
    arr_req(3) = 4
   
    ReDim Preserve arr_req(LBound(arr_req) To (2 * (UBound(arr_req)+1) - 1))
    For i = UBound(arr_req) To LBound(arr_req) Step -1
        arr_req(i) = arr_req(i \ 2)  ' uses INTEGER divide (\ not /)
    Next

AW
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
wanted to say that there is only a minor correction to ArthurWoods code
0
RyanBankAuthor Commented:
thanks so much angelIII and ArthurWoods!
0
Arthur_WoodCommented:
thanks angelIII.  Thought I had fully tested the code, but oh well.

Glad to be of assistance.

AW
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.