Square roots with arrays

I am supposed to compute the quantity for an array of values. I'm having trouble writing the correct code to do so. I have some code written, but the output values are incorrect it seems. Also, I am so supposed to return the resulting value, via explicit return statement, to the main program, and I have no idea how to do this exactly. Any suggestions?

void quantity (double x[], int n)
{
int i;

double Q = 0.0;
for (i=0; i <= n-1; i++)
{
Q = Q + sqrt(pow(x[i],2));
}
cout << " Q = " << fixed << setprecision(2) << setw(8) << right << Q<< endl << endl;
}
Last edited on
It's giving me the wrong quantity completely, and I'm not sure how to 'explicity return the resulting value'.
1) You cannot pass arrays like that, use pointers.
2) You cannot cout a void function.

1
2
3
4
5
6
7
8
9
void quantity(double* x, int n)
{
    double Q = 0.0;
    for(int i = 0; i < n-1; i++)
    {
        Q = Q + sqrt(pow(x[i],2));
    }
    cout << " Q = " << Q << endl;
}

I prefer to return answers.
1
2
3
4
5
6
7
8
9
double quantity(double* x, int n)
{
    double Q = 0.0;
    for(int i = 0; i < n-1; i++)
    {
        Q = Q + sqrt(pow(x[i],2));
    }
   return Q;
}


The very first example (in the linked tutorial) does return a value.


Show more of your code, the input, and the output.
Last edited on
I meant to cout 'Q'. My mistake. But, I'm supposed to return Q, and have a cout statement. When I try to do this, it says "error-return statement with a value".
Okay, I am now getting the correct output without putting the return statement. But, the instructions say to put the return statement. Here is what I have now.

case '6':
quantity (x, n);
cout << " Quantity Q = " << fixed << setprecision(2) << setw(8) << right << Q1 << endl << endl;
break;

void quantity (double x[], int n)
{
int i;

double Q = 0.0;
double Q1= 0.0;
for (i=0; i <= n-1; i++)
{
Q = Q +(pow(x[i],2));
Q1= sqrt(Q);
}
return Q1;
}

It's still not compiling, however.
Once again: the very first example in http://www.cplusplus.com/doc/tutorial/functions/
Change the return type from void to the type of value you want the function to return. In this case, change it todouble as shown in one of the earlier posts here.
Okay, I've done that. Now it is saying 'Q1 was not declared in this scope'. Any ideas on why this is.

double quantity (double x[], int n); // function prototype

case '6':
quantity (x, n);
cout << " Quantity Q = " << fixed << setprecision(2) << setw(8) << right << Q1 << endl << endl;
break;

double quantity (double x[], int n)
{
int i;

double Q = 0.0;
double Q1= 0.0;
for (i=0; i <= n-1; i++)
{
Q = Q +(pow(x[i],2));
Q1= sqrt(Q);
}
return Q1;
}

Last edited on
@jfesmire5: Adding code tags would make life better for everybody.
http://www.cplusplus.com/forum/articles/42672/

Now that your function returns a double...
You can assign it to Q1...
1
2
3
Q1 = quantity (x, n);
cout << " Quantity Q = " << fixed << setprecision(2) << setw(8) 
     << right << Q1 << endl << endl;

Or print it directly.
1
2
cout << " Quantity Q = " << fixed << setprecision(2) << setw(8) 
     << right << quantity (x, n) << endl << endl;
Last edited on
That got it fixed. Thanks a lot guys. My instructor throws all of this material at us that we haven't went over completely yet, so a lot of my knowledge on this is just trial and error, so I apologize if I sound ignorant in any way. Thanks again.
Topic archived. No new replies allowed.