Need help with the C++ programming. I am trying to compute the count of values, the average, and the standard deviation of the input given by user. My output is correct except for the standard deviation. My code is below:

Thanks

#include <iostream>
#include <string>
#include <cmath>

using namespace std;


int main()
{


        bool more = true;
        int count = 0;
        double sum = 0;




                cout << " Enter a value: (CTRL-D to quit): ";
                float num = 0.0;
                cin >> num;
                while (true)
                {
                ++count;
                cout << count << endl;
                sum += num;
                cout << " Enter a value: (CTRL-D to quit): ";
                cin >> num;
                        if  (cin.eof())

                        {
                                break;
                        }
                }


                double avg = 0;
                avg = sum / count;

                double s_dev = 0;
                s_dev = sqrt( (( pow(sum,2.0)) -(( 1.0/count) * (pow(sum,2.0))))/ (count -1.0));
                cout << endl << endl;
                cout << " There are " << count << " values. " << endl;
                cout << " The average is:  " << avg << endl;
                cout << " The standard deviation is:  " << s_dev << endl;

        return 0;
}

<< moderator edit: added code tags: [code][/code] >>

Dani AI

Generated

Short, practical note about the common pitfalls and a robust fix.

The usual causes of a wrong standard deviation are (a) using the square of the sum instead of the sum of squares and (b) catastrophic cancellation when values are large or close together. was correct to point out that you must track squared information; an even better approach for one-pass input is Welford’s algorithm, which keeps a running mean and a running sum-of-squared-differences for numerical stability. ’s formatting suggestions make the code easier to read; combine that readability with Welford for a safe, simple solution.

Here is a compact, complete C++ example implementing Welford (one pass, stable):

#include <iostream>
#include <cmath>

int main() {
    double x;
    long long n = 0;
    double mean = 0.0;
    double M2 = 0.0; // sum of squared differences

    while (std::cin >> x) {
        ++n;
        double delta = x - mean;
        mean += delta / n;
        M2 += delta * (x - mean);
    }

    if (n < 2) {
        std::cout << "Need at least two values\n";
        return 0;
    }

    double variance_sample = M2 / (n - 1); // use /n for population variance
    double sd = std::sqrt(std::max(0.0, variance_sample));
    std::cout << "count=" << n << " mean=" << mean << " sd=" << sd << "\n";
}

Practical tips: use double (not float), prefer while (cin >> x) over manual eof checks, guard n < 2 before dividing, and take sqrt(max(0.0, var)) to avoid tiny negative values from roundoff. Choose /n (population) or / (n-1) (sample) explicitly depending on the statistic you want.

Recommended Answers

All 3 Replies

You aren't using the std dev formula correctly.

See http://davidmlane.com/hyperstat/A16252.html

Also, here is an update to your code. Notice all the variables are declared at the top of main. Notice how the code is indented. The program is easier to read now.

Take care,
Bruce

#include <iostream>
#include <string>
#include <cmath>

using namespace std;


int main()
{

	bool	more	= true;
    int		count	= 0;
    double	sum		= 0;
    
	float	num		= 0.0;
	double	avg		= 0;
 	double	s_dev	= 0;
 
    
	cout << " Enter a value: (CTRL-D to quit): ";
    
	cin >> num;
    
	while (true)
    
	{
    
		++count;
        cout << count << endl;
        
        sum += num;
        
        cout << " Enter a value: (CTRL-D to quit): ";
        cin >> num;
 
		if  (cin.eof())
		{
			break;
		}
        
	}


    
   
	avg = sum / count;

	s_dev = sqrt((( pow(sum,2.0)) -(( 1.0/count) * (pow(sum,2.0)))) / (count - 1.0)
		);
    
	cout << endl << endl;
    
	cout << " There are " << count << " values. " << endl;
    
	cout << " The average is:  " << avg << endl;
    
	cout << " The standard deviation is:  " << s_dev << endl;

 
	return 0;

}

OK. So the tab didn't copy real well, but you get the idea.

You have a bug in your formula inputs.
To compute standard deviation, you must
accumulate the sum and the sum of the squares both.
Then compute the s.d. using the square-root of
the difference between the average of the squares and
the square of the average. It's simple, but you need
to add some variables to your program to accumulate
the average of the entries and the square of the entries
to get what you want. By the way, you do not need to
use pow(). Just accumulate num and num*num.

Hope this helps!

thanks, for the help. I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.