hello
I am writing a program that needs to calculate the average of numbers and the standard deviation of numbers, the numbers are input by the user.The program takes the numbers that the user enters and outputs them to a text file, then the program reads in the numbers and calculates the average of them. then the program closes the file. It then reopens and calculates the standard deviation outputs the number and then closes.
the standard deviation is calculated as:
sqrt((n1-avg.)^2+(n2-avg)^2+(n3-avg)^2+.../howmanynumbers)
My question is how would i get the values n1,n2,n3 and so forth if I entered the numbers as the variable 'numbers'. is there a way to just read one number at a time? This is what I got so far
Code:#include<iostream> #include<fstream> #include<cstdlib> #include<cmath> using namespace std; void asknumbers(ofstream& myfile,int& howmanynumbers,double& numbers); void readnumbers(ifstream& outfile, double numbers, double& total,int howmanynumbers,double& average); void standarddev(ifstream& outfile, double numbers,double average,double& standardd, int howmanynumbers); int main() { ofstream myfile; ifstream outfile; int howmanynumbers; double numbers,total,average,standardd; char repeat; do { myfile.open("numbers_file.txt",ios::trunc); //opens and deletes everything from before myfile.close(); //closes file asknumbers(myfile,howmanynumbers,numbers); //function for asking numbers readnumbers(outfile,numbers,total,howmanynumbers,average); //function for total and average standarddev(outfile,numbers,average,standardd,howmanynumbers);//function for standard deviation cout<<"Do you want to repeat: "; cin>>repeat; system("cls"); }while(repeat=='y'||repeat=='Y'); //end first do loop system("pause"); return 0; } void asknumbers(ofstream& myfile,int& howmanynumbers,double& numbers) { cout<<"how many numbers do you want to input:"; //asks how many number i want to put in cin>>howmanynumbers; int numbercounter; numbercounter=howmanynumbers; do //asks for the numbers { cout<<"enter the numbers:"; cin>>numbers; myfile.open ("numbers_file.txt",ios::app); //writes to file called numbers_file.txt myfile<<numbers<<endl; //writes numbers to the file myfile.close(); //closes file numbercounter--; //counts down how many numbers put in }while(numbercounter>=1); //ends asking for numbers } void readnumbers(ifstream& outfile,double numbers, double& total,int howmanynumbers,double& average) { total=0; outfile.open("numbers_file.txt"); //opens file for(int x=howmanynumbers;x>0;x--) { outfile>>numbers; //reads numbers total+=numbers; //calculates total from numbers input average=total/howmanynumbers; } cout<<"The total of the numbers is: "<<total<<endl; cout<<"The average of the "<<howmanynumbers<<" numbers is: "<<average<<endl; } void standarddev(ifstream& outfile, double numbers,double average,double& standardd,int howmanynumbers) { outfile.open("numbers_file.txt"); //opens file for(int x=howmanynumbers;x>0;x--) { standardd=sqrt(pow((numbers-average),2)/average); } cout<<"The standard deviation of those numbers is: "<<standardd<<endl; }



LinkBack URL
About LinkBacks


