1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#define SIZE 500
#include <iostream.h>
#include <fstream.h>
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
int main()
{
int i,n,choice,isum; //i is the counter
{
ifstream fin ( "input.txt" );
if (fin)
{ while (!fin.eof())
{ fin >> n[i];
isum+=n[i];
i++;
}
fin.close();
}
else
cout << "Could not open file\n";
return 0;
}
float array[SIZE],mean,median,mode;
// use a while not eof to read from text file and count how many numbers
do
{
cout << "\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n4.Exit";
cin >> choice;
switch(choice)
{
case 1: mean=mean_function(array,n);
cout << "\n\tMean = %f\n",mean;
break;
case 2: median=median_function(array,n);
cout << "\n\tMedian = %f\n",median;
break;
case 3: mode=mode_function(array,n);
cout << "\n\tMode = %f\n",mode;
break;
case 4: break;
default:cout << "Wrong Option";
break;
}
}while(choice!=4);
}
/***************************************************************
Function Name : mean_function
****************************************************************/
float mean_function(float array[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+array[i];
return (sum/n);
}
/***************************************************************
Function Name : median_function
Return Type : Float
****************************************************************/
float median_function(float a[],int n)
{
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}
float mode_function(float a[],int n)
{
return (3*median_function(a,n)-2*mean_function(a,n));
}
|