To show looping for two looping in random and cross - php

i have looping problem, i want To show replay random To looping
Code:
for ($i=0; $i <= 10; $i++) {
$result = $i;
echo "$result";
echo "<br>";
}
//----------------------------------------------
echo "<hr width='100%'>";
//----------------------------------------------
$s=$result;
$c=$result;
$test=array_fill($s, $c, 0);
$ts=microtime(true);
for ($i=0; $i <= 10; $i++) {
$selection1=mt_rand($s, $c);
$selection2=mt_rand($s, $c);
echo "$selection1 and $selection1";
echo "<br>";
}
i want result:
example result
and after random i want cross looping
Example result
#including completion of a genetic algorithm

mt_rand returns a value between the first and last values you provide. In your code, you are calling mt_rand($s, $c) when $s and $c are both set to 10, so they will always return 10.
I'm having trouble understanding what you want, so let's take a look at your code and see what's going on.
for ($i=0; $i <= 10; $i++) {
$result = $i;
echo "$result";
echo "<br>";
}
Above we're looping between 0 and 10, each time around storing our loop number as $result.
//----------------------------------------------
echo "<hr width='100%'>";
//----------------------------------------------
$s=$result;
$c=$result;
$test=array_fill($s, $c, 0);
$ts=microtime(true);
Our loop has finished and we're moving on. This code is executed once. Since $result was 10 at the end of the loop, $s and $c will always be set to 10.
for ($i=0; $i <= 10; $i++) {
$selection1=mt_rand($s, $c);
$selection2=mt_rand($s, $c);
echo "$selection1 and $selection1";
echo "<br>";
}
We're now going to loop between 0 and 10 again. mt_rand() will always return a value between $s and $c, which will always be set to 10 and 10, so will always be 10. We're just going to echo 10 and 10 10 times.
I'm unsure what you're looking for so I'll just leave that explanation there.

Related

Exam Qn: Convert do while loop to for loop (PHP)

Recently, my exams got over. My last exam was based on PHP. I got the following question for my exam:
"Convert the following script using for loop without affecting the output:-"
<?php
//Convert into for loop
$x = 0;
$count = 10;
do
{
echo ($count. "<BR>");
$count = $count - 2;
$x = $x + $count;
}
while($count < 1)
echo ($x);
?>
Please help me as my computer sir is out of station and I am really puzzled by it.
Well, If I understand well, You have to use "for" loop, instead of "do...while", but the printed text must not change.
Try:
$count = 10;
$x = 0;
$firstRun = true;
for(; $count > 1 || $firstRun;) {
$firstRun = false;
echo ($count . "<BR>");
$count -= 2;
$x = $x + $count;
}
echo ($x);
By the way loop is unnecessary, because $count will be greater than 1 after the first loop, so the while will get false.
EDIT
$firstRun to avoid infiniteLoop
$count in loop
EDIT
Fixed code for new requirement
Removed unnecessary code
Hmmm I don't know if your teacher wanted to own you... but the do{} will execute only once since $count is never < 1.
The output of your teacher's code is:
10
8
I presume there was a mistake in the code and the while would be while($count > 1) which would make more sense (since it's weird to ask for a loop to output only 10 8) and would result in this output:
10
8
6
4
2
20
Then a good for() loop would have been :
$x = 0;
$count = 10;
for($i = $count; $i > 1; $i -= 2)
{
$count -= 2;
echo $i . "<br>";
$x += $count;
}
echo $x;
Which will output the same values.
If you can, ask your teacher for this, and comment the answer ^^ ahahah

Difficulties with is_int function in PHP

I'm writing a code to solve a problem.one of my functions is not working properly.
function check_factor($sqr,$num)
{
for($i = $sqr ; true ; $i++)
{
$n = pow($sqr,2) - $num;
$s = sqrt($n);
if(is_int($s))
{
return $i;
}
}
}
I know that $s is a "double" , but even when I limit my loop counter to 2,I'll get an endless loop.
What am I missing here? why the function doesnt simply return null? and why I get Infinite loop even when there are 2 iterations?
true is making it an infinite loop
you will always get an infinit loop because sqrt() returns float means
$s = sqrt($n);
$s is a float now
and your test is testing if $s is an integer so it will be an infinit loop even after $i=2 the loop will always stuck and the $i=2 in every loop but
if you change the code to this
<?php
function check_factor($sqr,$num)
{
echo "<br>";
for($i = 1 ; $i < 3 ; $i++)
{
echo " in the loop $i<br>";
$n = pow($sqr,2) - $num;
echo "$n<br>";
$s = sqrt($n);
echo "$s<br>";
if(is_int($s))
{
echo "in the if <br>";
return $i;
}
}
return 0;
}
$val=0;
$val = check_factor(5,2);
echo "<br>$val<br>";
?>
the out put should be like this
in the loop 1
23
4.7958315233127
in the loop 2
23
4.7958315233127
0
and that's it. i hope i helped.
According to the manual: PHP function sqrt() always returns a float and NEVER an integer.
Check http://php.net/manual/en/function.sqrt.php

how to get a variable value that will be changed later

i have this code
$number = 1;
echo $number;
for ($i=0; $i < 10; $i++) {
$number++;
}
the output of echo $number is 1 not 11.
How can get the last $number value when I called it before it changed?
Once you output something to the browser, it's done. You cannot change it again later. The only way to handle this is to not output the variable until you have found its final value; ie in the example you move the echo statement to the bottom.
It's generally considered a good idea to first run all of your PHP code and determine all your variables and only then start outputting things to the browser in order to prevent the kind of problem you have now.
$number = 1;
echo 'before increment :'.$number;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo 'after increment :'.$number;
Try this way, now you will get expected result:
$number = 1;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo $number;
Reason, you have put echo $number before the increment, which was logically wrong:

How to get a total value from a loop result for integers in php. without Javascript of Jquery

how to add a total of the numbers displayed as an integer from a loop. the loop result is not an increment. here is the problem.
<?php
$i = 0;
$num =1;
while($i<=4){
echo $num;
$i++;
}
echo $num;
?>
So the result is something like this.
1 1 1 1
so my problem is how can i total the result which should be 4, and save it in a variable without incrementing. and even more confusing is. how to do the same when the value of $num is dynamic. Thank you very much. in advance
Just make an array then sum them:
<?php
$i = 0;
$num =1;
while($i<=4){
$nums[] = $num;
$i++;
}
echo array_sum($nums); //Outputs 5
?>
This assumes that $num is always numeric.
Alternatively, you could just iterate an output variable, based on the value of $num. Like so:
<?php
$num = 1; // or 2, or 3, or 4 or whatever
$output = 0;
for ($i=0; $i<=4; $i++) {
$output += $num;
}
echo $output;
?>

stuck here with lotto grid

I have a grid with 42 nrs where I will use the rand() function to pick out numbers from the grid and and mark them
so far I came up with
<?php
$row="";
print ("<table border=\"1\">");
for ($i=0; $i<6; $i++)
{
print ("<tr>");
for ($j=0; $j<7; $j++)
{
$random = rand(1,42);
$row += "(string)$random";
$som = $som + 1;
print("<th>".$som);
}
("</tr>");
}
print ("</table>");
print ("$rij");
// here I'm just testing to see if I can get a list of random numbers
for ($i=0; $i<6; $i++){
$randomNr = rand(1,42);
echo "$randomNr<br/>";
}
?>
I guess the idea is to match the numbers out of the rand function to the indexes of the table. But i'm really stuck here at getting the table to convert to an arra so I can match the index with the random numbers.
You're probably not too far off with your own attempt. You would just need to generate 6 random unique numbers and compare against them. Easiest way to do that is to generate an array using range() and pick the random numbers with array_rand() (which actually returns indexes, so you need a bit of additional code to get the values). Then you just need to find whether the currently outputted number is in the chosen number array using in_array()
Here's an example function of the general case that expands Sondre's example a bit. The function in the example takes following arguments: Total random numbers picked, Smallest number in the grid, Biggest number in the grid and the numbers per row in the grid. The function returns the generated HTML table source a string.
<?php
function generateHighlightedLotteryTable ($count = 6, $min = 1, $max = 42, $perRow = 7)
{
// Generate the picked numbers (actually we just get their indexes)
$nums = array_rand(range($min, $max), $count);
$output = "<table>\n";
for ($n = $min; $n <= $max; $n++)
{
// get "index" of the number, i.e. $min is the first number and thus 0
$i = $n - $min;
if ($i % $perRow == 0)
{
$output .= "<tr>";
}
// If the current number is picked
if (in_array($i, $nums))
{
$output .= "<td><strong>$n</strong></td>";
}
// If the current number hasn't been chosen
else
{
$output .= "<td>$n</td>";
}
if ($i % $perRow == $perRow - 1)
{
$output .= "</tr>\n";
}
}
// End row, if the numbers don't divide evenly among rows
if (($n - $min) % $perRow != 0)
{
$output .= "</tr>\n";
}
$output .= "</table>";
return $output;
}
echo generateHighlightedLotteryTable();
?>
I hope this is what you were trying to achieve.
This would create a grid of 42 numbers and mark out a random one. If you want to mark out more create and array and check against that insted of just the rand variable. In you're original code there you were actually running the rand-function 42 times which I guess is unintended.
EDIT: Or did you need the grid to be filled with random numbers?
$rand = rand(1, 42);
echo "<table>";
for($i = 1;$i <= 42; $i++) {
if($i%7 == 1) {
echo "<tr>";
}
$print = $rand == $i ? "<strong>" . $i . "</strong>" : $i;
echo "<td>" . $print . "</td>";
if($i%7 == 0) {
echo "</tr>";
}
}
echo "</table>";

Categories