The exercise tries to solve a quadratic equation and depending on what you give, the pointers will receive one thing or another. The first two cases do not give me a problem, but when I test the third and fourth, the pointers receive something that I have not programmed.
For example, when I put a and b zero, the equation is supposed to have no solution and therefore px1
and px2
that are pointers should give 0.000 and 0.000, however they give me -nan and -nan. In the fourth case that happens when the equation is of the first degree, that is to say that it a
is zero, it should return the first degree equation resolved in px1
y -nan in px2. On the contrary, it returns -nan -inf.
I've tried changing them else if
in case they didn't fit, but nothing, it's been several hours and I can't think of anything.
In case you need it, the first case is if the result of the equation is real, both results are stored in the two pointers. The second case is if there is an imaginary solution, the real part is put in px1
and the imaginary part in px2
.
I attach the part of the function, main
he only has the scanf
and print
to be able to use it.
int resolver(double a , double b, double c, double* px1, double* px2){
if((pow(b,2) - 4* a * c )>= 0){
*px1 = (-b + sqrt(pow(b,2) - 4* a * c))/ (2 * a);
*px2 = (-b - sqrt(pow(b,2) - 4* a * c))/ (2 * a);
return 1;
}else if((pow(b,2) - 4* a * c) < 0){
*px1 = -b/(2*a);
*px2 = sqrt(-(pow(b,2) - 4* a * c))/(2*a);
return 2;
}else if(a == 0 && b==0){
*px1 = 0;
*px2 = 0;
return 3;
}else if(a == 0 ){
*px1 = -c/b;
*px2 = sqrt(-1.0);
return 4;
}
return 0;
}
It means that it enters a previous case where the condition is also met.
For example, if
a=0, b=0
, the first if is:(pow(0,2) - 4* 0 * c )>= 0
, which should be(0 - 0) >= 0
, which should givetrue
.Your code works perfect, it just takes care of other things first.
Change the order of your validations and learn how to debug your code. This way you will know what happens with the execution depending on the values in the variables used