Design and develop an Ada program that continuously computes and displays value(s) for x, given quadratic equations (i.e. a second-order polynomials) of the form:
ax^2 + bx + c = 0
where the values for the coefficients a, b and c are supplied by the user, and are assumed to be integers within the range of -100 to 100. To control the loop use a menu interface of the form discussed when illustrating while loops with exits. The menu should include two options Calculate quadratic and End. Note that to solve a quadratic equation we must calculate the roots. This can be done using the quadratic formula:
root 1 = (-b + sqrt(b^2-4ac)) / 2a root2 = (-b - sqrt(b^2-4ac)) / 2a
Example:
X^2 + 2X - 8 = 0 a= 1, b = 2, c = -8 roots = (-2 +or- sqrt(2^2-4x1x-8)) / 2x1 = (-2 +or- sqrt(4+32)) / 2 root1 = (-2 + 6)/2 = 4/2 = 2.0 root2 = (-2 - 6)/2 = -8/2 = -4.0 X = 2.0 or -4.0
However, there are certain special consideration to be taken into account:
-8 = 0? a= 0, b = 0, c = -8 (degenerate case)
2X - 8 = 0 a= 0, b = 2, c = -8 (Linear equation) root = -c/b = 8/2 = 4.0 X = 4.0
X^2 + 2X + 8 = 0 a= 1, b = 2, c = 8 roots = (-2 +or- sqrt(2^2-4x1x8)) / 2x1 = (-2 +or- sqrt(4-32)) / 2 = (-2 +or- sqrt(-28)) Negative discriminant therefore no solution.
X^2 + 4X + 4 = 0 a= 1, b = 4, c = 4 roots = (-4 +or- sqrt(4^2-4x1x4)) / 2x1 = (-4 +or- sqrt(16-16)) / 2 (Discriminant = 0, there fore only one solution) root = -4/2 = -2 X = -2.0
Output, where appropriate, should be accurate to 4 decimal places.
You should hand in a report comprising the following sections:
Marks distributed evenly between design, implementation and testing. Refer to guidance notes on the presentation of work if necessary.
Created and maintained by Frans Coenen. Last updated 11 October 1999