CS 2073, Spring 2006
 Program 2
 Roots of a Quadratic Equation
    Week 2: Jan 23-27
 Due (on time): 2006-02-03  23:59:59
 Due (late):        2006-02-06  23:59:59

Program 2 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2006-02-03  23:59:59 (that's Friday, 3 February 2006, 11:59:59 pm) for full credit.
  • 2006-02-06  23:59:59 (that's Monday, 6 February 2006, 11:59:59 pm) for 75% credit.


Introduction: For this program you are to find the roots of a quadratic equation. Specifically, start with an equation: a*x2 + b*x + c = 0 , for specific doubles a, b, and c.

Then calculate and print the values of the roots, r1 and r2.

The principal difficulty of this program is that it needs a fairly complicated collection of if-else statements.


Details about what to do:
  1. Use scanf to read values for double variables a, b, and c, which are assumed to be the coefficients of a quadratic equation.
  2. Print the input numbers in the form of a quadratic equation, using the form shown in the example section below.
  3. If a is equal to zero, print a message that this is linear equation. In case b is not zero, there is a single root: -c/b, and if b does equal zero, there is no root. Print the value of this root or print that there is no root. In this case, your program should not do any of the following items.
  4. Otherwise, assuming a is not zero, calculate the discriminant d given by d = b2 - 4*a*c
  5. If the discriminant is greater than zero, then there are two real roots. Print a message that says this, use the quadratic formula to calculate their values, r1 and r2, and print these two values.
  6. In case the discriminant is equal to zero, there is a single repeated real root. Print a message that says this, use the quadratic formula to calculate the value, and print this value.
  7. If the discriminant is less than zero, then there are two imaginary roots. Print a message that there are two imaginary roots and then print the real and imaginary part of each root. The real part of both roots is the same: -b/(2*a), while the imaginary parts are sqrt(-d)/(2*a) and -sqrt(-d)/(2*a). Notice that we have d < 0, so -d > 0, and it makes sense to take the square root of -d. (It is important for you to realize that the C language has no built-in facilities to handle complex numbers. We are simply working with real numbers, and calculating real and imaginary parts (both reals) of the roots as complex numbers.)


Sample input and output: You may use a separate run for each input triple of numbers, since we haven't studied loops yet. Below in bold are the three numbers that you enter for each separate run. Everything else is what your program should output. You should try each of these 8 sets of inputs and your answers should be the same. For full credit, your output should look exactly the same as the output shown.


What you should email: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Program Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Program 2:

Last Name, First Name; Course Number; Program Number.

  1. C source for your program, quadratic.c. (Or whatever you wish to name it.)
  2. Results of a run or runs of the program, including at least all the 8 sets of data above.


Revision date: 2006-01-08. (Please use ISO 8601, the International Standard.)