Some looping logic faults..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Some looping logic faults..

Post by Charles256 »

First off: I can't quite figure out how I want to do this.. I can tell you what I am trying to do. First allow me to show you the code..

Code: Select all

<?php      
$matrix=array();
$matrix2=array();
function check_col($rows,$x)
{
    for ($i=0;$i<(count($rows)-1);$i++)
        {
            $o="".($i+1)."";
            $currentcols=explode(" ",$rows[$i]);
            $nextcols=explode(" ",$rows[$o]);
            if(count($currentcols)!=count($nextcols))
            {
                echo "The number of columns must be the same in every row. Try again.
                The error lies on row $o and ".($o+1)." in matrix $x. <a href='test.php'>Try again?</a>";
                die();
            }
            else
            {
                echo "The number of columns is the same in the following two rows in matrix $x.                Rows $o and ".($o+1).". <br />";
            }
            if ($i==(count($rows)-2))
            {
                echo "Second test completed on matrix $x. There are an equal number of columns in every row. <Br />";
            }
        }
}
function check_int($rows,$x)
{
    for ($i=0;$i<count($rows);$i++)
        {
            $colentries=explode(" ",$rows[$i]);
            for ($o=0;$o<count($colentries);$o++)
            {
                if ($x==1)
                {
                     global $matrix;
                     $matrix[]=$colentries[$o];
                }
                else if ($x==2)
                {
                     global $matrix2;
                     $matrix2[]=$colentries[$o];
                }
                    
                if (!(is_numeric($colentries[$o])))
                {
                    
                    echo "All entries must be integers. More than likely you have a trailin space which is causing this error. Hit the back button on your browser and edit your entries.";
                    die();
                }
                    
            }
        }
         echo " Third test passed. All entries in matrix $x are numeric.<br />";   
}
if (isset($_GET['process']))
{
    if ($_GET['process']=="yes")
    {
        $mat1=$_POST['mat1'];
        $mat2=$_POST['mat2'];
        $rows1=explode(";",$mat1);
        $rows2=explode(";",$mat2);
        $columns1=explode(" ",$rows1['0']);
        $columns2=explode(" ",$rows2['0']);
        if (count($rows2)!=count($columns1))
        {
            echo " The number of rows in matrix 2 must be equal to the number of columns in matrix 1. <a href='test.php'>Try again?</a>";
            die();
        }
        else
        {
            echo "First test passed. The number of rows in matrix two does equal the number of columns in matrix one.<br />";
        }
        check_col($rows1,1);
        check_col($rows2,2);
        check_int($rows1,1);
        check_int($rows2,2);
        echo "The resulting matrix will be a ".count($rows1)." by ".count($columns2)." matrix. <br /> Here is the resulting matrix :<br />";
        
        global $matrix,$matrix2;
        var_dump($matrix);
        echo "<br />";
        var_dump($matrix2);
        for ($i=0;$i<count($rows1);$i++)
		{
		  $elements=count($columns1);
		  for ($o=0; $o <$elements;$o++)
		  {
		    if ($i==0)
		    {
		   		echo $matrix[$o];
				echo "first two elements<Br />"; 
		   		$anotherrow=0;
			}
			else{
			  $anotherrow+= $o + ($elements-1);
			  echo $anotherrow;
			  echo "other elements<Br />";
			  echo $matrix[$anotherrow];
			}
		   }
			echo "<br />";
		}        
                  
        
    }
}
else
{
 echo " Seperate rows with a ; and columns are seperated using spaces. Pretty intuitive,ain't it? Please remember to not put a space between rows. I.E. '2 4 6 7;2 4 6 8' is correct. However, '2 3 4 5; 3 2 3 3' is not correct.
        <form action='test.php?process=yes' method='POST'>
        Matrix 1:<input type='text' length='70' name='mat1'></input><br />
        Matrix 2:<input type='text' length='70' name='mat2'></input><br />
        <input type='submit'></input>
        </form>";
}
      
?>
okay...the output

Code: Select all

First test passed. The number of rows in matrix two does equal the number of columns in matrix one.
The number of columns is the same in the following two rows in matrix 1. Rows 1 and 2.
Second test completed on matrix 1. There are an equal number of columns in every row.
The number of columns is the same in the following two rows in matrix 2. Rows 1 and 2.
Second test completed on matrix 2. There are an equal number of columns in every row.
Third test passed. All entries in matrix 1 are numeric.
Third test passed. All entries in matrix 2 are numeric.
The resulting matrix will be a 2 by 2 matrix.
Here is the resulting matrix :
array(4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" }
array(4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" } 1first two elements
2first two elements

1other elements
23other elements
4
either way, in my paticular example i'm trying to multiple 1 and 2 by 1 and 3 as well as 2 and 4 from the second matrix. that would constitue the first row of the resulting matrix. i.e. 7 10 would be the first row's answer. the second row's answer would be 3 and 4 multiplied by 1 and 3 along with 2 and 4 which results in 15 22.
so as you see answers are gotten by multiply the first row and the first column and adding all the resulting answers to get the first entry in the first row, repeat this for the nth entry in the first row. then move down to the second row of the first matrix and repeat... damn I hope someone can help..
Post Reply