So i have this problem where i generate random numbers from 1 - 10 then displaying the numbers then identifying what is repeating. BTW intNcases is the number of numbers and should not exceed 20. This is actually our assignment and i'm really having a hard time with it please help. This is my code so far.
Sample Output
Random numbers of case is: 7
Random numbers are: 4, 2, 1, 1, 4,3,2
Numbers: 4, 2, 1, 3
Repeating numbers are: 4, 2, 1
<html>
<body>
<?php
$intNcases = 5;
$hold = array(0,0,0);
$temp = array(0,0,0);
$rep = array(0,0,0);
$num = array(0,0,0);
$count = 1;
if($intNcases>20)
{
echo 'Error N cases is greater than 20';
}
else
{
echo 'The number of case/s is: '. $intNcases;
echo '<br><br>'. 'Input'.'<br>';
for($x=0;$x<$intNcases;$x++)
{
$N = rand(1,10);
echo $N. '<br>';
$hold[$x] = $N;
$temp[$x] = $N;
}
echo 'OUTPUT<br>';
for($d=0;$d<$intNcases;$d++)
{
for($j=1;$j<$intNcases;$j++)
{
if($hold[$d] == $temp[$j])
{
$rep[$j-1] = $hold[$j-1];
$hold[$j-1] = 0;
}
else
{
$num[$j-1] = $hold[$j-1];
}
}
echo '#'.$count.' - '.$num[$d]. '<br>';
$count++;
}
echo 'Repeating numbers are: ';
for($k=0;$k<sizeof($rep);$k++)
{
echo $rep[$k]. ' ';
}
}
?>
</body>
</html>
Maybe you can do this more easier with array_unique or array_shuffle or other array functions
You could try this.
$intcases = rand(1,20);
$numbers = array();
echo 'Random number of cases: '. $intcases . '</br>';
echo 'Random numbers are: ';
for($x=0;$x<$intcases;$x++)
{
$number = rand(1,10);
echo $number. ' ';
if(array_key_exists($number, $numbers))
{
$numbers[$number] = $numbers[$number] + 1;
}else
{
$numbers[$number] = 1;
}
}
echo '</br>';
echo 'Numbers are: ';
foreach($numbers as $number => $x)
{
echo $number . ' ';
}
echo '</br>';
echo 'Repeating numbers: ';
foreach($numbers as $key => $value)
{
if($value > 1)
{
echo $key . ' ';
}
}
echo '</br>';