php table with random numbers and finding max

Thread Solved

Join Date: Oct 2008
Posts: 35
Reputation: ROTC89 is an unknown quantity at this point 
Solved Threads: 0
ROTC89 ROTC89 is offline Offline
Light Poster

php table with random numbers and finding max

 
0
  #1
Jun 14th, 2009
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
  1. <html>
  2. <body>
  3. <title>Assign 2</title>
  4. </head>
  5. </body>
  6. <hr/><p/>
  7. <?php
  8.  
  9. $arr=array();
  10. $max_row=array();
  11.  
  12. function find_max_row()
  13. { global $max_row;
  14. global $arr;
  15. for($i=0;$i<10;$i++)
  16. { $min=0;
  17. for($d=1;$d<10;$d++)
  18. { $arr[$i][$d]=rand(1,100);
  19. }
  20. }
  21. }
  22.  
  23. function find_max_row_again()
  24. { global $max_row;
  25. global $arr;
  26. for($i=0;$i<10;$i++)
  27. { $min=0;
  28. for($d=1;$d<10;$d++)
  29. { if($arr[$i][$min]<$arr[$i][$d])
  30. { $min=$d;
  31. }
  32. }
  33. $max_row[$i]=$arr[$i][$min];
  34. }
  35. }
  36. function fill_array()
  37. { global $arr;
  38. for($i=0;$i<10;$i++)
  39. { $arr[$i]=array();
  40. for($d=1;$d<10;$d++)
  41. { $arr[$i][$d]=rand(1,100);
  42. }
  43. }
  44. }
  45. fill_array();
  46. echo "<table cellpadding=\"4\" border=\"1\" width=\85%\" align\"center\">";
  47. for($i=0;$i<10;$i++)
  48. { echo "<tr>";
  49. for($d=0;$d<10;$d++)
  50. { echo $arr[$i][$d];
  51. }
  52. }
  53. echo "</table><p/><p/><p/>\n";
  54. find_max_row_again();
  55. echo "<table cellpadding=\"4\" border=\"1\" align=\"center\">";
  56. for($i=0;$i<10;$i++)
  57. { echo "<tr>";
  58. for($d=0;$d<10;$d++)
  59. { echo $arr[$i][$d];
  60. }
  61. }
  62. echo "</table><p/><p/><p/>\n";
  63. find_max_row_again();
  64. echo "<table cellpadding=\"4\" border=\"1\" align=\"center\">";
  65. for($i=o;$i<10;$i++)
  66. { echo $max_row[$i];
  67. }
  68. echo "</table><p/>\n";
  69. ?>
  70. </body>
  71. </html>
please help
Last edited by Tekmaven; Jun 15th, 2009 at 1:43 am. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,431
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 233
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: php table with random numbers and finding max

 
0
  #2
Jun 14th, 2009
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
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: ROTC89 is an unknown quantity at this point 
Solved Threads: 0
ROTC89 ROTC89 is offline Offline
Light Poster

Re: php table with random numbers and finding max

 
0
  #3
Jun 14th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,431
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 233
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: php table with random numbers and finding max

 
0
  #4
Jun 14th, 2009
Originally Posted by ROTC89 View Post
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
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 257
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: php table with random numbers and finding max

 
0
  #5
Jun 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: ROTC89 is an unknown quantity at this point 
Solved Threads: 0
ROTC89 ROTC89 is offline Offline
Light Poster

Re: php table with random numbers and finding max

 
0
  #6
Jun 16th, 2009
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

  1. <html>
  2. <body>
  3. <?php
  4.  
  5. $arr[] = array();
  6. //$max_row = array();
  7.  
  8. function find_max_row($arr)//, $max_row)
  9. {
  10. $max_row;
  11. echo '<br /><table border = "1">';
  12. for($i=0;$i<10;$i++)
  13. { $min= $arr[i][d];
  14. for($d=1;$d<10;$d++)
  15. { if($min > $arr[$i][$d])
  16. {
  17. $min=$arr[$i][$d];
  18. }
  19.  
  20. //echo $min."<br>";
  21. //$max_row[$i]=$arr[$i][$min];
  22.  
  23. }
  24. echo '<tr><td>'.'value'.$min.'</td></tr>';
  25. $max_row[$i]=$min;
  26.  
  27. }
  28. echo '</table><br />';
  29. return $max_row;
  30. }
  31. function fill_array()
  32. { global $arr;
  33. for($i=0;$i<10;$i++)
  34. { $arr[$i]=array();
  35. for($d=1;$d<10;$d++)
  36. { $arr[$i][$d]=rand(1,100);
  37. }
  38. }
  39. }
  40. function print_array($max_row)
  41. { for($i=0;$i<10;$i++)
  42. { echo "<tr><td align=\"center\">";
  43. echo $max_row[$i];
  44. } echo "</td></tr>\n";
  45. }
  46.  
  47. fill_array();
  48. echo "Here is the table".'<br />';
  49. echo "<table cellpadding=\"2\" border=\"1\" width=\75%\" align\"center\">";
  50. for($i=0;$i<10;$i++)
  51. { echo "<tr>";
  52. for($d=0;$d<10;$d++)
  53. { echo "<td align=\"center\">";
  54. echo $arr[$i][$d];
  55. echo "</td>";
  56. }
  57. }
  58. echo "</table><p/><p/><p/>\n";
  59. echo "Call find_max"."<br />";
  60. $max_row = find_max_row($arr);
  61. echo "<table cellpadding=\"2\" border=\"1\" align=\"center\">";
  62. for($i=0;$i<10;$i++)
  63. { echo "<tr><td align=\"center\">";
  64. echo $max_row[$i];
  65. } echo "</td></tr>\n";
  66.  
  67.  
  68. echo "</table><p/>\n";
  69. //print_array($max_row);
  70. // for($i=0;$i<10;$i++)
  71. // { echo "<tr><td align=\"center\">";
  72. // echo $max_row[$i];
  73. // } echo "</td></tr>\n";
  74.  
  75. ?>
  76. </body>
  77. </html>
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: ROTC89 is an unknown quantity at this point 
Solved Threads: 0
ROTC89 ROTC89 is offline Offline
Light Poster

Re: php table with random numbers and finding max

 
0
  #7
Jun 16th, 2009
i figured it out. thanks for all the help you guys
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 538 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC