what am I doing wrong?

int find_length(int *A,int n){
int i,sum=0;
int length;
for(i=0;i<n;i++)
sum+=A[i]*A[i];
length= sqrt(sum);
return length;
}

it keeps giving me this error:
'sqrt' : ambiguous call to overloaded function
could be 'long double sqrt(long double)'
'float sqrt(float)'
'double sqrt(double)'
what am I doing wrong?
You're trying to call a function that doesn't exist.

The function sqrt does not take an int. You are trying to give it an int. Your compiler has three sqrt functions it knows about (more recent compilers have more). One takes a float. One takes a double. One takes a long double.

The compiler is not psychic. It cannot guess which of these functions you actually meant to use. You will have to tell it.
Last edited on
thanks I got it! i just did sqrt (double(sum));
thanks :)
This will compile cleanly in C++11.

C++98: length = std::sqrt( double(sum) ) ;

Make find_length() const-correct. And consider if the result type should be a floating point value.

find_length()? Or should it be root_sum_square()?

1
2
3
4
5
6
/* int */ double /* find_length */ root_sum_square( const /* const added */ int* A, int n )
{
    double sum = 0.0 ;
    for( int i = 0 ; i < n ; ++i ) sum += A[i] * A[i] ;
    return std::sqrt(sum) ;
}

Last edited on
Topic archived. No new replies allowed.