0

I am trying to use a modulo to make all even numbers replaced with a star and all odds replaced with a percent sign... this is my code:

<?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<=20; $row++){
    echo "<tr>\n";
    for($col=1;$col<=20; $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>";

// 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<=20; $row++){
    echo "<tr>\n";
    for($col=1;$col<=20; $col++) {
        echo"<td>{$temp_array[$key]}</td>\n";
        // increase the key
        $key++; 
    }
    echo "</tr>";
}
// end the html table of sorted values
echo "</table>";


// 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<=20; $row++){
    echo "<tr>\n";
    for($col=1;$col<=20; $col++) {
        echo"<td>{$temp_array[$key]}</td>\n";
        // increase the key
        $key++; 
    }
    echo "</tr>";
}
// end the html table of sorted values
echo "</table>";
?>

I need to make another table for the use of the modulo so it doesn't affect the other tables? Can anyone help? I can verify further if needed!

Thanks and respond a.s.a.p!
Sedique

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by <M/>
1

This is the principle:

for($row=1;$row<=20; $row++){
    echo "<tr>\n";
    for($col=1;$col<=20; $col++) {
        $x=rand($min,$max);
        $random_array[$row][$col] = $x;

        if($x % 2 == 0) {

            $char = '*';
        } else {

            $char = '%';
        }

        echo"<td>$char</td>\n";
        // add value to the $temp_array
        $temp_array[] = $x;
    }
    echo "</tr>";
}
0

Wow... I just praise you... you are a complete genius... do you mind if I call you a guru????

Thank you for helping, you don't realize how much you've helped me!

Thanks and best regards!
Sedique

1

No problem, my friend. And please do not call me a guru since I am far far from that :-). You will soon get practice too.

If you find the answers helpful you can always vote (up arrow on the right side). If you think the post is really bad you can also downvote it (down arrow on the right side).

If this wrapps up this topic you can mark is as solved. If another problems arise just start a new topic.

Edited by broj1

0

Since your far from a guru, might as well call you the Da Vinci of the web :) or the broj1 of the web :)

I think everything you post is a solution so I will definitly triple thumbs up!

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.