I thought I can make life a little easier in data statistics by making a small program which returns the results of sampling distribution of the mean (with standard error). It does this part successfully but in an attempt to return the z-score by using the formula I found here, it returns -1#IND. My interpretation of that formula is:
((1 / (sqrt(2 * pi) * stdev)) * pow(e, (normalpow))
where
double normalpow = -0.5 * ((mean - popmean) * (mean-popmean) / stdev)
I did a little more investigating and found that (mean - popmean) * (mean - popmean) was evaluating to 0 no matter what. How can I get around this problem of normalpow evaluating to 0.
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
double number ;
double mean ;
double popmean ;
double stdev ;
double square = 2;
double e = 2.71828182845904523536;
double pi = 3.14159265358979323846;
double normalpow = -0.5*((mean-popmean)*(mean-popmean)/stdev);
int main ()
{
string continuer ;
do
{
cout << "Enter Sample Mean: " << endl;
cin >> mean;
cout << "Enter Population Mean: " << endl;
cin >> popmean;
cout << "Enter Standard Deviation: " << endl;
cin >> stdev;
cout << "Enter Sample Size: " << endl;
cin >> number;
if (stdev == 0)
cout << ((mean-popmean)/(number))<< endl;
else
{
cout << ((mean-popmean)/((stdev)/(sqrt(number))))<< endl;
cout << ((1/(sqrt(2*pi)*stdev))*pow(e, (normalpow)))<< endl;
}
cout << "If you want to continue, Press Y" << endl ;
cin >> continuer;
} while (continuer == "Y" || continuer == "y") ;
return 0;
}
These are all variables with static storage duration, so those without explicit initialisers are initialised to 0.
Hence
mean,popmeanandstdevare all 0 whennormalpowis initialised, and that initialisation results inwhich gives a NaN.
You never change
normalpowafterwards, so any computation involving it results in a NaN.