Here is what I want this program to do:
Be able to calculate mean, standard deviation, and median. Data should be sorted using insert sort first though.
The program must be able to accept input from an input file and print a report to an output file. These files are to be named by the user at runtime.
I first wrote this program so that it would ask for user to enter the numbers, instead of reading from a file. And output to the screen of course. It worked just as it is supposed to, so I know that the algorithm for the calculations is not wrong. The problem started when I modified the program to read from a file, and print to a file. I am almost sure that the problem is that the program cannot read or store in the arrays, properly. It throws(print) out bunch of nonsense. You can try it out using the attached txt file to see what I mean. Here is my source code:
using namespace std;
const int Max_Size = 200;
void print (ofstream& outdata, double list[], int lenth);
void insertionSort (ofstream& outdata, double list[], int lenth);
void median (ofstream& outdata, double list[], int lenth);
void mean (ofstream& outdata, double list[], int lenth);
double standard_deviation(ofstream& outdata, double list[], int lenth);
void readdata(ifstream& indata, double list[], int& lenth, bool& lenthok);
int main()
{
double dataarray[Max_Size];
int datalenth;
bool lenthisok;
ifstream indata;
ofstream outdata;
char inputfile[20];
char outputfile[20];
cout<<"Please enter the input file name:"<<endl;
cin>>inputfile;
indata.open(inputfile);
if(!indata)
{
cout<<"Cannot open the input file."<<endl;
return 1;
}
cout<<"Please enter the output file name: "<<endl;
cin>>outputfile;
outdata.open(outputfile);
{
readdata(indata, dataarray, datalenth, lenthisok);
if (lenthisok)
insertionSort(outdata, dataarray, datalenth);
else
cout<<"Lenth of the secret code must be <="<<Max_Size<<endl;
print (outdata, dataarray, datalenth);
median(outdata, dataarray, datalenth);
mean(outdata, dataarray, datalenth);
standard_deviation(outdata, dataarray, datalenth);
}
indata.close();
outdata.close();
return 0;
}
void readdata(ifstream& indata, double list[], int& lenth, bool& lenthok)
{
int i;
lenthok = true;
indata>>lenth;
if (lenth>Max_Size)
{
lenthok = false;
return;
}
for (i=0; i<lenth; i++)
indata>>list[i];
}
void insertionSort(ofstream& outdata, double list[], int lenth)
{
int firstoutoforder;
int location;
double temp;
for (firstoutoforder=1; firstoutoforder < lenth; firstoutoforder++)
if (list[firstoutoforder] < list[firstoutoforder-1])
{
temp = list[firstoutoforder];
location = firstoutoforder;
do
{
list[location] = list[location-1];
location--;
}
while (location > 0 && list[location-1] > temp);
list[location] = temp;
}
}
void print (ofstream& outdata, double list[], int lenth)
{
int i;
outdata<<"Using insertion sort algorithm, the elements are:"<<endl;
for (i=0; i<lenth; i++)
outdata<<list[i]<<" ";
}
void median (ofstream& outdata, double list[], int lenth)
{
int middle;
double median;
middle = lenth / 2;
if (lenth % 2==0)
{
median = (list[middle-1] + list[middle]) / 2;
}
else
median = (list[middle]);
outdata<<"\nThe median for the elements entered is:"<<median<<endl;
}
void mean (ofstream& outdata, double list[], int lenth)
{
double sum = 0;
double average = 0;
int i;
for (i=0; i<lenth; i++)
sum = sum+list[i];
average = sum/lenth;
outdata<<"\nThe Mean is:"<<average<<endl;
}
double standard_deviation(ofstream& outdata, double list[], int lenth)
{
double sum = 0;
double average = 0;
double sq_diff_sum = 0;
double variance = 0;
double diff = 0;
int i;
double deviation = 0;
for (i=0; i<lenth; i++)
sum = sum+list[i];
average = sum/lenth;
for (i=0; i<lenth; i++)
{
diff = list[i] - average;
sq_diff_sum += diff * diff;
}
variance = sq_diff_sum/lenth;
deviation = sqrt(variance);
outdata<<"\nThe Standard Deviation is:"<<deviation<<endl;
return (deviation);
}
// end of code