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

10/7/2015 Mean, Median and Mode c++ | codingmadesimple

codingmadesimple

C++ coding covering basics, data structures, operating systems and computer graphics

30JUN / 2012

Mean, Median and Mode c++

#include<iostream.h>
#include<conio.h>
int main()
{
int array[30],sum, n,median1,median2, mode,count[30];
int pos1,pos2;
sum=0;
float mean=0.0;
cout<<“enter n”;
cin>>n;
for(int a=0;a<n;a++)
{
cout<<“enter”;
cin>>array[a];
count[a]=0;
}
for(int b=0;b<n;b++)
{
sum=sum+array[b];
}
cout<<“sum is”<<sum;
int small,pos,temp;

for(int i=0;i<n‑1;i++)
{
small=array[i];
pos=i;
for(int j=i+1;j<n;j++)
{
if(small>array[j])
{
small=array[j];
pos=j;

}
https://codingmadesimple.wordpress.com/2012/06/30/mean­median­and­mode­c/ 1/3
10/7/2015 Mean, Median and Mode c++ | codingmadesimple

}
}
temp=array[pos];
array[pos]=array[i];
array[i]=temp;
}
mean=sum/n;
cout<<“\n mean is”<<mean;
if(n%2!=0)
{
pos1=pos2=(n+1)/2;
}
else
{
pos1=n/2;
pos2=n/2 +1;
}
median1=array[pos1];
median2=array[pos2];
cout<<” median is “<<median1<<“\t”<<median2;
for(int c=0;c<n; c++)
{
for(int d=0;d<n;d++)
{
if(array[c]==array[d])
{
count[c]++;
}
}
}
int modepos,max=count[0];
for(int e=1;e<n;e++)
{
if(max<count[e])
{
max=count[e];
modepos=e;
}
}
mode=array[modepos];
cout<<“mode is”<<mode;
getch();
return 0;
}

Posted by codingmadesimple in Basic C++

Permalink  2 Comments

https://codingmadesimple.wordpress.com/2012/06/30/mean­median­and­mode­c/ 2/3
10/7/2015 Mean, Median and Mode c++ | codingmadesimple

2 responses to “Mean, Median and Mode c++”

Facebook Chat
August 10, 2012 at 11:12 pm
Just thought i would comment and say neat design, did you code it yourself? Looks great.

REPLY ↓
Facebook Chat
August 15, 2012 at 10:31 am
Hi, i must say fantastic blog you have, i stumbled across it in AOL. Does you get much
traffic?

REPLY ↓

Blog at WordPress.com. | The Pink Touch 2 Theme.

https://codingmadesimple.wordpress.com/2012/06/30/mean­median­and­mode­c/ 3/3

You might also like