Last active
December 31, 2015 22:09
-
-
Save itskingori/8051297 to your computer and use it in GitHub Desktop.
Manipulating Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Exercise 05: Manipulating Arrays</title> | |
</head> | |
<body> | |
<?php | |
// Create your array of 30 high temperatures then find the average high | |
// temp, the five warmest high temps and the five coolest high temps. Print | |
// your findings to the browser. | |
// 68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78, | |
// 68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83 | |
// Create the array with the high tempratures in the same order I was given | |
$high_temps = array(68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, | |
78, 68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, | |
89, 83); | |
// Find the total number of temperatures in the array | |
$no_of_temps = count($high_temps); | |
// Find the sum of all the temperatures | |
$sum_of_temps = array_sum($high_temps); | |
// Find the average of the temperatures | |
$average_temp = $sum_of_temps / $no_of_temps; | |
// Output the average temperature | |
echo "Average Temperature"."<br/>"; | |
echo $average_temp."<br/>"; | |
echo "<br/>"; | |
// Sort array for lowest to highest | |
sort($high_temps); | |
// Output the five highest | |
echo "Five Highest Temperatures"."<br/>"; | |
for ($i = $no_of_temps-1; $i >= $no_of_temps-5 ; $i--) { | |
echo $high_temps[$i]."<br/>"; | |
} | |
echo "<br/>"; | |
// Output the five lowest | |
echo "Five Lowest Temperatures"."<br/>"; | |
for ($i = 0; $i <= 4; $i++) { | |
echo $high_temps[$i]."<br/>"; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment