Jump to content

Quick Array Query Question


Recommended Posts

This is my first post so please bear with me.

I have created the array $myarray[10][10]

The array is filled with either 1's or 0's:

0100000100

0001000000

0000000100

0001000000

0000000000

0010000110

0000000000

0000010000

0010000000

0000001000

So say this is a seating chart (well, it is) and I have 8 people being hired. I need to find eight 0's in a 2 by 4 block that can be assigned 1's (designating those seats as being in use). I'm struggling on the syntax of querying the array for a 2 by 4 block of 0's and once they are found flipping those bits to 1's. Due to sporatic seating at the moment the block can be anywhere within the array as long as the group is together.

Any ideas would be appreciated,

Scooby

Link to comment
Share on other sites

Try

$width = 2
$height = 4
for $try = 0 to 1
  For $col = 0 to 8
    For $row = 0 to 5
      If Check($myarray, $col, $row, $width, $height) == 1 Then
       ; bingo - found a two by four
        MsgBox(0, "Success", $width & "x" & $height & " starting at column " & $col & " row " & $row)
        Set($myarray, $col, $row, $width, $height)
        ExitLoop 3
      EndIf
    Next
  Next
 ; failed with a 2x4, try a 4x2
  $width = 4
  $height = 2
Next

Func Check(ByRef $array, $cstart, $rstart, $width, $height)
  Local $i, $j
  For $j = $rstart to $rstart + $width - 1
    For $i = $cstart to $cstart + $height - 1
      If $array[$j][$i] == 1 Then
       ; someone there already, can't use this block
        return 0
      EndIf
    Next
  Next
 ; no-one there at all, can use it
  return 1
EndFunc

Func Set(ByRef $array, $cstart, $rstart, $width, $height)
  Local $i, $j
  For $j = $rstart to $rstart + $width - 1
    For $i = $cstart to $cstart + $height - 1
      $array[$j][$i] = 1
    Next
  Next
EndFunc

GrahamS

Link to comment
Share on other sites

Well, I'm glad to report that with GrahamS's help I have fianlly gotten this to work. I did however have to swap $width and $height on lines 21, 22 and 35, 36. Thank you soo much!!

One step closer to novice,

Scooby

Edited by Scooby
Link to comment
Share on other sites

Well, I'm glad to report that with GrahamS's help I have fianlly gotten this to work.  I did however have to swap $width and $height on lines 21, 22 and 35, 36. Thank you soo much!! 

One step closer to novice,

Scooby

Probably should have mentioned that, it depends if you think the rows come before the columns or vice versa. Obviously we had them the other way round (there is no right or wrong way here, just need consistency)

Glad to help

GrahamS

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...