The program is written to find the average of ‘n’ given inputs. The ‘n’ inputs are taken from the user.
The numbers are first added together and then the sum is divided by ‘n’ to get the average.
For example, let the given inputs be 1, 2, 3, 4, 5.
Therefore, the average will be:
(1 + 2 + 3 + 4 + 5 )/ 5 = 15 / 3 = 5
INPUT: The ‘n’ numbers
OUTPUT: Average of the numbers.
PROCESS:
Step 1: [Taking the input]
Read n [number of elements]
For i=0 to n-1 repeat
Read arr[i]
[End of ‘for’ loop]
Step 2: [Finding the average of the numbers]
Set sum<-0
For i=0 to n-1 repeat
Set sum<-sum+arr[i]
[End of ‘for’ loop]
[Finding the average]
Set avg<-(sum/n)
Print "The average of the numbers: avg"
Step 3: Stop.
for(i=0;i<n;i++)--------------------------O(n)
{
sum=sum+arr[i];
}
avg=(sum/n);-----------------------------O(1)
The time complexity of finding the average of numbers is O(n) where ‘n’ is the number of elements whose average is to be found.
The space complexity of finding the average of numbers is O(n) where ‘n’ is the number of elements whose average is to be found.