Write pseudocode for an algorithm for finding real roots of equation ax2 + bx + c = 0 for arbitrary real coefficients a, b, and
c. (you may assume the availability of the square root function sqrt (x).)

Respuesta :

To find the roots of a quadratic equation [tex] ax^2+bx+c=0 [/tex] you need to use the quadratic formula

[tex] x_{1,2} = \dfrac{-b\pm\sqrt{b^2-4ac}}{2a} [/tex]

This implies that if [tex] b^2-4ac<0 [/tex] the equation has no solutions, if [tex] b^2-4ac=0 [/tex] the equation has one double solution, and two distinct solutions otherwise.

So, the pseudocode could be something like this:

double delta = b*b - 4*a*c;

if (delta < 0) print ("There are no real solutions!");

else if(delta = 0) {

double x = -b/(2*a);

print("There is a double solution: " + x);

} else {

double det = sqrt(delta);

double x1 = (-b+det)/(2*a);

double x1 = (-b-det)/(2*a);

print ("The two solutions are " + x1 + "and" + x2);

}