0% found this document useful (0 votes)
96 views2 pages

Aim: To Calculate Mean, Median and Mode: Practical: 5

The document describes an algorithm to calculate the mean, median, and mode of an array. It provides code to find the mean by summing all elements and dividing by the array size. It calculates the median by sorting the array and selecting the middle element(s). The mode is found by counting the frequency of each unique element and selecting the most frequent. The code implements these algorithms on a sample array and outputs the mean as 12.2857, median as 14, and mode as 19.

Uploaded by

Basit Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views2 pages

Aim: To Calculate Mean, Median and Mode: Practical: 5

The document describes an algorithm to calculate the mean, median, and mode of an array. It provides code to find the mean by summing all elements and dividing by the array size. It calculates the median by sorting the array and selecting the middle element(s). The mode is found by counting the frequency of each unique element and selecting the most frequent. The code implements these algorithms on a sample array and outputs the mean as 12.2857, median as 14, and mode as 19.

Uploaded by

Basit Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DAA LAB Roll no: 1702688

Practical : 5

Aim: To calculate mean ,median and mode

Algorithm:
MMM( array , size )
1. Calculate sum of all the elements of the array.
2 Divide the sum by size of array
3 Mean = sum/size of array
4 If size is even then
median is arr[size/2]
else
median is (arr[(size-1)/2] + arr[size/2] ) / 2.0;
5 Set counter for each element in array
6 Maximum the count for which value that's the mode.
7 End

Program :
# include <iostream>
using namespace std;
double findMean(int a[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
return (double)sum/(double)n;
}
double findMedian(int a[], int n)
{
if (n % 2 != 0)
return (double)a[n/2];
return (double)(a[(n-1)/2] + a[n/2])/2.0;
}

int main()
{
int a[] = { 1,5,8,14,19,19,20};
int n = sizeof(a)/sizeof(a[0]);
cout << "Mean = " << findMean(a, n) << endl;
cout << "Median = " << findMedian(a, n) << endl;

Page no: 8
DAA LAB Roll no: 1702688

int max=0;
for(int i=0;i<n;i++)
{ if(max<a[i])
max=a[i];
}
int count[max+1]={};
for(int i=0;i<n;i++)
count[a[i]]++;
int mod=0;
for(int i=0;i<=max;i++)
{ if(count[mod]<count[i])
mod=i; }
cout<<"Mode = "<<mod<<endl;
return 0;
}

Output:

Page no: 9

You might also like