• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

Checking for a series of consecutive values in an array

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
0
aspireworks
Asked:
aspireworks
  • 2
2 Solutions
 
ozoCommented:
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
0
 
aspireworksAuthor 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.
0
 
ozoCommented:
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
0
 
harfangCommented:
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

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now