# 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 2mean = (sum of all data) / (total number of data)Therefore, mean = (4 + 8 + 7 + 6 + 4 + 2 + 4 + 4 + 1 + 2)/10 = 42/10 =4.2for 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. Thereforemedian =[(n/2)th element + (n + 2)/2th element]/2= (10/2 th + 11/2 th)/2 = (5th + 6th)/2 = (4 + 4)/2 =4we 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: **

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

**Algorithm for median: **

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

**Algorithm for mode: **

- sort the array
- declare three variables, let consider
**max_count, res,**and**count.** - initialize
**max_count**with**1**,**res**with the**first element**of the array and**count**with**1.** - 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. - close the loop
- 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: **

#include “stdio.h”

#include “conio.h”

int main()

{

int n[100],nsize,i,j,temp,median;

clrscr():

print(“59(maximum 100):”):

scanf (“⁒d”,&nsize);

print(“5,7,9,4,3,7,4,7,\n”,nsize)

for(i=0;i<nsize;i++)

scanf ("⁒d",&n[i]):

for(i=0;i<nsize-1;i++)

{

for(j=i+1;j<nsize;j++)

{

if(n[i]<n[j])

{

temp=n[n]:

n[i]=n[j]:

n[j]=temp:

}

}

}

if(nsize⁒2==0)

{

median=(n[(nsize-1)/2]+n[nsize/2])/2;

}

else

{

median=n[nsize/2];

}

printf("\nMedian value=⁒d",median);

getch();

}

help me give me a example of this program please for my final presentation

why you used float arr[] instead of int arr[] in line no.59?