LV_
LV_

Reputation: 45

SUM and average weight PHP

I'm working on something that calculates me total weight of my class and average weight of my class. In $T I entered weight of girls in class, in

Echo "SUM OF WHOLE CLASS IS : $s"; 

must be SUM of weight of whole class and

echo "<br/>AVERAGE WEIGHT IS $p"; 

is average weight of class.. I dont see where the problem is, it just says that is on line number 4..

 <body>
    <?PHP 
    $T=array (47,47,62,60,71,55,50,52,62,80,65);
    $s=0; $BUB=0; 
    for ($BUB=0;$BUB<=11;$BUB++)
    {
        $s=$s+$T[$BUB];
        $BUB++;
    }

    $p=$s/11;
    echo "SUM OF WHOLE CLASS IS : $s"; 
    echo "<br/>AVERAGE WEIGHT IS $p";

    ?>
    </body>

Upvotes: 1

Views: 384

Answers (2)

OfirH
OfirH

Reputation: 657

Try replacing that: $BUB<=11 with $BUB<11.

Notice that $BUB start with the value 0. as well as the array. so $T[11] isn't exists.

Upvotes: 0

Make it simple using the array functions of PHP !

<?php
    $T=array (47,47,62,60,71,55,50,52,62,80,65);
    echo "SUM OF WHOLE CLASS IS : ".array_sum($T); //"prints the sum 
    echo "<br/>AVERAGE WEIGHT IS ".array_sum($T)/count($T); //"prints" the average

Demo

Upvotes: 3

Related Questions