Tried to break down the math... I can't seem to make it stop erroring me :D Honestly, this is the way we broke it down from our lessons, but I don't understand standard deviation in the slightest... anyone know why I'm getting overloaded? I'm getting this error message:
error C2668: 'sqrt' : ambiguous call to overloaded function
(but if I change anything, it just claims it's an overloaded function)
#include <iostream>
#include <cmath>
usingnamespace std;
int standardDeviation(constint nameofArray[], int arraySize, int mean);
main()
{
//other code defining variables, calling other functions, defining arrays, etc.
deviation = standardDeviation(mainArray, mainArraySize, mean); //calling function
return 0;
}
int standardDeviation(constint nameofArray[], int arraySize, int mean)
{
int sum = 0;
int deviation = 0;
for(int i = 0; i<arraySize; i++)
sum = sum + (mean-nameofArray[i]) * (mean-nameofArray[i]);
deviation = sqrt(sum/ arraySize-1) ;
return deviation;
}
Looks like I fix the errors by changing everything to doubles, but that complicates other features that I'm using the same variables, for example the % operator...
sqrt() is defined for several different numerical types; e.g. float and int. Try doing floating point division (cast one of the operands to float) then pass it to the function.
Certainly you can sqrt() an int, but the problem is you probably want the result of your division to be a floating point number or you risk loss of precision. You don't need to cast everything to double as you are right now. Your sum should be an integer as the array is made up of integers. I would just cast to floating point before doing the square root, then back to integer for your return value.