I wrote a small C++ program to calculate the mean and standard deviation of a given data set. Here it is:

#include <iostream>
#include <cmath>
using namespace std;
int main (int argc, char* argv[])
{
int number_of_items;
cout << "How many pieces of data are in the list? ";
cin >> number_of_items;
float list[number_of_items];
// get all the data items
cout << "Please enter the data:" << endl;
int i;
for (i = 1; i <= number_of_items; i++)
{
cout << i << ". "; cin >> list[i];
}
// find the mean (average) of the data
float xbar = 0;
for (i = 0; i <= number_of_items; i++)
xbar = xbar + list[i];
xbar = xbar / number_of_items;
cout << "The mean (average) is " << xbar << "." << endl;
// find the standard deviation
float numerator = 0;
float denominator = number_of_items;
for (i = 1; i <= number_of_items; i++)
numerator = numerator + pow((list[i] - xbar), 2);
float standard_deviation = sqrt (numerator/denominator);
cout << "The standard deviation for the given data is "
<< standard_deviation << "." << endl;
return 0;
}

Like this:

LikeLoading...

Related

This entry was posted on Saturday, January 2nd, 2010 at 12:14 PM and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Why ask the user for the number of pieces of data? Why not just tell them to enter the data with spaces or commas or whatever. Then split the string on that token, and you will know how many pieces of data were in the set.

This is an old post and I’m not here to bash your code — just to point in a new direction. I see myself through the time machine of your code here ðŸ™‚
On second thought, I hope your blog is/you are still alive.

This is really a C program with C++ includes — there is no need for C++ here. To be more accurate, this should be a function (in C, or a class in C++) that receives two arguments: number_of_items as an int and list[] as an array (in C, or could just reference the data from somewhere else, or in C++ get passed an object that contains the data already, or… the possibilities are endless) and returns an array (or something similar) of the values, or writes it somewhere and returns a location/pointer/filename to it.

If it were re-written this way it could be used in any number of other programs as a basic math utility — though, of course, good libraries which already include similar functions exist (so go harvest them for great ideas!).

Things to watch out for are garbage data and clear end-of-file/input signals. For this to really be usable code you need to do some type checking on the data to make sure you’re actually getting numbers instead of, say, image data, text of an email or something that will intentionally overrun your buffer and do Bad Things to your system.

Read some code from the Boost library, Qt4, or something like R (an open source scientific math library) to get some ideas how others have handled the things outlined above to learn more. It can also be useful to re-write this in a completely different language like Lisp (or maybe Python — though I personally feel Python’s approach is too similar to C to be useful for this purpose) and then re-write it from scratch again in C/C++ and see what you do differently.

Hi there,
There is a small bug in your function.
for (i = 1; i <= number_of_items; i++)
numerator = numerator + pow((list[i] – xbar), 2); to caluclate the standard deviation. It should be i = 0; i < number_of_items. If you start at 1, you would have skipped the index 0 in your list, and thus have an incorrect standard deviation.

Your code is a complete mess and it doesn’t work at all. Did you try to run it before you posted it? You make the rookie mistake of forgetting that arrays begin with 0 (zero) and not 1.

Here is the corrected code:

#include
#include
using namespace std;

int main ()
{
//local declarations
int number_of_items;
double avg(0);

//local statements
cout <> number_of_items;

double list[number_of_items];
for (int i = 0; i < number_of_items; i++)
{
cout <> list[i];
}

// find the mean (average) of the data
double xbar = 0;
for(int i = 0; i <= number_of_items; i++)
xbar += list[i];

xbar = xbar / number_of_items;
cout << "The mean (average) is " << xbar << "." << endl;

// find the standard deviation
float numerator = 0;
float denominator = number_of_items;

for (int i = 0; i < number_of_items; i++)
{
numerator += pow((list[i] – xbar), 2.0);
}
float standard_deviation = sqrt (numerator/denominator);
cout << "The standard deviation for the given data is: "
<< standard_deviation << "." << endl;

Why ask the user for the number of pieces of data? Why not just tell them to enter the data with spaces or commas or whatever. Then split the string on that token, and you will know how many pieces of data were in the set.

This is an old post and I’m not here to bash your code — just to point in a new direction. I see myself through the time machine of your code here ðŸ™‚

On second thought, I hope your blog is/you are still alive.

This is really a C program with C++ includes — there is no need for C++ here. To be more accurate, this should be a function (in C, or a class in C++) that receives two arguments: number_of_items as an int and list[] as an array (in C, or could just reference the data from somewhere else, or in C++ get passed an object that contains the data already, or… the possibilities are endless) and returns an array (or something similar) of the values, or writes it somewhere and returns a location/pointer/filename to it.

If it were re-written this way it could be used in any number of other programs as a basic math utility — though, of course, good libraries which already include similar functions exist (so go harvest them for great ideas!).

Things to watch out for are garbage data and clear end-of-file/input signals. For this to really be usable code you need to do some type checking on the data to make sure you’re actually getting numbers instead of, say, image data, text of an email or something that will intentionally overrun your buffer and do Bad Things to your system.

Read some code from the Boost library, Qt4, or something like R (an open source scientific math library) to get some ideas how others have handled the things outlined above to learn more. It can also be useful to re-write this in a completely different language like Lisp (or maybe Python — though I personally feel Python’s approach is too similar to C to be useful for this purpose) and then re-write it from scratch again in C/C++ and see what you do differently.

thnxxxxxxx

Hi there,

There is a small bug in your function.

for (i = 1; i <= number_of_items; i++)

numerator = numerator + pow((list[i] – xbar), 2); to caluclate the standard deviation. It should be i = 0; i < number_of_items. If you start at 1, you would have skipped the index 0 in your list, and thus have an incorrect standard deviation.

Your code is a complete mess and it doesn’t work at all. Did you try to run it before you posted it? You make the rookie mistake of forgetting that arrays begin with 0 (zero) and not 1.

Here is the corrected code:

#include

#include

using namespace std;

int main ()

{

//local declarations

int number_of_items;

double avg(0);

//local statements

cout <> number_of_items;

double list[number_of_items];

for (int i = 0; i < number_of_items; i++)

{

cout <> list[i];

}

// find the mean (average) of the data

double xbar = 0;

for(int i = 0; i <= number_of_items; i++)

xbar += list[i];

xbar = xbar / number_of_items;

cout << "The mean (average) is " << xbar << "." << endl;

// find the standard deviation

float numerator = 0;

float denominator = number_of_items;

for (int i = 0; i < number_of_items; i++)

{

numerator += pow((list[i] – xbar), 2.0);

}

float standard_deviation = sqrt (numerator/denominator);

cout << "The standard deviation for the given data is: "

<< standard_deviation << "." << endl;

return 0;

}

Hi,

Thanks for sharing your codes,

But I believe the float denominator = number_of_items;

should be float denominator = number_of_items – 1;

Then you will get the correct standard deviation ðŸ™‚