0

Hello, I started c++, and I recently got an assignment, which is supposed to calculate the sum, variance, mean deviation, and mean in a set. The user can input any number positive or negative for the set. Because it is a set, the person may enter as many numbers as they want. I have succeeded in making the program do addition and mean/average. I need help in coming up with a function which will make a variance, and then square root of that will give mean deviation. Use the following website to get the formula (online algorithm). The problem is that there are some things that i don't know what it stands for or what it does: http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm
any help is appreciated.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


void main()
{
    double value = 0;
    double sum = 0;
    int count = 0;
	double cnt = 0;
	double mean = 0;

    while(value != -1)
{
    cout << "Enter a value: ";
    cin >> value;
    if (value != -1)
    {
        cnt++;
        sum += value;
    }
   
}

	
    cout << "Number:  " << cnt << endl;
    cout << "Sum:  " << sum << endl;
	mean = sum/cnt;
    cout << fixed << showpoint << setprecision(2) << "Average: " << mean;
    fflush(stdin);
    cout << "\n press enter to end" ;
    cin.get();

}

double deviation(double mean, int cnt, double sum)
{

}
4
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by mike_2000_17
0

>> The problem is that there are some things that i don't know what it stands for or what it does


Like what? If you don't understand the formulas, then a math/statistics forum might be a better place to ask. Google "standard deviation example" and you'll get thousands of hits. I generally DO NOT go with wikipedia. This one isn't bad, and there are others. Get out a calculator and pencil and peper and follow the example. Put down the C++ part until you get the math part under control.

http://ltcconline.net/greenl/courses/201/descstat/mean.htm

0

I had to do this very exercise last month. However, the solution looked nothing like what you have.

The values of the set were input into a vector using the push_back() function. Then the stated statistics were found using the sum() function and loops.

However, Vernon is absolutely correct. If you don't understand statistics (mean, variance, etc.), then you can't write a program to compute them. The author of the book I am using has that as the very first rule: You can't program what you don't understand. First, understand the problem.

0

From the link you gave, first, these are recursion formulas, meaning that they are calculated as the data comes in, so all those calculations should be within that value-entering loop you have (or you can loop through the data in another function and apply the formulas). Since there aren't that many symbols to understand I can give you a quick definition of them:
x_n : is the new element that was just given by the user.
x(bar)_n-1 : is the average or mean computed up to and including the previous element (x_n-1)
x(bar)_n : is the average or mean that includes the new element x_n
n : is the number of elements inputted so far (cnt), including the new element x_n
s2_n : is the sample variance (s_n being the mean deviation) that includes the new element x_n

That's about it. The sigma one is for a population instead of sample, that's just a weird statistical technicality. You will notice that the formula for the sample variance will be undefined for the first element (n-1 == 0), but that's alright because the variance of a set containing only one element has to be zero (the element does not deviate from itself!). So both the variance and mean deviation start with a value of zero, and you start counting them with those formulas from the second element and on.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.