Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Competitive Professional Skills - III

(CODE :18CSC307L)

Name : Palak Khurana


Reg No. : RA1911 003030437
Branch : CSE-I

Ques. – Write a program to implement Mean , Mode and Median functions.

PROGRAM:

#include <bits/stdc++.h>
using namespace std;
float mean(float *arr, int n){
float sum = 0;
for(int i = 0;i < n; i++)
sum += arr[i];

return sum/n;
}

float median(float *arr, int n){

sort(arr, arr + n);

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

return arr[n/2];
}

float mode( float *arr, int n){

sort(arr, arr + n);


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;
}
}

if (count > max_count)


{
max_count = count;
res = arr[n - 1];
}

return res;
}

int main(){
int n;
float *arr=new float[n];
cout<<"Enter the size of array : ";
cin>>n;
cout<<"Enter the elements of array : ";
for(int i = 0; i < n; i++)
cin>>arr[i];

cout<<"Mean = "<<mean(arr, n)<<endl;


cout<<"Median = "<<median(arr, n)<<endl;
cout<<"Mode = "<<mode(arr, n)<<endl;

return 0;
}

OUTPUT:

You might also like