Computer Science 2611
Spring 2000
Laboratory Assignment 5

Notation. I will write x^2 to indicate x to the second power.

The quadratic equation ax^2 + bx + c = 0 has solutions given by x = (-b + sqrt(b^2 - 4ac))/(2a) and x = (-b - sqrt(b^2 - 4ac))/(2a). The quantity b^2-4ac is called the discriminant of the equation. If it is negative, the equation has no solutions. If it is 0, the equation has just one solution. If the discriminant is positive, the equation has two solutions.

Write a program that reads three numbers a, b and c from the user and tells the user the solutions(s) to equation ax^2 + bx + c = 0. It should either report that there are no solutions, one solution (and tell what that solution is) or two solutions (and tell both solutions.)

Use functions. Make the functions short an simple. Provide a clear and precise contract for each function. Write the program as follows.

  1. Think about the functions that you will find convenient. Roughly what would you like each function to do? You should be sure to choose functions so that you do not need to write the same thing more than once. If you find that you repeat yourself in the program, you are not using the correct functions.

  2. Tie down the contract of each function, so that you know exactly what it is supposed to do. Write down the contract.

  3. Write the implementation of each function.

  4. Test the program. Be sure to exercise all parts of your program. For example, try it on an equation that has no solutions, an equation that has one solution and an equation that has two solutions. If it does not work, you might want to test each function separately. Since each has a contract, you know what each is supposed to do, so you can test whether it satisfies its contract.

Turn in a printout of your program.