0

I am trying to write a programm that calculates the crossproduct of two vectors. I wrote a function that calculates the cross product, but when I call the function I get really small values. I think I have a problem with initializing but I have no clue what I can do? I am fairly new to programming so sorry if it is obvious but I really don't get anywhere with it, would be nice if anyone could help.

my code:

#include <iostream>
#include <cmath>

using namespace std;

void kreuzprodukt(float a[3], float b[3], float a1, float a2, float a3)
{

    a1 = a[1] * b[2] - a[2] * b[1];
    a2 = a[2] * b[0] - a[0] * b[2];
    a3 = a[0] * b[1] - a[1] * b[0];
}

int main()
{

    float r;
    float alpha;
    float k1, k2, k3;

    cout << "Bitte gib einen Wert fuer r ein: " << endl;
    cin >> r;
    cout << "r = " << r << endl;
    cout << "Bitte gib einen Wert fuer alpha ein: " << endl;
    cin >> alpha;
    cout << "alpha = " << alpha << endl;

    float r1[3] = { r * sin(alpha), 0, -r * cos(alpha) };
    float r2[3] = { r * cos(alpha), r * sin(alpha), 0 };
    float r3[3] = { 0, r * cos(alpha), -r * sin(alpha) };

    kreuzprodukt(r1, r2, k1, k2, k3);
    cout << r1[0] << ";" << r1[1] << ";" << r1[2] << ";" << endl;
    cout << r2[0] << ";" << r2[1] << ";" << r2[2] << ";" << endl;
    cout << k1 << ";" << k2 << ";" << k3 << ";" << endl;

    float a1 = r1[1] * r2[2] - r1[2] * r2[1];

    cout << "k1 should be; " << a1 << "!" << endl;

    cout << "Alles io." << endl;
}

so my problem is that I get really small values for k1, k2 and k3. But when I try to make the calculation in the main function the right value gets calculated? Why is this?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • You need to pass `a1`, `a2`, and `a3` by reference instead of by value. See the duplicate question for more information. – Sam Varshavchik Oct 28 '20 at 12:07
  • It should be void `kreuzprodukt(float a[3], float b[3], float& a1, float& a2, float& a3)`, read up on the difference between pass by value & pass by reference. – Ylisar Oct 28 '20 at 12:07
  • 2
    I receive 7 compiler warnings when I compile in g++9.3 with `-pedantic -Wall -Wextra` Most of them are relevant. Always start by resolving the compiler warnings. They are the first line of defense against runtime logic errors. – user4581301 Oct 28 '20 at 12:07

0 Answers0