PHP Program to find the average of elements in an array


April 3, 2022, Learn eTutorial
1917

Find the sum of elements in an array?

In this program, we are going to find the average of elements in an array. To find the average we first have to find the sum to the sum of elements in an array we have to add every element with each other after that we have to divide the result by the number of elements in the array. After that, we can print the result as the average of elements in the array. For example, the array is 45,20,69,52,31  then the output will be 43.4.

How to find the average of elements in an array using PHP?

In this program, to find the average of elements in an array we are accepting the values from the user and assign to the array arr[]. To insert values into an array first we have to read the limit of the array into the variable len. Then we have to initialize an empty array arr[] and after that, we have to assign the value 0 into the variable i and perform the loop until the condition 'i < len' becomes false and also increment the value of the variable i in every iteration and in the loop block we have to read the elements into the array arr[i] from the user. Then we can print the elements in the array arr[].      Then we have to assign the value 0 into the variable sum and after that, we have to assign the value 0 into the variable i and perform the loop until the condition 'i < len' becomes false and also increment the value of the variable i in every iteration and in the loop block we have to perform the operation 'sum += arr[i]' and assign the result into the variable sum after the completion of the loop we have to perform the operation 'sum / len' and assign the result into the variable avg and we can print the value of the variable avg as the average of the elements of the array.

ALGORITHM

Step 1: Accept the limit of the array from the user and assign it to the variable len

Step 2: Initialize an empty array arr[]

Step 3: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < len' becomes false and increment the value of variable i in every iteration

        (i) Assign the computed value of 'i + 1' into the variable j (only for printing the element count in sub-step ii)

        (ii) Read the elements from the user and assign them to the array arr[i]

Step 4: Print every element of the array arr[] using the for loop

Step 5: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < len' becomes false and increment the value of variable i in every iteration

        (i) Add the value of variable sum with the element of arr[i] and assign the result into the variable sum

Step 6: Assign the computed value of 'sum / len' to the variable avg

Step 7: Print the value of the variable avg as the average of the elements of the array

PHP Source Code

                                          <?php
$len = readline("Enter the limit of the array: ");
$arr = array();
for ($i = 0; $i < $len; $i++) {
    $j = $i + 1;
    $arr[$i] = readline("Enter element $j: ");
}
echo "The elements in the array are: \n";
for ($i = 0; $i < $len; $i++) {
    echo "$arr[$i] ";
}
$sum = 0;
for ($i = 0; $i < $len; $i++) {
    $sum += $arr[$i];
}
$avg = $sum / $len;
echo "\nThe average of the elements in the array is $avg ";
?>
                                      

OUTPUT

Enter the limit of the array: 5
Enter element 1: 68
Enter element 2: 74
Enter element 3: 15
Enter element 4: 23
Enter element 5: 41
The elements in the array are:
68 74 15 23 41 
The average of the elements in the array is 44.2