ok i posted in a exiting thread before as my question was relevant to that thread. Now im starting this thread so i wont hog the other person's thread.
so far with the help of various people like, hk_mp5kpdw andyhunter, and 7stud ive managed to get a little further on my program but im still having some confusing results.
here is the code
the text file is as followsCode:#include <iostream> #include <fstream> #include <cmath> using namespace std; const int FILENAMELEN =20, ARRAYLEN =20; class array { public: int readarray(); array0(); float finddev(); float findaverage(); float writeresults(); private: int j; int i; int count; int numbers[20]; float sum; float average; float var; float std; float sqrofsums; float sumofsqrs; char values[20]; char filename[FILENAMELEN]; ifstream inflie; //int currentChar; }; array::array0() { count =0; } int array::readarray() { char filename[FILENAMELEN]; char inchar; ifstream infile; //currentChar = 0; count = 0; cout<<"Enter filename->" <<'\t'; cin>> filename; /* for (i=0; i<20; i++) { numbers[i]=0; }*/ //openfile infile.open(filename); //if you cant open the file, print error message if (!infile) { cerr<< "Cannot open " <<filename << endl; exit(1); } //Read the file while(!infile.eof() && count < 20) { //something is going on right here? getting mixed output infile.getline(values,20); cout<<values<<endl; inchar =infile.get(); cout <<inchar<<endl; { if( infile >> numbers[count]) ++count; else break; } } infile.close(); return(0); } //find the average float array::findaverage() { sum = 0.0; average = 0.0; for (j=0;j<count;j++) { sum+=numbers[j]; } average =sum/j; return average; } //find the std float array::finddev() { sum = 0.0; var = 0.0; std = 0.0; sqrofsums = 0.0; sumofsqrs = 0.0; //sum of the numbers squared for (j=0;j<count; j++) { sum+=numbers[j]; } sqrofsums =(float)pow(sum,2); //sum of squared numbers for(j=0;j<count; j++) { sumofsqrs+=numbers[j] *numbers[j]; } //variance var =(sumofsqrs -(sqrofsums/j)); var = (var/j-1); std = (float)sqrt(var); return std; } //display the results float array::writeresults() { cout << "The average is: " << findaverage(); cout << endl; cout << "The standard deviation is: " << finddev(); cout << endl; return 0; } int main() { int numbers[20]; int *pn=numbers; array myarray; myarray.readarray(); myarray.writeresults(); return 0; }
array1.txt
the result i get isCode:5 4 3 2 6 5
Code:Enter filename-> array1.txt 5 4 2 5 The average is: 4.5 The standard deviation is: 1.11803 Press any key to continue



LinkBack URL
About LinkBacks


