0

ok so i am trying to fill a 2d array with random numbers and find the max size in each row. i don't know what is wrong with my code.

here is the code

<html>
<body>
<title>Assign 2</title>
</head>
</body>
<hr/><p/>
<?php

$arr=array();
$max_row=array();

function find_max_row()
{       global $max_row;
        global $arr;
        for($i=0;$i<10;$i++)
        {       $min=0;
                for($d=1;$d<10;$d++)
                {       $arr[$i][$d]=rand(1,100);
                }
  }
}

function find_max_row_again()
{       global $max_row;
        global $arr;
        for($i=0;$i<10;$i++)
        {       $min=0;
                for($d=1;$d<10;$d++)
                {       if($arr[$i][$min]<$arr[$i][$d])
                        {       $min=$d;
                        }
                }
        $max_row[$i]=$arr[$i][$min];
        }
}
function fill_array()
{       global $arr;
for($i=0;$i<10;$i++)
        {       $arr[$i]=array();
                for($d=1;$d<10;$d++)
                {       $arr[$i][$d]=rand(1,100);
                }
        }
}
fill_array();
echo "<table cellpadding=\"4\" border=\"1\" width=\85%\" align\"center\">";
for($i=0;$i<10;$i++)
{       echo "<tr>";
        for($d=0;$d<10;$d++)
        {       echo $arr[$i][$d];
        }
}
echo "</table><p/><p/><p/>\n";
find_max_row_again();
echo "<table cellpadding=\"4\" border=\"1\" align=\"center\">";
for($i=0;$i<10;$i++)
{       echo "<tr>";
        for($d=0;$d<10;$d++)
        {       echo $arr[$i][$d];
        }
}
echo "</table><p/><p/><p/>\n";
find_max_row_again();
echo "<table cellpadding=\"4\" border=\"1\" align=\"center\">";
for($i=o;$i<10;$i++)
{       echo $max_row[$i];
}
echo "</table><p/>\n";
?>
</body>
</html>

please help

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by ROTC89
0

Well there's a whole lot wrong. A) You didn't use code tags, B) You're using global instead of passing arguments to the functions, C) You're duplicating a function for no reason, D) You're not taking advantage of built-in language functions http://www.php.net/arrays

0

alright..i put the code tags and the table appeared with the random numbers. but the box that is suppose to show the max numbers does not appear. how else would i get the program to work without using the the function again the second time?

0

alright..i put the code tags and the table appeared with the random numbers. but the box that is suppose to show the max numbers does not appear. how else would i get the program to work without using the the function again the second time?

I meant use code tags in your post, not in your code. Take a gander at the PHP manual and come back once you have a grasp on how functions work. http://www.php.net/functions

0

if you have random number and want to get maximum or minimum value then you can use the SORT function, it will sort out your values ascending or descending and you can take the first or last value as your minimum or maximum value

0

ok so i fixed up my program and took out the second find_max. the only problem now is when i go to print out the max numbers in a second column there is no value in the box. apparently there is no number in the array. i dont know why. here is the code

<html>
<body>
<?php

$arr[] = array();
//$max_row = array();

function find_max_row($arr)//, $max_row)
{
        $max_row;
        echo '<br /><table border = "1">';
        for($i=0;$i<10;$i++)
        {       $min= $arr[i][d];
                for($d=1;$d<10;$d++)
                {       if($min > $arr[$i][$d])
                        {
                                $min=$arr[$i][$d];
                        }

                        //echo $min."<br>";
						   //$max_row[$i]=$arr[$i][$min];

                }
                        echo '<tr><td>'.'value'.$min.'</td></tr>';
                        $max_row[$i]=$min;

        }
        echo '</table><br />';
        return $max_row;
}
function fill_array()
{       global $arr;
        for($i=0;$i<10;$i++)
        {       $arr[$i]=array();
                for($d=1;$d<10;$d++)
                {       $arr[$i][$d]=rand(1,100);
                }
        }
}
function print_array($max_row)
{       for($i=0;$i<10;$i++)
        {       echo "<tr><td align=\"center\">";
                echo $max_row[$i];
        }       echo "</td></tr>\n";
}

fill_array();
echo "Here is the table".'<br />';
echo "<table cellpadding=\"2\" border=\"1\" width=\75%\" align\"center\">";
for($i=0;$i<10;$i++)
{       echo "<tr>";
        for($d=0;$d<10;$d++)
        {       echo "<td align=\"center\">";
                echo $arr[$i][$d];
                echo "</td>";
        }
}
echo "</table><p/><p/><p/>\n";
echo "Call find_max"."<br />";
$max_row = find_max_row($arr);
echo "<table cellpadding=\"2\" border=\"1\" align=\"center\">";
 for($i=0;$i<10;$i++)
        {       echo "<tr><td align=\"center\">";
                echo $max_row[$i];
        }       echo "</td></tr>\n";


echo "</table><p/>\n";
//print_array($max_row);
// for($i=0;$i<10;$i++)
//       {       echo "<tr><td align=\"center\">";
//                echo $max_row[$i];
//        }       echo "</td></tr>\n";

?>
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.