0

I had a task to write program that gives 20 numbers from 9 to 99 using empty array and rand function which I did...but the second step is for me to calculate and get the average number.

With functions like array_sum I can get what I want, but the idea is not to use any of array functions, just arithmetic operators

$arrNums = array();
$sum = 0;
$intTotalNum = 20;

for($i = 0; $i < $intTotalNum; $i++) {
  $intRand = rand(9, 99);
  $arrNums[] = $intRand;
  $sum = $sum + $arrNums;
  $average = $sum / count($arrNums);
}


var_dump($arrNums);
echo "<br>";
echo $average;

Code gives me an error "Unsupported operand types"

2
  • 1
    you are trying add a integer with an array that's why the error "Unsupported operand types" Commented Aug 22, 2019 at 10:38
  • So what should I do? Commented Aug 22, 2019 at 10:40

3 Answers 3

2

This is how you can achieve it without using an array functions:

$arrNums = array();
$sum = 0;
$intTotalNum = 20;

for($i = 0; $i < $intTotalNum; $i++) 
{
  $intRand = rand(9, 99);
  array_push($arrNums,$intRand);
  $sum = $sum+$intRand;
}


echo '<pre>';
print_r($arrNums);
echo "<br>";
echo $sum;


//echo $average;

Just add your $sum to current $intRand and print it outside the loop.

The reason why I have used array_push functions so that you can print all the array elements outside for loop and check and verify how many numbers generated. If you don't want to print an array then it is no need, you can comment or remove it.

Sign up to request clarification or add additional context in comments.

2 Comments

array_push is a array function !
Updated! I have just used to push elements for debugging purpose, I am not manipulating any array functions to get sum :) @shihab
1

Just put the $average out side the for loop


    $arrNums = array();
    $sum = 0;
    $intTotalNum = 20;

    for($i = 0; $i < $intTotalNum; $i++) {
      $intRand = rand(9, 99);
      $arrNums[] = $intRand;
      $sum += $intRand;

    }

    $average = $sum / $intTotalNum;

    var_dump($arrNums);
    echo "<br>";
    echo $average;

Comments

0

You are trying add a integer with an array that's why the error "Unsupported operand types" . you can get the average more easily like this



    $sum = 0;
    $intTotalNum = 20;
    $arrNums = array();

    for($i = 0; $i < $intTotalNum; $i++) {
      $arrNums[] = rand(9, 99);
      $sum = $sum + $arrNums[$i];
    }

    $average = $sum / $intTotalNum;
    var_dump($arrNums);
    echo "<br>";
    echo $average;

4 Comments

Can you explain me why we need variable $intSum with value of 0, and how that affects on code to give us average number?
we are initializing $sum with 0 so that we can add other values into it . like it is a basket before you put anything on it you have to empty it . so that it remains with only the things that you have put into it
It's a complete waste to calculate the average on each iteration.
@Andreas you are right .i have updated my code . At first i just pointed the fault in his code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.