We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Checking for a series of consecutive values in an array

Medium Priority
616 Views
Last Modified: 2012-05-06
Hello,

I have an array that 38 elements in it - Array(38).  The value can be either 1 or 0.  For array elements 1 through 31 in the array, I have to determine if the next consecutive 6, 3, or 2 elements are also valued at 1.  Once the array element gets past 31, it does not need to check elements 32-38.

I.e.  IF Array(1)=1, do Array(2), Array(3), Array(4), Array(5), Array(6), Array(7) all = 1 ?
If not, IF Array(1) = 1 do Array(2), Array(3), Array(4) all = 1 ?
If not, if Array(1) = 1 do Array(2), Array(3) = 1 ?

If 7 consecutive elements are true, then the 4 and 3 check do not need to be done.  However, if 7 consecutive is not true, then I need to check for consecutive 4, then consecutive 3.

I can do this with a series of FOR .. NEXT loops but it is not very efficient because all
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
IF Array(1)=1 count from i=1 until either i=6 or Array(1+i) = 0
then check if i = 6 or i>=3 or i >= 2

Author

Commented:
The problem with checking i >= 3 or i>2  is that it could be non-consecutive elements.

Array(1) = 1
Array(2) = 1
Array(3) = 0
Array(4) = 0
Array(5) = 1
Array(6) = 1
Array(7) = 1

If i >=3 or i >=2 is true in this scenario, but I need to find out for consecutive elements
Array(1) = 1
Array(2) = 1
Array(3) = 1
Array(4) = 1
Array(5) = 0
Array(6) = 0

The above would be true for the 3 consecutive and 4 consecutive but false for the 7 consecutive.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
count from i=1 until either i=6 or Array(1+i) = 0
so if
Array(1) = 1
Array(2) = 1
Array(3) = 0
Array(4) = 0
Array(5) = 1
Array(6) = 1
Array(7) = 1
you would stop at Array(3) = 0

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
You can break it down into two phases: counting and display. That allows you to loop backwards, from 38 to 1, which is more efficient. See the sample VBA code below.

(°v°)
    Dim A(1 To 38) As Integer
    Dim i As Integer
    Dim n As Integer
    
    ' fill array ( p(1) = 80% )
    For i = 1 To 38
        A(i) = IIf(Rnd() < 0.2, 0, 1)
    Next i
    
    ' count runs
    For i = 38 To 1 Step -1
        If A(i) Then n = n + 1: A(i) = n Else n = 0
    Next i
    
    ' display
    For i = 1 To 38
        Debug.Print "A(" & i & ") = " & A(i) & ": ";
        Select Case A(i)
        Case Is >= 7:       Debug.Print "run+6"
        Case Is >= 4:       Debug.Print "run+3"
        Case Is >= 3:       Debug.Print "run+2"
        Case Else:          Debug.Print
        End Select
    Next i

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.