Robson » Little Red Book » Mean mark #1

Chapter 3 - Question 2

A marks list is available for a class of ten students. Design a program to output the mean mark for the class.

Solution 1

<?
   // ten students all have marks
   // for this solution, the marks are going to be from 0 (all wrong) to 10 (all correct)
   $student_marks = array(3, 7, 2, 5, 10, 7, 9, 0, 2, 3);
   
   // add up all the values in the arry
   // then divide by the number of values in the array
   // this solution would work for any number of students
   $mean_mark = array_sum($student_marks) / count($student_marks);
   
   // output the result
   echo 'Mean mark: ' . $mean_mark;
?>

Which produces:

Mean mark: 4.8

Log

© 2004-17 robson | cc unless stated