here is the whole program...
Code:
//Name: Joseph Valenzuela
//Date: March 11, 2003
//Purpose: To design and implement my own program for project1.
#include <iostream.h> // For output to the screen and input from user
#include <fstream.h> // To allow input and output streams
#include <stdlib.h> // Contains exit function
#include <math.h> // Contains square root function
void compute(double& sum, double& sum_of_squares);
//Precondition:None
//Postcondition:To compute the sum and sum of squares of the numbers in the input file
double mean(int xnum, double sum);
//Precondition:Requires the amount of numbers and sum of those numbers
//Postcondition:Calculates the mean of the numbers in the input file and returns the value
double variance(double sum_of_squares, int xnum, double avg);
//Precondition:Requires sum of the squares, the amount of numbers and the mean of the numbers
//Postcondition:Calculates the variance of the numbers in the input file and returns the value
double deviation(double var);
//Precondition:Requires the variance
//Postcondition:Calculates the standard deviation of the numbers in the input file and returns the value
double median (int xnum);
//Precondition:Requires amount of numbers in input file
//Postcondition:Calculates the median of the numbers in the input file, and returns the value
void main()
{
double sum,sum_of_squares,avg,var,dev,med; //Declares variables
int xnum;
cout <<"\nWELCOME TO THE BASIC STATISTICS PACKAGE!\n";
cout << "Enter amount of numbers to be evaluated: ";
cin >> xnum;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
compute(sum,sum_of_squares);
avg = mean(xnum, sum);
cout << "The Mean price of the numbers is $" << avg << endl;
med = median(xnum);
cout <<"The median price of the numbers is $" << med << endl;
var = variance(sum_of_squares,xnum,avg);
cout <<"The Variance price of the numbers is $" << var << endl;
dev = deviation(var);
cout <<"The Standard Deviation price of the numbers is $" << dev << endl;
}
//Precondition:None
//Postcondition:To compute the sum and sum of squares of the numbers in the input file
void compute(double& sum, double& sum_of_squares)
{
sum = sum_of_squares = 0;
double in_num=0;
ifstream input_data;
input_data.open("stats.dat");
if (input_data.fail())
{
cout <<"Input file could not be opened.\n";
exit(1);
}
input_data >> in_num;
while (!input_data.eof())
{
sum += in_num;
sum_of_squares += in_num*in_num;
input_data >> in_num;
};
input_data.close();
}
//Precondition:Requires the amount of numbers and sum of those numbers
//Postcondition:Calculates the mean of the numbers in the input file
double mean(int xnum, double sum)
{
double avg=0;
avg = sum/xnum;
return(avg);
}
//Precondition:Requires sum of the squares, the amount of numbers and the mean of the numbers
//Postcondition:Calculates the variance of the numbers in the input file
double variance(double sum_of_squares, int xnum, double avg)
{
double var=0;
var = (sum_of_squares/xnum) - (avg*avg);
return(var);
}
//Precondition:Requires the variance
//Postcondition:Calculates the standard deviation of the numbers in the input file
double deviation(double var)
{
double dev;
dev = sqrt(var);
return(dev);
}
//Precondition:Requires amount of numbers in input file
//Postcondition:Calculates the median of the numbers in the input file, and returns the value
double median (int xnum)
{
double median,med_pos,count=0, temp;
ifstream input_data;
input_data.open("stats.dat");
if (input_data.fail())
{
cout <<"Input file could not be opened.\n";
exit(1);
med_pos = xnum/2;
}
if ((xnum%2)==0)
{
median=0;
while (input_data >> temp)
{
count++;
if((count==med_pos)||(count==med_pos+1))
median = median + temp;
}
median = median/2;
}
else if ((xnum%2)!=0)
{
median=0;
while (input_data >> temp)
{
count++;
if(count==med_pos+1)
median = median + temp;
}
median = median/2;
}
return(median);
}