Computer Science, asked by hinabutt284, 7 months ago

Write a 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).)

Answers

Answered by harsika1265
4

Answer:

Explanation:

The easiest way to solve a quadratic equation is with the Quadratic formula:

x=−b±b2−4ac √2a.  

To ensure you get real roots, you have to make sure the Discriminant is positive so you are not taking the square root of a negative number, which generates an error in many languages.

So a generic BASIC-like pseudocode might be as below, where d is the discriminant.

let d = b * b - 4 * a * c

if d < 0 then

print "No real roots."

else if d = 0 then

print "Root is: "; -b / (2 * a); "."

else

print "Roots are: "; (-b + sqrt ( d )) / (2 * a); " and "; (-b - sqrt ( d )) / (2 * a); "."

end if

To do this as a function definition, you would have to be able to return a single value when the discriminant is zero, two values when the discriminant is positive, and throw an error when the discriminant is negative.

Similar questions