Undefined offset on multiple lines - php

I have written a small program to solve a mathematical problem. But when I run, it gives an undefined offset error on line number 9,11,13,15.
I have searched various questions, but didn't find anything useful.
What might be causing this. ?
<?php
$arr = [1,3,5,7,9,11,13,15];
$tries=0;
$answer=0;
while(($answer!=30) && ($tries!=1000))
{
$tries = $tries+1;
$num1=getRandomNumber();
$num2=getRandomNumber();
$num3=getRandomNumber();
$num4=getRandomNumber();
$num5=getRandomNumber();
if($num5 + $num4 + $num3 + $num2 + $num1 == 30)
{
$answer = 30;
echo $num1 + "+" + $num2 + "+" + $num3 + "+" + $num4 + "+" + $num5 + " = 30";
break;
}
}
if($tries==1000)
{
echo "1000 tries completed";
}
function getRandomNumber()
{
$arr = [1,3,5,7,9,11,13,15];
$r = mt_rand(1,15);
if(($r%2)!=0)
{
return $arr[$r];
}
}
?>

In your getRandomNumber() function, you're generating an array index between 1 and 15, but your array is only 8 elements long.
To fix this, update the call to mt_rand() to support your actual array size:
$r = mt_rand(0, count($arr) - 1);
Side-note (not answer specific), string concatenation in PHP is done with the period, . and not the +:
echo $num1 + "+" + $num2 + "+" + $num3 + "+" + $num4 + "+" + $num5 + " = 30";
// should be:
echo $num1 . "+" . $num2 . "+" . $num3 . "+" . $num4 . "+" . $num5 . " = 30";

You should change line:
$r = mt_rand(1,15);
into
$r = mt_rand(0,count($arr)-1);
because your $arr in your getRandomNumber function has only 8 elements (not 16)

function getRandomNumber()
{
$arr = [1,3,5,7,9,11,13,15];
$r = mt_rand(1,15);
if(($r%2)!=0)
{
return $arr[$r];
}
}
The mt_rand function returns a number higher then the array index witch is 7. You can either extend the array and make it have 16 index or reduce the range in mt_rand function to 0-7.

Related

mt_rand math numbers

I want to create an application that shows multiplication, addition, subtraction and division with 2 random numbers. I made a function that shows random numbers:
function Numbers() {
echo(mt_rand() . "<br>");
echo(mt_rand() . "<br>");
echo(mt_rand(1,10));
}
Numbers();
Can someone explain to me how I can make it go +/- and x each other?
I now changed my code to this:
function Numbers() {
$Number1= echo(mt_rand() . "<br>");
$Number2= echo(mt_rand() . "<br>");
$Number1 + $Number2;
$Number1 - $Number2;
$Number1 / $Number2;
}
Numbers();
Here's an example for the addition, you figure out the rest:
$a = mt_rand();
$b = mt_rand();
echo "$a + $b = " . ($a + $b);
function printRnd()
{
$a_ = rand(1,10);
$b_ = rand(1,10);
echo "a={$a_}, b={$b_}<br><br>";
$plus_ = $a_+$b_;
$minus_ = $a_-$b_;
$multi_ = $a_*$b_;
$divid_ = $a_/$b_;
echo "a+b={$plus_}<br>";
echo "a-b={$minus_}<br>";
echo "a*b={$multi_}<br>";
echo "a/b={$divid_}<br>";
}
printRnd();

Ensure no variables are equal (Lottery Code)

So I am working on my Final Project for a web application development class I'm taking and I am creating a Powerball lottery generator. For this to work, the White ball numbers cannot be duplicated. Here is how my code is looking so far:
<?php
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
?>
The issue with this code is that there is a chance that variables 'a' through 'e' have a chance of being duplicate numbers. What code could I use to ensure that none of the variables 'a' through 'e' are the same? I thought of doing something like:
if($a != $b || $a != $c || $a || $d...){
//echo numbers
}else{
//generate new numbers
};
But that is just too much work and I always try to find the most efficient ways to write code. I don't want to have to write more code than I need to. Any assistance would be greatly appreciated. Thank you in advance!
You could generate the numbers this way:
$arr = range(1, 69);
shuffle($arr);
$a = $arr[0];
$b = $arr[1];
$c = $arr[2];
$d = $arr[3];
$e = $arr[4];
Also take a look at Generating random numbers without repeats
Add them in an array and check for uniqueness:
<?php
for($x = 1; $x < 6; $x++){
$unique = false;
while(!$unique) {
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
$numbers = array($a, $b, $c, $d, $e);
if(count($numbers) == count(array_unique($numbers)) {
$unique = true;
}
}
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
}
While loop the random generation of numbers and check for duplicates on the fly.
Test it here:
https://3v4l.org/odOqb
I have changed the random numbers to a smaller size to see if it does create duplicates.
But I have not seen any.
<?php
$arr =array();
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
While (count($arr) != 5){
$arr[] = floor((lcg_value() * 6 + 1));
$arr = array_unique($arr);
}
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
Var_dump($arr);
//echo all white ball numbers and powerball number
//echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
To make it as efficient as possible you do not want to have to generate a new set of numbers each time therefore if a duplicate appears you would just want to re-pick for that letter right away.
To do this you can add the elements to an array and search through it after each letter to make sure its a unique number. This is done through the utilization of the for loop, while loop, and check variable.
<?php
for($x = 1; $x < 6; $x++) {
//set each white ball variable (a through e) to a random number between 1 and 69
$uniqueNumbers = array();
$check = true;
$a = floor((lcg_value() * 69 + 1));
array_push($uniqueNumbers, $a);
while ($check) {
$check = false;
$b = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($b == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $b);
$check = true;
while ($check) {
$check = false;
$c = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($c == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $c);
$check = true;
while ($check) {
$check = false;
$d = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($d == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $d);
$check = true;
while ($check) {
$check = false;
$e = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($e == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $e);
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
}
Below code is for 6/49 Canada lottery
<body bgcolor="gold"><font size="9"></font>
<pre>
<?php
// Code by bhupinder Deol . modify according to needs
for ($i=0;$i<=10;$i++) {
for ($x=0;$x<=5;$x++) {
$rand[$x]=rand(1,49);
}
asort($rand);
$result = array_unique($rand);
$count = count($result);
if ($count == 6) {
print_r($rand);
$x=0;
}
else
{
echo "same numbers in array";
--$x;
}
}
?>
</pre>
</body>

PHP variable is not shown as an integer

if($koltukk%4 == 2){
if($koltukk > 1000){
$koltukH = "B";
(int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
}
else{
$koltukH = "E";
(int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
}
}
$koltukR = ($koltukk / 4) + 1;
I want to get the $koltukR variable as an integer but i couldn't do it (int) did not work
You need to use the (int) casting on the other side of the assignment operator:
$koltukR = (int)(($koltukk / 4) + 1);
Or, use intval() like this:
$kolturR = intval(($koltukk / 4) + 1);
$koltukR = intval(($koltukk / 4) + 1);
You should use better variable names, move the math out of the if/else since both are the same, and you shouldn't even need to cast this as an int manually.
PHP has an intval() method that turns a variable into an integer. Pass in your variable as a parameter.
intval()
<?php
if($koltukk%4 == 2)
{
if($koltukk > 1000)
{
$koltukH = "B";
$koltukR = (int)(($koltukk / 4) + 1);
} else{
$koltukH = "E";
$koltukR = (int)(($koltukk / 4) + 1);
}
}
echo $koltukR;
?>
One important note here: intval() is NOT round(). intval() is similar to floor().
I think what you really want is round():
Here's the real answer: K.I.S.S.
if($k%4 == 2){
if($k > 1000){
$H = "B"
}
else{
$H = "E";
}
}
$R = round($k / 4) + 1;
Stealing an example from http://us2.php.net/intval to illustrate:
echo number_format(8.20*100, 20), "<br />";
echo intval(8.20*100), "<br />";
echo floor(8.20*100), "<br />";
echo round(8.20*100), "<br />";
819.99999999999988631316
819
819
820

Why isn't my PHP calculator working? (string functions..)

This calculator is supposed to take a typed input with spaces, (like "2 + 2") in the web page text box, and then echo the answer above it. Currently, the output is either "0", or it will seemingly clip a random number off of the input.
I'm 95% sure the problem is within the substr functions - they are not being assigned the correct values. This is my guess because of the output behavior. To demonstrate,
1 + 1 will = 0
2 + 2 will = 0
...
9 + 9 will = 0
10 + 10 will = 1
20 + 20 will = 2
note: The $firstNumber, $secondNumber, and $operator initial declaration values starting on line 16 are arbitrary.
Here are substr( , ) examples from PHP manual:
echo substr('abcdef', 1); // bcdef <br>
echo substr('abcdef', 1, 3); // bcd<br>
echo substr('abcdef', 0, 4); // abcd<br>
echo substr('abcdef', 0, 8); // abcdef<br>
echo substr('abcdef', -1, 1); // f<br>
.
//'if' is executed when submit is pressed
if (isset($_POST['test']))
{
$input = $_POST['test'];
$answer = calculate($input);
echo $answer;
}
//does string processing and number calculation
function calculate($input)
{
//declarations (random)
$firstNumber = 20;
$secondNumber = 30;
$operator = "+";
$answer = 7;
//string processing.
for($i = 0; $i <= strlen($input); $i++)
{
//if current position of the string scan is an operator,
if( trim(substr($input, $i, $i + 1), " ") == "+" ||trim(substr($input, $i, $i + 1), " ") == "-"
||trim(substr($input, $i, $i + 1), " ") == "*" ||trim(substr($input, $i, $i + 1), " ") == "/")
{
//then
//$operator = current position TO current position + 1
$operator = substr($input, $i, $i + 1);
//trim $operator
$operator = trim($operator, " ");
//$firstNumber = 0 TO current position - 1
$firstNumber = substr($input, 0, $i - 1);
//trim $operator
$firstNumber = trim($firstNumber, " ");
//$secondNumber = current position + 1 TO end of string
$secondNumber = substr($input, $i + 1, strlen($input));
//trim $operator
$secondNumber = trim($secondNumber, " ");
}
}
//if operator is ... then do that operation.
//example: if "+", then add $firstNumber and $secondNumber
if($operator == "+")
{
$answer = $firstNumber + $secondNumber;
}
else if($operator == "-")
{
$answer = $firstNumber - $secondNumber;
}
else if($operator == "*")
{
$answer = $firstNumber * $secondNumber;
}
else if($operator == "/")
{
$answer = $firstNumber / $secondNumber;
}
//return the calculated answer for echo
return $answer;
}
?>
<form action="test.php" method="POST">
<textarea name="test" rows="3" cols="30"></textarea>
<input type="submit" value="calculate!">
</form>
Your problem is your use of substr.
substr take a string, a start position and a length. It's the length that you are misusing. As you pan through the input string, you are increasing the length of the substring that you are looking for. Since operators have length of 1, you should be using
//if current position of the string scan is an operator,
if( trim(substr($input, $i, 1), " ") == "+" || trim(substr($input, $i, 1), " ") == "-"
||trim(substr($input, $i, 1), " ") == "*" || trim(substr($input, $i, 1), " ") == "/")
{
....
You also need to update your code here:
//$operator = current position TO current position + 1
$operator = substr($input, $i, 1);
//trim $operator
$operator = trim($operator, " ");
You could simplify this a little by doing
potentialOperator = trim(substr($input, $i, 1), " ")
and comparing that to your supported operators. That'll save you multiple unnecessary calls to trim and substr
It'll also mean that you identify the operator once and once only.
You might also want to look at preg_split with PREG_SPLIT_DELIM_CAPTURE for parsing the input string, instead of scanning it char by char.

PHP sqrt() returns NAN

I've written a piece of code to carry out the quadratic equation:
function quadratic($a,$b,$c) {
$mb = $b - ($b*2);
$bs = $b * $b;
$fac = ($a * $c) * 4;
$ans1 = ($mb + sqrt(($bs - $fac))) / (2 * $a);
$ans2 = ($mb - sqrt(($bs - $fac))) / (2 * $a);
echo ("Your <b>+</b> value is: " . $ans1 . "<br />");
echo ("Your <b>-</b> value is: " . $ans2);
}
The problem is that, if for example a=2, b=4, c=8, both answers are outputted as NAN. Any ideas as to how to fix this so that I get an actual number output?
$a * $c * 4 = 64
$bs = 4 * 4 = 16
sqrt(($bs - $fac))) = sqrt(-48)
You cant take the sqrt of a negative number, it is not defined, hence the result is NaN.
Futhermore your formula can be simplified as:
$mb = $b - ($b*2) = -$b
So instad of $mb you can simply use -$b.
Besides that, your formula is correct for the quadratic equation.
If interested in having a "real number" solution, see:
function quadratic($a,$b,$c) {
$mb = $b - ($b*2);
$bs = $b * $b;
$fac = ($a * $c) * 4;
$bsfac = $bs-$fac;
if($bsfac < 0){
$bsfac *= -1;
}
$ans1 = ($mb + sqrt(($bsfac))) / (2 * $a);
$ans2 = ($mb - sqrt(($bsfac))) / (2 * $a);
echo ("Your <b>+</b> value is: " . $ans1 . "<br />");
echo ("Your <b>-</b> value is: " . $ans2);
}
*Rounding not included
Try swapping in these lines.
$ans1 = ($mb + sqrt(abs($bs - $fac))) / (2 * $a);
$ans2 = ($mb - sqrt(abs($bs - $fac))) / (2 * $a);

Categories