Share via

C++ Cross Product Function

Sid Kraft 41 Reputation points
2026-05-16T17:03:17.89+00:00
Have the function below in Visual Studio, C++. The function does a cross product for a vector VEC1,Vec2,Vec3. When I call the function as "crossproductVector(Vec1,Vec2,Vec3)" the value returned for Vec3 has zero components even though the values for out5[0], out5[1], out5[2] have values 0.0, 0.0, .866 respectively. Not sure why the return is not there in the function arguments argval2, argval3 and argval4, all vector variables? Sid Kraft.

void crossproductVector(vector<float>argvec2, vector<float>argvec3,
    vector<float>argvec4)
{
    vector<float> out3(3, 0.0f);
    vector<float> out4(3, 0.0f);
    vector<float> out5(3, 0.0f);
    float ans2 = 0;
    float ans3 = 0;
    float ans4 = 0;
    float ans5 = 0;
    float ans6 = 0;
    float ans7 = 0;
    float ans8 = 0;
    float ans9 = 0;
    float ans10 = 0;
    I = 0;
    do
    {
        out3[I] = argvec2[I];
        out4[I] = argvec3[I];
        out5[I] = argvec4[I];
        I = I + 1;
    } while (I <= 2);
    out5[0] = (out3[1] * out4[2] - out4[1] * out3[2]);
    out5[1] = -(out3[0] * out4[2] - out4[1] * out3[2]);
    out5[2] = (out3[0] * out4[1] - out4[1] * out3[1]);
    ans2 = sqrt(out3[0] * out3[0] + out3[1] * out3[1] + out3[2] * out3[2]);
    ans3 = sqrt(out4[0] * out4[0] + out4[1] * out4[1] + out4[2] * out4[2]);
    ans4 = sqrt(out5[0] * out5[0] + out5[1] * out5[1] + out5[2] * out5[2]);
    ans5 = (out3[0] * out3[0]);
    ans6 = (out4[1] * out4[1]);
    ans7 = (out3[1] * out3[1]);
    ans8 = (out4[2] * out4[2]);
    ans9 = (out3[2] * out3[2]);
    ans10 = (out4[2] * out4[2]);

    if ((ans2 != 0.0f && ans3 != 0.0f) && ((ans5 != ans6) || (ans7 != ans8) || (ans9 != ans10)))
        MAG1 = ans4 / (ans2 * ans3);
    else
    {
        cout << "Error: vectors co-linear or have zero magnitude, cannot compute cross product." << "\n";
        itemp = 1; // Set error flag
    }
    // Normalize negative zero entries, if any, for the cross product components
    I = 0;
    do
    {
        if (out5[I] == -0.0f) out5[I] = 0.0f;
    } while (++I <= 2);
    argvec4[0] = out5[0];
    argvec4[1] = out5[1];
    argvec4[2] = out5[2];
    return;
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


2 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 6,865 Reputation points Microsoft External Staff Moderator
    2026-05-18T04:22:44.83+00:00

    Hi @Sid Kraft ,

    Your function is passing all three vectors by value, so argvec4 is only a local copy inside the function. Because of that, the values assigned to out5 are not written back to Vec3 in the calling code.

    I tested this and see that:

    • when the third argument is passed by value, the caller’s vector remains unchanged
    • when the third argument is passed by reference, the caller receives the computed result correctly

    You should change the function signature to this:

    
    void crossproductVector(const std::vector<float>& argvec2,
    
                            const std::vector<float>& argvec3,
    
                            std::vector<float>& argvec4)
    
    

    The first two vectors are input only, so const std::vector<float>& is appropriate. The third vector is the output, so it must be a non-const reference.

    For example:

    
    void crossproductVector(const std::vector<float>& a,
    
                            const std::vector<float>& b,
    
                            std::vector<float>& c)
    
    {
    
        c.resize(3);
    
        c[0] = a[1] * b[2] - a[2] * b[1];
    
        c[1] = a[2] * b[0] - a[0] * b[2];
    
        c[2] = a[0] * b[1] - a[1] * b[0];
    
    }
    
    

    If you saw an error such as “must be a modifiable lvalue” when using references, that usually means the parameter was declared as const std::vector<float>&. A const reference cannot be modified, so the output parameter must not be const.

    Also, please double-check the formulas in your original code for the 2nd and 3rd components, because they do not match the standard cross-product formula. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.

    Thank you. 

    Was this answer helpful?

    0 comments No comments

  2. Q&A Assist
    2026-05-16T17:03:30.54+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    crossproductVector takes its parameters by value:

    void crossproductVector(vector<float> argvec2,
                            vector<float> argvec3,
                            vector<float> argvec4)
    

    Each argvec* is a copy of the vector passed at the call site. Inside the function, this line:

    argvec4[0] = out5[0];
    argvec4[1] = out5[1];
    argvec4[2] = out5[2];
    

    modifies only the local copy argvec4. The original Vec3 in the caller is unchanged, so it still has zeros after the call.

    To have the function write the result back into the caller’s vector, use references (or pointers) instead of pass‑by‑value. For example:

    void crossproductVector(const vector<float>& argvec2,
                            const vector<float>& argvec3,
                            vector<float>& argvec4)   // note '&'
    {
        vector<float> out3(3, 0.0f);
        vector<float> out4(3, 0.0f);
        vector<float> out5(3, 0.0f);
    
        int I = 0;
        do
        {
            out3[I] = argvec2[I];
            out4[I] = argvec3[I];
            ++I;
        } while (I <= 2);
    
        out5[0] =  out3[1] * out4[2] - out3[2] * out4[1];
        out5[1] =  out3[2] * out4[0] - out3[0] * out4[2];
        out5[2] =  out3[0] * out4[1] - out3[1] * out4[0];
    
        // write back into caller's Vec3
        argvec4[0] = out5[0];
        argvec4[1] = out5[1];
        argvec4[2] = out5[2];
    }
    

    Call it as before:

    vector<float> Vec1(3), Vec2(3), Vec3(3);
    // fill Vec1, Vec2
    crossproductVector(Vec1, Vec2, Vec3);  // Vec3 now contains the cross product
    

    Alternatively, return the result instead of trying to modify an argument:

    vector<float> crossproductVector(const vector<float>& a,
                                     const vector<float>& b)
    {
        vector<float> r(3);
        r[0] = a[1] * b[2] - a[2] * b[1];
        r[1] = a[2] * b[0] - a[0] * b[2];
        r[2] = a[0] * b[1] - a[1] * b[0];
        return r;
    }
    
    vector<float> Vec3 = crossproductVector(Vec1, Vec2);
    

    The same rule explains the earlier scalar example: function parameters are passed by value unless explicitly declared as references or pointers, and a function can return only a single value of its declared return type.


    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.