Homework Help - Standard Deviation

Hey guys. I am new to C++ and up to this point I have had no trouble getting any of my projects to compile. This is HOMEWORK, so I am not looking to be spoon fed any answers, I just really need someone who knows what they are doing to look it over if you have the time. I have looked over the other forums and found one match that is very close to my problem here, however, mine is a bit different.

To give an idea of what I am trying to do, I need to create a program that computes the mean and standard deviation of 4 integer values. I am given the formula for standard deviation and told that n=4.

Here is my code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{

//Define variables

float numberOne;
float numberTwo;
float numberThree;
float numberFour;
float mean;
float standardDeviation;
float someInt;

someInt = 4-1;

//Get input from the user.

cout << "Input your first number :" << endl;
cin >> numberOne;
cout << "Input your second number:" << endl;
cin >> numberTwo;
cout << "Input your third number :" << endl;
cin >> numberThree;
cout << "Input your fourth number:" << endl;
cin >> numberFour;

//Calculates Mean
mean = (numberOne + numberTwo + numberThree + numberFour) / 4;

//Calculates Standard Deviation
standardDeviation = sqrt((numberOne-mean,2) + (numberTwo-mean,2) + 
(numberThree-mean,2) + (numberFour-mean,2)/ someInt );

//Outout of mean and standard deviation.

cout << "The mean is " << mean << " and the standard deviation is " << standardDeviation << "." << endl;

return 0;
} 




The error that my compiler gives are 2 "Unresolved Externals" .The error codes are LNK2019 and LNK1120. I believe the problem is mostly occuring during the calculation of the mean and standard deviation. Perhaps my parenthesis are off? I've read many forums and tutorials and so far, everything I have tried has not yet worked. Thanks in advance!

Kendra
And the unresolved externals are...?

Your standard deviation doesn't look right. As it stands, you have sqrt(2+2+2+2/someInt). What is that supposed to mean?
Your error is on this line.

1
2
3
//Calculates Standard Deviation
standardDeviation = sqrt((numberOne-mean,2) + (numberTwo-mean,2) + 
(numberThree-mean,2) + (numberFour-mean,2)/ someInt );


You can't call sqrt like that... it takes two arguments, not four. Not to mention that you called it several times without the function identifier. This should work:

1
2
//Calculates Standard Deviation
standardDeviation = (sqrt((numberOne - mean),2) + sqrt((numberTwo - mean), 2) + sqrt((numberThree - mean),2) + sqrt((numberFour - mean),2)) / someInt;
Last edited on
Thank you for your prompt reply.

These are the errors from my first attempts of compiling my code.

error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

fatal error LNK1120: 1 unresolved externals

packetpirate
Thank you for the code suggestion. I did try it and I believe that its very close, however, it gave me 4 of this error
(38) : error C2661: 'sqrt' : no overloaded function takes 2 arguments

I'm still playing around with the code to see if I can get it figured out. Thanks again!
Last edited on
error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

This is a project setup issue. It seems you chose Windows Application when selecting the project type. What you should do instead is choose Empty Project. You may be better off just starting a new project instead of playing with the settings of your current project.

packetpirate wrote:
You can't call sqrt like that... it takes two arguments, not four

I only see one argument (and it only takes one argument).

Kendra, the issue with your standard deviation calculation is these: ((n - mean), 2). That is the comma operator. In short both statements are evaluated and the result of the second one is used as the result of the operation. So, as Zhuge stated, you're just doing sqrt(2 + 2 + 2 + 2 / 3.0).

This should be your standard deviation line:

standardDeviation = sqrt( ( pow(numberOne - mean, 2) + pow(numberTwo - mean, 2) + pow(numberThree - mean, 2) + pow(numberFour - mean, 2) ) / someInt);
Last edited on
THANKS So much!

So, I have been thinking for a couple of days that my c++ compiler had been compromised. A few days ago , my anti virus cleared up a virus that was attached to some of my c++ files. Since then, the sample codes given to the class from the instructor has not been compiling properly and then when I wrote out this code, it gave me the same error LNK2019...long story short, i deleted the compiler, and reinstalled it. That being said, i ran my code again, and still came up with errors HOWEVER, after i included this line for standard deviation, my code worked properly!

standardDeviation = sqrt( ( pow(numberOne - mean, 2) + pow(numberTwo - mean, 2) + pow(numberThree - mean, 2) + pow(numberFour - mean, 2) ) / someInt);

Thanks shacktar for information that helped me completely debug my code. And Much thanks to everyone else!
Yeah, sorry. I was thinking of pow(). But still, he wasn't passing one value to it, he was trying to pass 4.

1
2
3
4
5
6
7
8
//Calculates Standard Deviation
standardDeviation = sqrt(
                                        (numberOne-mean,2) + 
                                        (numberTwo-mean,2) + 
                                        (numberThree-mean,2) + 
                                        (numberFour-mean,2) / 
                                        someInt 
                                        );


Notice how he was using a comma in those four parentheses within his sqrt call? It seems like he was trying to raise the number on the left to the power of 2, or call sqrt. This is a syntax error. He has to call sqrt() on all those numbers individually, like this (sorry about the blunder last time):
 
standardDeviation = (sqrt(numberOne - mean) + sqrt(numberTwo - mean) + sqrt(numberThree - mean) + sqrt(numberFour - mean)) / someInt;


That should work.
That is syntactically correct but it won't calculate the standard deviation.

Notice how he was using a comma in those four parentheses within his sqrt call?

Yes, but the comma use there resolves to the use of the comma operator, not parameter differentiation.

This: (numberOne-mean,2) is one expression, where numberOne - mean is evaluated and discarded. Then, 2 is evaluated (trivially) and is used as the result of the expression.

http://en.wikipedia.org/wiki/Comma_operator

It seems like he was trying to raise the number on the left to the power of 2, or call sqrt. This is a syntax error.

Of course, they meant to do pow(numberOne-mean, 2), pow(numberTwo-mean, 2), etc.

However, it was not a syntax error.
Last edited on
Topic archived. No new replies allowed.