0

I am trying to input multiple arrays into a function the arrays are b0[5] and b1[5]

do I declare it at the top as

double calcTotVol(double[], double[], float, int);

then receiving

double totalVol;
totalVol = calcTotVol(b0, b1, dbh, totalHt);

do I use the names of the array when creating the function

double calcTotVol(double b0[], double b1[], float dbh, int totalHt)

double totalVol;

totalVol = b0[5] + b1[5] * (dbh*dbh) * totalHt;

return totalVol;
5
  • its always better to post real code rather than odd lines. I think the answer is 'yes, use the names from the function declaration in the body of the function' Commented Aug 4, 2014 at 23:58
  • Add a pair of braces around the body of the function in the last code fragment, and it would look fine. Commented Aug 4, 2014 at 23:58
  • 2
    note that the code you show will fail. an array declared as b0[5] has 5 elements the line totalval = b0[5] ... is trying to access the 6th element Commented Aug 4, 2014 at 23:59
  • I'm not sure if totalVol will recieve the correct value you are expecting... b0[5] + b1[5] will calculate to the addition of the values currently in those positions in the array, not the whole collection of values Commented Aug 4, 2014 at 23:59
  • also make sure that the precedence of + and * doesnt catch you out Commented Aug 5, 2014 at 0:00

2 Answers 2

1

I think this is what you are trying to do:

double calcTotVol(double b0[], double b1[], int arraySize, float dbh, int totalHt)
{
    double totalVol = 0;

    for(int i = 0; i < arraySize; i++)
    {
        totalVol += ( b0[i] + b1[i] );
    }
    float values = (dbh*dbh) * totalHt;
    totalVol *= values;

    return totalVol;
}

And a solution that is even better is replacing the c-style arrays with the new C++11 std::array type for added safety and easier usage.

Sign up to request clarification or add additional context in comments.

2 Comments

You have offered a reasonable guess at the OP's intentions, as they offer a cubical volume calculation, but they have other errors (addressing the ordinal sixth position in an array with only five locations, not bothering to pass the size of the arrays, omitting the parenthesis around the sum of the b0[i]+b1[i],...), so be careful guessing what the OP intends.
whoa, that is correct! Reading values after the real end of an array can cause undefined behavior or a security risk... editing thanks
0

You're doing everything correctly except 'opening {' and 'closing }' braces in the given code.

1- As you asked: Do I declare it at the top as?

double calcTotVol(double[], double[], float, int);

Yes you can declare it as it is, it's called Prototype of a function in which you don't need to write down the names for variables.

2- And yes you're calling this function calcTotVol correctly in

totalVol = calcTotVol(b0, b1, dbh, totalHt);

3- You're third question is doubtful but here is a little demonstration for your doubtful question: Surely, You've to use the names of variables including the names of arrays in the function definition, without these names you'll get compile time error. And it's not required to write down the same names of variables or arrays name as your variables names that you're passing while calling your function, you can write any name as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.