<html><head><title></title></head><body>
<?
// Solution of Assignment 2; CSIT 207 (Spring 07)
$a = array(); // Two dimensional array (stores usernames and passwords)
$u = array(); // One-dimensional array (stores usernames to check for repetition)
$p = array(); // One-dimensional array (stores passwords to check for repetition)
$array_size = 8;
for ($i = 1; $i <= $array_size; $i++)
{
	$username_length = 5;
	do
	{
		$username = ""; // Must be here
        	for ($j=0; $j < $username_length; $j++)
        	{
                	// Generate a random number n between 65 and 90
	                // Remember uppercase letters have ASCII positions 65 to 90.
        	        $n = mt_rand(65,90);
               		// Get the letter whose ASCII position is n
	                $c =chr($n);
        	        // Concatenate this letter to the previous ones by
                	// using the dot operator.
	                $username = $username.$c;
        	}
	}
	while (in_array($username,$u));
	do
	{
		$password = mt_rand(10000,99999);
	}
	while (in_array($password,$p));
	#$a[$i] = array($username,$password); // OK
	// Assign the usernames and passwords to the 2D array a
	$a[] = array($username,$password);
	// Assign the username to u
	$u[] = $username;
	// Assign the password to p
	$p[] = $password;
}
echo "<b>Before Sorting: </b><br>\n";
echo "<center><table width='40%' border='1'><tr><td><b>Username</b></td><td><b>Password</b></td></tr>\n";
foreach ($a as $row)
{
	echo "<tr>";
	foreach ($row as $col)
		echo "<td>$col</td>";
	echo "</tr>\n";
}
echo "</table></center>\n";

asort($a);
echo "<b>After Sorting: </b><br>\n";
echo "<center><table width='40%' border='1'><tr><td><b>Username</b></td><td><b>Password</b></td></tr>\n";
foreach ($a as $row)
{
        echo "<tr>";
        foreach ($row as $col)
                echo "<td>$col</td>";
        echo "</tr>\n";
}
echo "</table></center>\n";

?>

</body></html>
