PRACTICAL EXERCISE 7 - QUADRATIC EQUATIONS

(Week 10 Starting November 30th)

Hand in date - one week after your scheduled tutorial session in week 10.


1. REQUIREMENTS

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:

  1. If a and b are both zero there is no solution (this is referred to as the degenerate case):
    -8 = 0?
    a= 0, b = 0, c = -8
    (degenerate case)
    
  2. If a is zero and b is non zero the equation becomes a linear equation.

    2X - 8 = 0
    a= 0, b = 2, c = -8
    (Linear equation)
    
    root = -c/b = 8/2 = 4.0
    X = 4.0
    
  3. If the value for the term b^2 - 4ac (the discriminant) is negative there is no solution (we cannot find the square root of a negative number):
    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.
    
  4. If the discriminant is 0 then there are two identical solutiions, i.e. only one solution (root) need be calculated:
    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.


2. REPORT

You should hand in a report comprising the following sections:

  1. Requirements
  2. Design - including top down analysis, and Nassi-Shneiderman and flow charts
  3. Implementation
  4. Black box and white box test cases and results and evidence of data validation

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