The program is written to find the average of the odd numbers from ‘n’ given inputs. The ‘n’ inputs are taken from the user.
The numbers are first checked, the odd numbers are added together and the number of odd numbers is counted. Then the sum of the odd numbers is divided by the counted value to get the average.
For example, let the given inputs be 1, 2, 3, 4, 5.
Therefore, the average will be:
( 1+ 3 + 5 ) / 3 = 9 / 3 = 3
INPUT: The ‘n’ numbers
OUTPUT: Average of the odd 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 odd numbers]
Set sum<-0
Set c<-0
For i=0 to n-1 repeat
If arr[i] mod 2 ≠ 0 then
Set sum<-sum+arr[i]
Set c<-c+1
[End of ‘for’ loop]
[Finding the average]
Set avg<-(sum/c)
Print "The average of the numbers: avg"
Step 3: Stop.
for(i=0;i<n;i++)----------------------------------O(n)
{
if(arr[i]%2==0)-----------------------O(1)
{
sum=sum+arr[i];
c++;
}
}
//finding the average
avg=(sum/c);----------------------------------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.