To calculate the mean and medium of any sorted series

Question - 
Given an sorted series, you have to find the mean and medium of that series.

Example - 
Input : 1, 2, 3, 4, 5
Output : mean = 3 , medium =3

Approach - 
we solve this question with the help of standard formula.

Implementation - 

//To calculate mean and medium
#include<iostream>
using namespace std;
int main()
{
    cout<<"\t\t\tTo find mean and medium of sorted series"<<endl;
    int n;
    cout<<"Enter the no of numbers in sorted series : ";cin>>n;
    int arr[n];
    cout<<"Enter sorted series : "<<endl;
    for(int i=0;i<n;i++)
    cin>>arr[i];
    double mean=0,medium;
    for(int i=0;i<n;i++)
    mean+=arr[i];
    mean=mean/n;
    
    if(n%2==0){
        medium=(arr[n/2-1]+arr[n/2])/2;
    }
    else
    medium=arr[n/2];
    cout<<"Mean of this series is : "<<mean<<endl;
    cout<<"Medium of this series is : "<<medium;


}

Time complixity - O(1)

Comments