How can this PHP code be changed to use a parameter to the
function that you would then pass $students in? (instead of using a
global).
Also, can you show how to use a loop to handle any length of array?
(instead of an array that is 3 elements).
<?php
$students = array(
array('FName'=>'Jane', 'LName'=>'Doe', 'ClassScore'=>90),
array('FName'=>'Bob', 'LName'=>'Joe', 'ClassScore'=>50),
array('FName'=>'John', 'LName'=>'Doe', 'ClassScore'=>20)
);
//1. CalculateSum() Function
function CalculateSum()
{
// Use $students array throughout the function.
global $students;
// Add the $students array ClassScores together.
$Sum = $students[0]['ClassScore'] + $students[1]['ClassScore'] + $students[2]['ClassScore'];
return $Sum;
}
// Print out the results.
echo "Sum of scores = " . CalculateSum()."<br>" ;
//2. CalculateAverage() Function
function CalculateAverage()
{
// Use $students array throughout the function.
global $students;
// Use the CalculateSum() Function to find the Average.
$Sum = CalculateSum();
// Get the average by dividing the class score sum by the # of students.
$Avg = $Sum / 3;
return $Avg;
}
// Print out the results.
echo "Average of scores = " . CalculateAverage() . "<br>";
//3. FindMin() Function
$MinArray = FindMin();
// Create the function.
function FindMin()
{
// Use $students array throughout the function.
global $students;
// Define variable to return a class score.
$score1 = $students[0]['ClassScore'];
$score2 = $students[1]['ClassScore'];
$score3 = $students[2]['ClassScore'];
// If score1 is lower than 2 & 3.
if ($score1 <= $score2 && $score1 <= $score3)
{
return $students[0];
}
// If score2 is lower than 1 & 3.
elseif (($score2 <= $score1 && $score2 <= $score3))
{
return $students[1];
}
// If score3 is lower than 1 & 2.
else
{
return $students[2];
}
} // end FindMin().
// Print out the results.
echo "Minimum class score: " . $MinArray['FName'] . " " . $MinArray['LName'] ." ". $MinArray['ClassScore'] . "<br>";
//4. FindMax() Function
$MaxArray = FindMax();
// Create the function.
function FindMax()
{
// Use $students array throughout the function.
global $students;
// Define variable to return a class score.
$score1 = $students[0]['ClassScore'];
$score2 = $students[1]['ClassScore'];
$score3 = $students[2]['ClassScore'];
// If score1 is higher than 2 & 3.
if ($score1 >= $score2 && $score1 >= $score3)
{
return $students[0];
}
// If score2 is higher than 1 & 3.
elseif (($score2 >= $score1 && $score2 >= $score3))
{
return $students[1];
}
// If score3 is higher than 1 & 2.
else
{
return $students[2];
}
} // end FindMax().
echo "Maximum class score: " . $MaxArray['FName'] . " " . $MaxArray['LName'] ." ". $MaxArray['ClassScore'] . "<br>";
?>
Here we have changed all function definitions to accept a parameter. And the parameter needed to be passed while calling the function.
For making code useful for any no of elements and not just 3 we have used for loop and it iterates count($students) no of times, i.e no of elements in the array. Propper comments have been included.
Code:
<?php
$students = array(
array('FName'=>'Jane', 'LName'=>'Doe', 'ClassScore'=>90),
array('FName'=>'Bob', 'LName'=>'Joe', 'ClassScore'=>50),
array('FName'=>'John', 'LName'=>'Doe', 'ClassScore'=>20)
);
//1. CalculateSum() Function
function CalculateSum($students)
{
// initial sum is zero
$sum = 0;
// Use $students array throughout the function.
// global $students;
// Add the $students array ClassScores together.
for($i=0 ; $i<count($students); $i++)
$sum += $students[$i]['ClassScore'];
return $sum;
}
// Print out the results.
// Here we passed $students to function
echo "Sum of scores = " . CalculateSum($students)."<br>" ;
//2. CalculateAverage() Function
function CalculateAverage($students)
{
// Use the CalculateSum() Function to find the Average.
// passed $students as parameter
$Sum = CalculateSum($students);
// Get the average by dividing the class score sum by the no of students.
$Avg = $Sum / count($students);
return $Avg;
}
// Print out the results.
echo "Average of scores = " . CalculateAverage($students) . "<br>";
//3. FindMin() Function
$MinArray = FindMin($students);
// Create the function.
function FindMin($students)
{
// assuming 1st elemenet is minimum
$minVal = $students[0]['ClassScore'];
$min=0;
// Use for loop to check all elements
for($i=0; $i<count($students); $i++){
// if new minimum is found, set it to minVal
if($minVal < $students[$i]['ClassScore']){
$minVal = $students[$i]['ClassScore'];
$min = $i;
}
}
// return element of array that had minimum classscore
return $students[$min];
} // end FindMin().
// Print out the results.
echo "Minimum class score: " . $MinArray['FName'] . " " . $MinArray['LName'] ." ". $MinArray['ClassScore'] . "<br>";
//4. FindMax() Function
$MaxArray = FindMax($students);
// Create the function.
function FindMax($students)
{
// assuming 1st elemenet is maximum
$maxVal = $students[0]['ClassScore'];
$max=0;
// Use for loop to check all elements
for($i=0; $i<count($students); $i++){
// if new minimum is found, set it to minVal
if($maxVal > $students[$i]['ClassScore']){
$maxVal = $students[$i]['ClassScore'];
$max = $i;
}
}
// return element of array that had minimum classscore
return $students[$max];
} // end FindMax().
echo "Maximum class score: " . $MaxArray['FName'] . " " . $MaxArray['LName'] ." ". $MaxArray['ClassScore'] . "<br>";
?>
Output:
Sum of scores = 160<br>Average of scores = 53.333333333333<br>Minimum class score: Jane Doe 90<br>Maximum class score: John Doe 20<br>
Get Answers For Free
Most questions answered within 1 hours.