Find Mean, Median and Mode of an Ungrouped data

This tutorial is for learning how to find mean, median and mode of an ungrouped data in C++. Mean, median and mode also called as measures of central tendency are numbers which represent a whole set of data.

Mean

The mean is the average of data. means, The mean is the sum of whole data divided by the number of data.

Median

The median is the middle number in the ordered set of data.

Mode

The mode is the most common number in the set of data.

Example: Mean, Median and Mode

input data: 4 8 7 6 4 2 4 4 1 2

mean = (sum of all data) / (total number of data)
Therefore,
       mean = (4 + 8 + 7 + 6 + 4 + 2 + 4 + 4 + 1 + 2)/10
            = 42/10 = 4.2

for median we need to sort input data in assendig order.
input data (in assending order) = 1 2 2 4 4 4 4 6 7 8
Since, we have even number of data.
Therefore median = [(n/2)th element + (n + 2)/2th element]/2
                 = (10/2 th + 11/2 th)/2
                 = (5th + 6th)/2
                 = (4 + 4)/2 
                 = 4

we can see from the data that 4 has maximum number of frequency in the input data.
Therefore, Mode = 4

Algorithm to find Mean, Median and Mode in C++

  • declare an array of size n and initialize with the data in it.

Algorithm for mean: 

  1. declare a variable sum and initialize it with 0.
  2. start loop form i = 0 to n. For each arr[i], add arr[i] in the sum.
  3. print means of data as sum/n

Algorithm for median: 

  1. sort the array.
  2. if the length of array i.e. n is odd then print arr[i]/2.
  3. if the length of array i.e. n is even, then print  (arr[n/2 – 1] + arr[n/2])/2

Algorithm for mode:

  1. sort the array
  2. declare three variables, let consider max_count, res, and count.
  3. initialize max_count with 1, res with the first element of the array and count with 1.
  4. start a loop form i = 0 to n. for each arr[i], if arr[i] is equal to arr[i – 1] then increment count by 1, otherwise, if count is greater than max_count then max_count = count and save arr[i – 1] in res. save 1 in count and close else.
  5. close the loop
  6. print res.

C++ program to find Mean, Median and Mode of an Ungrouped data

#include <bits/stdc++.h> 
using namespace std;

//finding mean of the ungrouped data in array
float mean(float arr[], int n){
    float sum = 0;

    for(int i = 0;i < n; i++)
        sum += arr[i];
    
    return sum/n;
}

//finding median of the ungrouped data in the array
float median(float arr[], int n){

    //sort the array
    sort(arr, arr + n);

    if(n % 2 == 0)
        return (arr[n/2 - 1] + arr[n/2])/2;

    return arr[n/2];
}

//finding mode of ungrouped data
float mode( float arr[], int n){

    // Sort the array 
    sort(arr, arr + n); 
  
    //finding max frequency  
    int max_count = 1, res = arr[0], count = 1; 

    for (int i = 1; i < n; i++) { 
        if (arr[i] == arr[i - 1]) 
            count++; 
        else { 
            if (count > max_count) { 
                max_count = count; 
                res = arr[i - 1]; 
            } 
            count = 1; 
        } 
    } 
  
    // when the last element is most frequent 
    if (count > max_count) 
    { 
        max_count = count; 
        res = arr[n - 1]; 
    } 
    
    return res;
}

int main(){
    int n;
    float arr[50];

    cout<<"Enter the size of array: ";
    cin>>n;

    //input in the array
    cout<<"Enter the elements of array: ";
    for(int i = 0; i < n; i++)
        cin>>arr[i];

    //print mean, median and mode of ungrouped data in array
    cout<<"\nMean = "<<mean(arr, n);
    cout<<"\nMedian = "<<median(arr, n);
    cout<<"\nMode = "<<mode(arr, n);
    
    return 0;
}

Output: 

Enter the size of array: 10
Enter the elements of array: 4 8 7 6 4 2 4 4 1 2

Mean = 4.2
Median = 4
Mode = 4

Time Complexity: O(n), where n is the number of data

 

you may also read: 

  1. Search element in a sorted matrix in C++
  2. How to find Hamming distance between two strings in c++

Leave a Reply

Your email address will not be published. Required fields are marked *