hello
I am writing a program that will take numbers entered by the user and calculate the average and standard deviation of those numbers. the program takes the numbers entered and outputs them to a text file then reads them and calculates them.
I am stumped on how to calculate the standard deviation with the way I enter the numbers. could I please get some hints or something about it.
basically I dont know how to get the 'numbers' variable for the standarddev function. the program returns '-1.#ind' for the standard deviation, which i know is wrong.
the standard deviation formula is the square root of the average of
(n1-avg)^2+(n2-avg)^2+(n3-avg)^2+...
where n1=the first number i entered,n2=the second number,n3=the third number,and so on.
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,double& numbers1);
int main()
{
ofstream myfile;
ifstream outfile;
int howmanynumbers;
double numbers,total,average,standardd,numbers1;
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,numbers1);//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:"<<endl;
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;
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,double& numbers1)
{
outfile.open("numbers_file.txt"); //opens file
for(int x=howmanynumbers;x>0;x--)
{
numbers1+= pow((numbers-average),2);
}
standardd=sqrt(numbers1/howmanynumbers); //sqrt of average of all things
cout<<"The standard deviation of those numbers is: "<<standardd<<endl;
}