this code compiles, but when i run the program, it outputs "floating point exception". to my knowledge this happens when you divide by 0, but ive worked it out on paper, and i am not dividing by 0.

its the method implementation for a class. if you need it i can post the .h and main

float LinearEquation::Intersection(LinearEquation & linearequation , float & x , float & y)
{
   int D = 0;
   int E = 0;
   int F = 0;
   linearequation.GetCoefficients(D ,E ,F);
   y = ((D * C) - (A * F))/((A * E) - (D * B));
   x = SolveX(y);
   return x,y;
}

Recommended Answers

All 3 Replies

You're not changing D, E or F.

They are zero when you create them, they are zero when you do this:
y = ((D * C) - (A * F))/((A * E) - (D * B));
so you ARE dividing by zero.

You need to learn about "pass by value", "pass by reference" and probably "return values".

The function GetCoefficients is given a COPY of the values D, E, F, because you are passing by value. Whatever it does, it does to those COPIES and not to the originals.

Moschops - how do you know the GetCoefficients method isn't using reference paramters?

Yes, it apparent the OP does have some confusion on this issue, as the function above has the x and y passed by reference but attempts to return both of them.

return x,y; will only return one value, the y. Since there are two values that the caller needs, the return mechanism will not suffice. There is no need for a return statement in this function. x and y hold their solution values at the time the function exits, and they are references to the actual arguments at the calling location.

Another area that might be causing the error - does the linearequation object have coefficients as data members? When the getcoefficents method fills in the locally declared D, E, F variables, does that cause the linearequation to get updated? Where do A, B and C get their values?

Moschops - how do you know the GetCoefficients method isn't using reference paramters?

I did thnk of that; it's a guess based on his probable inexperience, that it's the simplest solution, and that the code demonstrates a lack of understanding of the mechanisms of passing values across functions.

A better answer is just to stick cout << A << " " << B << " " << C << " " << D << " " << E << " " << F << endl; after line 6 and then there won't be any more guessing.

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.