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:

include <cmath>
include <iostream>
include <fstream>
include <iomanip>

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 

First of you really should consider using an IDE that will format your code. What you've submitted is very hard to read.

Second here's the error I noticed.

You declare datalenth and pass it to readdata routine. It's getting the value from the first record in your textfile. Which is 10 even though there are only 2 after that one. By changing your text file to this 03 66 10 88 I was able to get an output like this:

Using insertion sort algorithm, the elements are:
10 66 88
The median for the elements entered is:66

The Mean is:54.6667

The Standard Deviation is:32.8363

This can also be acheived by setting datalenth to 3 and not setting it in readdata
I changed the order of the records in the text file so to show the sort working.

Thanks a lot "tinstaafl", what I really wanted to do was to have the program figure out the array lenth itself, but I haven't learned pointers and that stuff yet, so fo now I will leave it as is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.