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.
#include <iostream>
#include <cmath>
usingnamespace 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!
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:
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!
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).
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!
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):
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.