?
Solved

array algorithm

Posted on 2006-04-14
11
Medium Priority
?
207 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:RyanBank
  • 5
  • 4
  • 2
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16452536
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
 

Author Comment

by:RyanBank
ID: 16452608
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
 

Author Comment

by:RyanBank
ID: 16452644
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16452675
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
 

Author Comment

by:RyanBank
ID: 16452687
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
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 1000 total points
ID: 16452729
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
 

Author Comment

by:RyanBank
ID: 16452819
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 16452825

    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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16452827
wanted to say that there is only a minor correction to ArthurWoods code
0
 

Author Comment

by:RyanBank
ID: 16452833
thanks so much angelIII and ArthurWoods!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16452972
thanks angelIII.  Thought I had fully tested the code, but oh well.

Glad to be of assistance.

AW
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question