Here is the php code that I have. I am trying to take the average of the array and then take the avearge and the display numbers from the array that are less then the average using a foreach loop then an if statement inside the foreach loop.

<?php
 $total = 0;
 $avg = 0;
 $n = 0;
 $Random = array(); 
// put the data in
echo "<p>Put the numbers in<br />";
for($counter = 0; $counter <= 9; $counter++)
{
    $n = rand(1, 100);
    $int[$counter] = $n;
    array_push($Random, $n);
    echo "$n, ";
    //echo "<pre>";
    //print_r($Random);
    //echo "</pre>";
    //add the random numbers together
    $total = $total + $n;


}

echo "</p>";
echo $total;
$avg = $total  / count($Random);
echo "<p>" . $avg . "</p>";
// display the number of elements less then the average
foreach ($Random as $numbers)
    {
    $acount = 0;
    $count = 0;
}



?>

You can do it like this:

<?php

function avg($limit = 9)
{
    $array = array();
    $result = array();
    for($i = 0; $i <= $limit; $i++) {

        $r = rand(1,100);
        if(in_array($r,$array))
        {
            #add another loop
            $i--;
        }
        else
        {
            $array[] = $r;
        }
    }

    $c = count($array);
    $s = array_sum($array);

    $avg = round($s/$c);

    $result['avg'] = $avg;
    for($i = 0; $i < $c; $i++) {
        $result['numbers'][] = ($avg > $array[$i]) ? $array[$i].' ':'';
    }

    $result['numbers'] = array_filter($result['numbers']);
    return $result;
}

print_r(avg());

?>

This function removes duplicates from the array generated by rand() and performs an additional loop everytime it finds one. An example of output will be:

Array
(
    [avg] => 56
    [numbers] => Array
        (
            [0] => 54 
            [3] => 48 
            [5] => 6 
            [7] => 25 
            [8] => 46 
        )

)

Hope is useful.

Member Avatar for diafol

I couldn't really get what you needed, so I knocked this function up. Not intended to be a complete solution or to address you specific problem. I see Cereal's bashed one out along your original code - perhaps that's more appropriate for you.

function createArray($items,$min, $max,$divisor=1, $precision=0){
    if(is_int($items) && is_int($min) && is_int($max) && is_numeric($divisor) &&  is_int($precision) && $min < $max){
        for($x=0;$x<$items;$x++){
            $r[] = number_format(mt_rand($min,$max)/$divisor,$precision);   
        }
        $ave = array_sum($r)/count($r);
        $above = array();
        $below = array();
        foreach($r as $item){
            if($item >= $ave){
                $above[] = $item;   
            }else{
                $below[] = $item;
            }
        }
        sort($above);
        sort($below);
        //return an array
        return array('num'=>$items,'array'=>$r,'above'=>$above,'below'=>$below,'ave'=>$ave);
    }
}

/* CREATE ARRAY PARAMETERS
1) No. items (int) [required]
2) Minimum value (int) [required]
3) Maximum number (int) [required]
4) Divisor (int) - positive powers of 10: 1,10,100 etc for generating floats [optional]
5) Precision (int) - for deciding the displayed number of decimal places in array values [optional]
*/

if($my_array = createArray(10,12,88)){
    $output = "The original array of {$my_array['num']} numbers: " . implode(',', $my_array['array']) . " had an average value of {$my_array['ave']}. Therefore the following numbers are above or equal to {$my_array['ave']}: " . implode(',', $my_array['above']) . " leaving these numbers ". implode(',', $my_array['below']) . " below the average.";
}else{
    $output = 'Values not correct'; 
}

//in the approapriate place:

echo $output;

?>

@diafol yours is good as is more complete than mine.. and I see I also forgot to sort.. o_o' bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.