In: Computer Science
Write a PHP script that:
1) Has an associative array with 10 student names and test scores (0 to 100). ie. $test_scores('John' => 95, ... );
2)Write a function to find the Average of an array. Input is an array, output is the average. Test to make sure an array is inputted. Use ARRAY_SUM and COUNT.
3)Output the test scores highest to lowest, print scores above the average in Green. Find a PHP Sort function for associative arrays, high to low. <font color='green'>text</font>
4)Output the test scores lowest to highest, print scores below the average in Red. Find a PHP Sort function for associative arrays, low to high. <font color='red'>text</font>
If you have any doubts, please give me comment...
<?php
function calcAvg($test_scores){
return array_sum($test_scores)/count($test_scores);
}
$test_scores = array("John"=>95, "Smith"=>87, "Kim"=>91, "Julie"=>75, "Doe"=>80, "Raju"=>85, "James"=>78, "Sam"=>65, "Rubi"=>80, "Dean"=>72);
$avg = calcAvg($test_scores);
echo "Average: ".$avg."<br />";
arsort($test_scores);
echo "<b>Above average: </b><br />";
foreach($test_scores as $name=>$score){
if($score>=$avg)
echo "<font color='green'>".$name." - ".$score."</font><br />";
}
asort($test_scores);
echo "<b>Below average: </b><br />";
foreach($test_scores as $name=>$score){
if($score<$avg)
echo "<font color='red'>".$name." - ".$score."</font><br />";
}