Hi, I wrote a function as part of a homework assignment, but all it seems to be outputting is 1.#QNAN (I know what this is, I just can't find the cause of it). Here is the function:

double length ( const int a[], int size )
{
       double length = 0;
       for ( int i = 0; i < size; i++ )
       {
           length += pow((double)a[i],2);
       }
       length = sqrt( length );
}

I figure its a stupid mistake, but I just don't see it. Thanks beforehand for an answer!

Recommended Answers

All 4 Replies

Can you show me what a[] contains? It is probably an overflow problem.

The values of it are prompted in the main function, but i haven't used any numbers larger than 10, or less than 1.

Oh this is a silly mistake, your function name and the variable name are the same.
Change one of them. Plus you are not returning anything? Do this :

double length ( const int a[], int size )
{
       double len = 0;
       for ( int i = 0; i < size; i++ )
       {
           len += pow((double)a[i],2);
       }
       return sqrt( len );
}

What did I say? It was probably a stupid mistake :) Thanks for pointing it out, I never would of noticed if you hadn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.