0

Hello everyone I am required to create a program that reads in an input file containing digits and then finds the standard deviation using the following method:

sqrt(( x1 - mu )^2 + ( x2 - mu )^2 + ( x3 - mu )^2 + ( x4 - mu )^2)/mu

The x's are equal to the digits read in, and mu is equal to the mean. I am having trouble doing this because I don't know how to set up different variables (x1, x2, x3, x4) for the values that are read in from the input file within my while loop. Also it is important to note that we are supposed to read in the first digit and then every third digit after that. This is what I have so far:

    fin.open(FileName.c_str());
    if (fin.fail())
    {
        cout <<"Bad file name or location.\n" ;
        exit(0);
    }
    fin >> X;
    first_score = X;
    Counter = 0, Sum=0;
    while (!fin.eof() )
    {   
        Counter++;
        if (Counter%3==0)
        {
            fin >> X;
            Sum += X;
            Counter++;
            Counter2 ++ ;
            Avg = (Sum+first_score)/(Counter2+1);
            deviation = pow((X-Avg),2);
            sum_of_deviations += deviation;
        }
        fin >> Score;
    }
    quotient_of_deviations = sum_of_deviations/Counter2;
    standard_dev2 = sqrt(quotient_of_deviations);
    fin.close();

I know this code is logically incorrect because I am subtracting a different mean from every x value. Does someone know how I could assign the X within the while loop to a new variable each time the while loop is ran? If I can do this, I will then be able to subtract each x value by the same mean outside of the loop. I hope I explained that good enough so that you guys can understand my problem. If not I will be happy to explain more. Thanks in advance for your time.

2 Answers2

1

The problem with this is that you need to know the value of the average, but you won't know this until you have read in all the data. You are trying to calculate the deviation based on the average of the terms read in so far. This is incorrect

You should use the sqrt(Sum(x^2) /n - (sum(n) /n)^2) formula for the standard deviation.

Calculate the two sums in the loop, and then divide by n and complete the calculation at the end. Then you don't need to assign a new variable each time.

James K
  • 3,692
  • 1
  • 28
  • 36
  • thanks for the quick response.. however I don't think I understand the code you posted. Is there any other way you could explain it better? – coolioschmoolio Oct 20 '13 at 01:35
1

If you don't want to use arrays then you might have to read file multiple times.

int counter = 0;
int sum1=0;
ifstream fin,fin2;   //fin and fin2 to read the file each time.
fin.open("myfile.txt");  //opening a file to read it.



while (!fin.eof() )   //reading a file
{
   fin>>X;
   sum1  = sum1+X;    //adding all the numbers in the file
   counter++;      //counting number of items in the file

}

fin.close()
//Now first calculate mean
int mean=0;
mean = sum1/counter;   //calculating the mean

//now calculate sum of squares of difference of each term and mean
int sum2=0;
fin2.open("myfile.txt");     //again opening the file with fin2

while (!fin2.eof() )   //again reading the file
{
   fin2>>Y;
   sum2  = sum2+ pow(Y-mean,2);     

}

fin2.close()


 //finally standard deviation

 double sd=0;

 sd = sqrt(sum2/mean);    //calculating standard deviation
java_doctor_101
  • 3,287
  • 4
  • 46
  • 78
  • ahhh while your method probably works, our class has not touched arrays yet so I have no idea how they work so I should probably steer clear of them. Is there a method that works without using arrays? Thanks for your time, sorry that your method wasn't what I was looking for. – coolioschmoolio Oct 20 '13 at 01:51
  • It is supposed to handle an unknown amount of digits since i am reading them in from an input file. – coolioschmoolio Oct 20 '13 at 01:54
  • I have modified the code, in case you can't use arrays you will have to read file multiple times since without arrays you can't store these values anywhere and the only option left is to read these values again from the file. Hope this helps.. – java_doctor_101 Oct 20 '13 at 02:02
  • Thank you so much for your help.. I am looking it over now trying to make sense of it. I'll let you know if I figure it out.. Thanks again! – coolioschmoolio Oct 20 '13 at 02:08
  • What does this line mean: FILE* fin,fin2; – coolioschmoolio Oct 20 '13 at 02:09
  • There you go, I presume you aren't familiar with pointers yet. I have used ifstream instead of File* (called file pointer). fin and fin2 are two ifstream type variables used to read files each time. – java_doctor_101 Oct 20 '13 at 02:18
  • perfect thanks so much nitinsh99 you are a boss! I figured it out now that you helped me. ++rep – coolioschmoolio Oct 20 '13 at 02:38