# Loops, Functions and Arrays

#1

Im having difficulty in coding this, maybe you guys can help me

[ul][li]Create two random variable, \$rand1 (-10, 2) and \$rand2 (-3,6)[/li]
[li][/li][/ul]

[ul][li]create a function that will find and calculate: the remainder, sum, the difference and the product of \$rand1 and \$rand2. The function will return those calculations in an array in the mentioned order[/li]
[li][/li][/ul]

[ul][li]outside of the function, the program will call the function and display each of the result.[/li]
[li][/li][/ul]

[ul][li]the entire program should running if the remainder is not equal to zero[/li]
[li][/li][/ul]

#2

Show us your code as it is so far. Post it inside the PHP tags.

#3

[php]
echo “Loops, function, and arrays.”;
\$var1 rand(-10, 2);
\$var2 rand(-3, 6);
}

I think this is wrong, everytime i run it i get an error

[/php]

#4

??? This is not PHP code. But close…

In PHP you ASSIGN variables. Therefore, you can use these to get the random numbers
[php]
\$var1 = rand(-10, 2);
\$var2 = rand(-3, 6);
[/php]
You can put the addition into a function if needed, but to get results you would need to call the function and print the results.
You only need to do the addition once, not twice as you have in your code.
Then, to call it you would have to return the value somehow. Here is one that should work as an example.
[php]
\$results = \$input1 + \$input2;
return \$results;
}

\$var1 = rand(-10, 2);
\$var2 = rand(-3, 6);
[/php]
Hope this example helps you. I suggest you read this site on use of functions. It has a huge amount of programming knowledge on it for all areas you may need. But, please come back here when you have another question. That is why we are here. Good luck!

W3Schools (WWW Schools) Tutorials https://www.w3schools.com/php/default.asp

#5

Based on your requirements, you have some details that are off.

Create two random variable, \$rand1 (-10, 2) and \$rand2 (-3,6)
[php]\$rand1 = rand(-10, 2); \$rand2 = rand(-3,6);[/php]
create a function that will find and calculate: the remainder, sum, the difference and the product of \$rand1 and \$rand2. The function will return those calculations in an array in the mentioned order

[php]function calculate(num1, num2) {
\$result[‘remainder’] = 0; // find the remainder
\$result[‘sum’] = 0; // find the sum
\$result[‘difference’] = 0; // find the difference
\$result[‘product’] = 0; // find the product
return \$result;
}[/php]
Now, put it together
[php]do {
\$rand1 = rand(-10, 2);
\$rand2 = rand(-3,6)

``````\$result = calculate(\$rand1, \$rand2);
// pick a loop and iterate thru the results given
foreach( \$result as \$key => \$value) {
echo "The {\$key} of {\$rand1} and {\$rand2} is: {\$value}\n";
}
``````

} while(\$result[‘remainder’] != 0 ); [/php]

the entire program should running if the remainder is not equal to zero

#6

LOL, yep! I just wanted to get him started and not give it all to him for his classroom project. LOL

#7

Didn’t mean to give as much as I did, it just lead to a more streamlined way that you would approach the task rather than learning some bad habits.

OP, normally, you wouldn’t have four different processes in the same function. You would break them into separate entities. However, your requirements state a single function.