Jump to content


Photo

2D Array/Matrix


  • Please log in to reply
10 replies to this topic

#1 dougal85

dougal85
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 April 2006 - 12:53 AM

[!--fonto:Verdana--][span style=\"font-family:Verdana\"][!--/fonto--]
Hi,

First post! :)

I am having a problem with my PHP.

I am trying to make a 8x8 grid, with boolean values that i can then check and count how many true values are in each column, row and diagonal.

At the moment i have managed to get the grid/matrix successfully and also i have managed to check the rows and columns. So thats all cool.

However i am having trouble working out the best way to check the diagonals.

I have noticed that there is a bit of a patern with them, for example some of them as you follow it through either the x or the y value is going down and the other is going up.

I just dont know how best to put this into code.

Any suggestions would be greatly appreciated!

Thanks
Dougal[!--fontc--][/span][!--/fontc--]

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 26 April 2006 - 01:27 AM

to traverse a 2d array(must be a i x i array)
<?php

$array = array(array(1,0,1,0),array(1,0,0,0),array(0,0,1,1),array(1,0,1,1);

$arrsize = count($array[0]);

$tlbrcount = NULL; var to store number of true values in tl - br diag
$bltrcount = NULL; var to store number of true values in bl - tr diag

for ($i = 0; $i < $arrsize; $i++) {

//tl - br loop
 $tlbrcont = $array[$i][$i] == true ?  $tlbrcont + 1 : $tlbrcont;

//bl - tr loop
 $bltrcont = $array[$i][($arrsize - 1) - $i] == true ?  $bltrcont + 1 : $bltrcont;

}

?>

There you go - took me 5 minutes to post that!!!!!!!! should start charging.
follow me on twitter @PHPsycho

#3 dougal85

dougal85
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 April 2006 - 01:33 AM

Cool! thats really good! thanks

However, am i right in thinking thats the diagonals from the opposite corners.

I also need to check the other diagonals in the array - they are whats getting me stuck the most!

Any further suggestions?

thats a cool start! thanks

Dougal

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 26 April 2006 - 02:04 AM

so ALL diagonlas?

-ie any diagonal starting from an element on the outside of the matrix?

from top left to bottom right? or in all directions?


follow me on twitter @PHPsycho

#5 dougal85

dougal85
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 April 2006 - 10:21 AM

[!--quoteo(post=368698:date=Apr 26 2006, 03:04 AM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 26 2006, 03:04 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
so ALL diagonlas?

-ie any diagonal starting from an element on the outside of the matrix?

from top left to bottom right? or in all directions?
[/quote]

all directions.

basically, items need to be placed into the matrix... however no more than two can be in any diagonal line same with horizontal and vertical.

ie. this example has a diagonal with 3

0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

*EDIT* i realise that example is quite hard to see, but you should get the idea :)

#6 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 26 April 2006 - 10:27 AM

Welcome to PHP freaks Dougal.
From Neil The Runner
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#7 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 26 April 2006 - 11:22 AM

Hello,

I am not sure if i understand it correctly. still no harm in trying....

to check for any diagonal elements, you will have to navigate thru each element of the array and check diagonal elements for each.

example : suppose the current array element u r checking is


arr( x , y )

the possible digonal elements are

arr ( x-1, y-1 )
arr ( x+1, y-1 )
arr ( x-1, y+1 )
arr ( x+1, y+1 )

out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals

hth

sorry if i confused u more :) was just trying to help :)

Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#8 dougal85

dougal85
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 April 2006 - 12:14 PM

[!--quoteo(post=368792:date=Apr 26 2006, 12:22 PM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ Apr 26 2006, 12:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello,

I am not sure if i understand it correctly. still no harm in trying....

to check for any diagonal elements, you will have to navigate thru each element of the array and check diagonal elements for each.

example : suppose the current array element u r checking is
arr( x , y )

the possible digonal elements are

arr ( x-1, y-1 )
arr ( x+1, y-1 )
arr ( x-1, y+1 )
arr ( x+1, y+1 )

out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals

hth

sorry if i confused u more :) was just trying to help :)
[/quote]

omg, thats actually a really good idea. I think i can see how that would works, since i know the upper and lower limits i think i should be able to get some sort of loop to go through all that.

The only thing is it will probably end up checking all the diagonals multiple times but i could put it in my documentation that its doing that just to be sure! hehe :)

hmm, guess i could try and work it out to see what the minimum number of elements i would need to check for it.

Thanks! I'm gonna give this a shot tonight.

#9 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 26 April 2006 - 12:19 PM

Hello,

as i said,

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals
[/quote]

u need not check arr ( x-1, y-1 ) and arr ( x+1, y-1 ) , so u will not check repeatedly....:) ...give it a shot, i think it should work.

hth
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#10 dougal85

dougal85
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 April 2006 - 02:24 PM

[!--quoteo(post=368807:date=Apr 26 2006, 01:19 PM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ Apr 26 2006, 01:19 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello,

as i said,
u need not check arr ( x-1, y-1 ) and arr ( x+1, y-1 ) , so u will not check repeatedly....:) ...give it a shot, i think it should work.

hth
[/quote]


sorry i didnt see that!

Thanks, i'm just going to give it a shot now - i'll let you know how i get on.

#11 dougal85

dougal85
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 April 2006 - 02:58 PM

also realised that;

(x-1, y+1) only needs start from the elements on the left and top
(x+1, y+1) only needs to start on the elements on the left and bottom.

just coding it now, fingers crossed




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users