Well...
If you want mean then - what Geo said.
If you want median - let me explain it to you for any number of numbers, not just 3.
Sort the numbers in ascending or decending order. Then pick the "middle" element.
If it is an even no. of numbers then there are two medians (the two middle elements).
Code:
#include<iostream>
using namespace std;
void main()
{
int size, median, med1, med2, temp;
cout<<"How many numbers?";
cin>>size;
const int S = size;
[s] int *array=new int ;
cout<<"Enter the list of numbers: ";
for(i=0; i<size; i++)
cin>>array[i];
//Now to sort them (using simple bubble sort)
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
{
if(array[i]>=array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
//For odd no. of elements (like 3,7,11 etc)
if (!size%2)
{
median = array[(size+1)/2 - 1];
cout<<endl <<"Median is :" <<median;
}
//For even no. of elements
else
{
med1 = array[size/2];
med2 = array[size/2 - 1];
cout<<endl <<"Median 1 is: " <<med1 <<endl;
cout<<"Median 2 is: " <<med2 <<endl;
}
}
|