0

So I stumbled upon this question and I wanted to figure out how to find the mean, median, mode, highest number, lowest number, how many evens, and how many odds are in each row... I honestly can't figure this out...
I want it to display next to the table I have created, which is the last table... and I want to connect the new colums to the original table... do you see what I mean?

Here is my code:

<p>
  <?php
$x=0;
$min=900;
$max=2000;
// initialize array of random values
$random_array = array();
// initialize temporary array for sorting
$temp_array = array();
// start html table for random values
echo "<table border=\"1\">";
// create $random_array with random numbers, display html table rows and
// store each value in onedimensional $temp_array for sorting
for($row=1;$row<=10; $row++){
    echo "<tr>\n";
    for($col=1;$col<=10; $col++) {
        $x=rand($min,$max);
        $random_array[$row][$col] = $x;
        echo"<td>$x</td>\n"; 
        // add value to the $temp_array
        $temp_array[] = $x;
    }
    echo "</tr>";
}
// end the html table
echo "</table>";
?>
</p>
<p>
  <?php
// sort the $temp_array (sort works on onedimensional arrays)
sort($temp_array);
// start a table for sorted values
echo "<table border=\"1\">";
// initialize counter that will be the $temp_array's key
$key = 0;
// display html table rows of sorted values
for($row=1;$row<=10; $row++){
    echo "<tr>\n";
    for($col=1;$col<=10; $col++) {
        echo"<td>{$temp_array[$key]}</td>\n";
        // increase the key
        $key++; 
    }
    echo "</tr>";
}
// end the html table of sorted values
echo "</table>";
?>
</p>
<p>
  <?php
// sort the $temp_array (sort works on onedimensional arrays)
rsort($temp_array);
// start a table for sorted values
echo "<table border=\"1\">";
// initialize counter that will be the $temp_array's key
$key = 0;
// display html table rows of sorted values
for($row=1;$row<=10; $row++){
    echo "<tr>\n";
    for($col=1;$col<=10; $col++) {
        echo"<td>{$temp_array[$key]}</td>\n";
        // increase the key
        $key++; 
    }
    echo "</tr>";
}
// end the html table of sorted values
echo "</table>";
?>
</p>
<?php
echo "<table border=\"1\">";
$AVE=0;
$mean=0;
for($row=1;$row<=10; $row++){
    echo "<tr>\n";
    for($col=1;$col<=10; $col++) {
        $AVE=$AVE+$random_array[$row][$col];
        $mean=$mean($random_array) . '<br>';
        if($random_array[$row][$col] % 2 == 0) {
            $char = '*';
        } else {
            $char = '%';
        }
        $C=$random_array[$row][$col];
        echo"<td>$char $C</td>\n";
        // add value to the $temp_array
        $temp_array[] = $x;
    }
    echo "</tr>";
}
echo "</table>";
        $CAL_AVE=$AVE/400;
        echo "$CAL_AVE";
        echo 'Mean: '.mmmr($random_array, 'mean') . '<br>';
?>

Tell me if you need more of an explanation of what I am attempting :)
Thanks!
Sedique

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by pritaeas
0

You already outputted the table, so connecting them is not possible. What you should do is calculate before outputting those tables, and then create the new one in a single loop.

0

Well, i'm not that successful... do you mind giving me an example? I am a newbie coder, so my skills are weak compared to yours.

0

You now use separate loops for your tables, you need to merge them into one. So instead of having:

for($row = 1; $row <= 10; $row++) {
  echo "<tr>\n";
  for($col = 1; $col <= 10; $col++) {

three times, you only need that once. The different bodies use different arrays, so make sure they are all generated before the new loop.

Edited by pritaeas

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.