# Thread: what did I do wrong?

1. ## what did I do wrong?

Boy, I sure would love it if someone could help me figure this out. My professor has created some code that's a loop for enter numbers. I need to add code to make the program figure averages and standard deviation. I've added the formulas as I believe they should be, and the program runs, but when I test the program with some small numbers I can do in my head, the average and deviation is wrong. Here's the code, stripped of pretty formatting....

Code:
```// Program Abstract:       the purpose of this program is to determine the average and standard deviation of input numbers
//   Input Required:       list of numbers
//   Output Desired:       program will output the list of numbers,
//                         average and standard deviation of each number
// =============================================================================

#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>
#include <math.h>

//==============================================================================

// variable declarations

// alphabetized variable dictionary
// =========================================================================
// d is data
// i is spacing
// m is average
// n is count
// s is standard deviation
// x is sum

int main() {
double d, i, m, n, s, x;
double average, stddev, sum, sumsqr;

ofstream fout;

fout.open ("prompt.txt");

// introduce the program to determine average and
// standard deviation of a list of numbers

//==============================================================================
cout << setw (61) << "Program for average and standard deviation";

// press any key to continue
getchar();

// run program until user is done
// loop for data entry, calculates sum and counter, ends after last while
do
{
// setting beginning numbers to 0
n = 0;
x = 0;
sumsqr = 0;

// loop B check and correct data entry
do
{
clrscr ();

// prompt the user for the next data
cout << setw (61) << "Enter next data, enter -1 to end data stream";
cout << ": ";
cin >> d;

do
{
clrscr ();

cout << setw (37) << "Is " << d << " correct?";
cout << endl << endl << endl;

// query the user for correctness of input data
cout << setw (55) << "Enter y for yes or n for no: ";

// input response to query
}

++n ;
x += d;
sumsqr += d * d;

// record verified data in output textfile
fout << d << endl;
}
}
while (d != -1);

// calculate average
if ( n > 0 )
m = x / n;
else
m = 0 ;

// press any key to continue
getchar();

// calculate standard deviation
if ( n > 1 )
s = sqrt ((n * sumsqr - x * x) / (n * (n - 1))) ;
else
s = 0;

clrscr ();

cout << setw (50) <<  "The average is: " << m;

cout << setw (50) <<  "The standard deviation is: " << s;

// press any key to continue
getchar();

do
{
clrscr ();

// query user for another input data
cout << setw (55) << "Is there another set of data?";

cout << setw (55) << "Enter y for yes or n for no: ";
}
}

fout.close ();
return 0;
}```

2. Originally Posted by cherigee
Boy, I sure would love it if someone could help me figure this out.
Here's how you can figure it out:

Put some output statements close to your calculations to see what's happening.

Like this:
Code:
```      if(reply=='y') {

++n ;
x += d;
sumsqr += d * d;
cout << "x = " << x
<< ", sumsqr = " << sumsqr
<< ", n = " << n << endl;

// record verified data in output textfile
fout << d << endl;
}
}```
and this
Code:
```        // calculate average
cout << "x = " << x
<< ", sumsqr = " << sumsqr
<< ", n = " << n << endl;

if ( n > 0 )
m = x / n;
else
m = 0 ;```
Regards,

Dave