| | |
php table with random numbers and finding max
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 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
please help
here is the code
php Syntax (Toggle Plain Text)
<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>
Last edited by Tekmaven; Jun 15th, 2009 at 1:43 am. Reason: Code Tags
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.
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.
•
•
•
•
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?
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.
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.
•
•
Join Date: Apr 2009
Posts: 257
Reputation:
Solved Threads: 37
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
•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 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
php Syntax (Toggle Plain Text)
<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>
![]() |
Similar Threads
- Random numbers (finding odd and even numbers) (C)
- A lovely mixture of random numbers and arrays (C++)
- random numbers (C++)
- php table help (PHP)
- Help! How to pull numbers from table at random (C++)
- not getting non repeating random numbers many times (C)
- problem in generating non repeated random numbers (C)
Other Threads in the PHP Forum
- Previous Thread: Displaying web page snapshot.
- Next Thread: PHP hit counter
Views: 538 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database dataentry date directory display download dynamic echo email error file files folder form forms freelancing function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap sorting source space speed sql structure syntax system table tutorial tutorials update updates upload url validation validator variable video web xml youtube zend






