standard deviation function overloaded

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)
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
#include <iostream>
#include <cmath>
using namespace std;

int standardDeviation(const int 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(const int 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;
}
Last edited on
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.
Thank you very much, Zhuge. I changed both sum and deviation to float, which allowed the function to not overload.

1
2
3
4
5
6
7
8
9
10
11
12
int standardDeviation(const int nameofArray[], int arraySize, int mean)
{
	float sum = 0;
	float deviation = 0;

	for(int i = 0; i<arraySize; i++)
		sum = (mean-nameofArray[i]) * (mean-nameofArray[i]);

	deviation = sqrt(sum/ arraySize-1) ;
	
	return deviation;
}
However, now I have 3 :
warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
warnings on me... :/ I can't change everything to float.... is there a way to squareroot something with an int?
I didn't know about how to convert types, for anyone else having this issue, here's a nice linky. After I fix my code, I will post. http://xoax.net/comp/cpp/console/Lesson25.php
And here's my code: (haven't run it yet, hopefully it works, but no error codes, at least)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int standardDeviation(const int nameofArray[], int arraySize, int mean)
{
	double sum = 0;
	int deviation = 0;
	double deviation1 = 0;

	for(int i = 0; i<arraySize; i++)
		sum = (double(mean-nameofArray[i])) * (double(mean-nameofArray[i]));

	deviation1 = sqrt(double(sum/ arraySize-1)) ;
	deviation = (int(deviation1));
	
	return deviation;
}
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.
Topic archived. No new replies allowed.