CS 1044, Assignment 4, Schuetz
Spring 1998
Due Date: 11:59 pm, March 31, 1998
Introduction
Write a program to find the roots of a quadratic equation, using the following design which involves the use of four functions. Recall that for a quadratic equation
Ax² + Bx + C = 0
the roots are given by
-B + sqrt (B² - 4AC) and -B - sqrt (B² - 4AC)
------------------- -------------------
2A 2A
Note that the quantity (B² - 4AC) is called the discriminant (D). If A != 0,
then there are three distinct possibilities for D.(1) If D > 0, then there are two distinct real roots. For example, if A = B = 1 and D = 4, then we have the two roots
-1 + sqrt (4) -1 + 2
----------- = ----- = 0.5 and
2 2
-1 - sqrt (4) -1 - 2
----------- = ----- = -1.5
2 2
(2) If D = 0, then both roots are real and have the same value. For example, if
A = 1, B = 2, and D = 0, then the roots are both
-2 - sqrt (0)
----------- = -1.0
2
(3) If D < 0, then there are two complex number solutions of the form M + Ni
and M - Ni. For example, if A = B = 1 and D = -4, then we have the two roots
-1 + sqrt (-4) -1 + 2i
------------ = ------ = -0.5 + 1.0i and
2 2
-1 - sqrt (-4) -1 - 2i
------------ = ------ = -0.5 - 1.0i
2 2
-------------------------------
Note: If A = 0 but B != 0, then the equation becomes Bx + C = 0 which has the single real solution -C / B. If A = B = 0, then the coefficients do not form a valid equation, and an error message should be written. In your program, all the floating point numbers and coefficients in your equations should be stored as type double to avoid loss of precision.Input
The input for your program is to be read from the file, asgn4.in. Each line of this file has three real numbers that represent the equation coefficients A, B, and C, respectively. It is unknown how many equations are to be solved. A typical input file might have the following data:
1.0 -2.0 -3.0 1.0 -2.0 10.0 1.0 -2.0 1.0 0.0 -0.0 1.0 0.0 2.0 4.0Functions
This problem is to be solved using the following four functions:
(a) If numrealsolns = 2 (D >= 0) then num1 and num2 are the two roots which may be identical if D=0.
(b) If numrealsolns = 1 (A = 0) then num1 is the single real root and num2 is meaningless.
(c) If numrealsolns = 0 (D < 0) then num1 and num2 are the real and imaginary parts of the complex solutions (M and N). You may assume that num2 is positive. Then the answers are num1 + num2 i and num1 - num2 i.
The output must be written to the file, asgn4.out. The output generated by the input data above is
Number
of
A B C Solutions Root 1 Root2
-------------------------------------------------------------------------
1.00 -2.00 -3.00 2 Real 3.00 -1.00
1.00 -2.00 10.00 2 Complex 1.00 + 3.00i 1.00 - 3.00i
1.00 -2.00 1.00 2 Real 1.00 1.00
0.00 0.00 1.00 ***** Error: Invalid coefficients ! *****
0.00 2.00 4.00 1 Real -2.00
DocumentationAs with every program you will write this semester, your code will need to conform to the departmental standards, which include prettyprinting such as whitespace and indentation, a documentation header for each function you write including main, and readable inline documentation that describes the function of each chunk of code. All function parameters must also be documented so that a user will understand how they are used.
Your Submission
Your programs are to be submitted to the automatic grader; they are archived by the grader and will be hand graded for conformance to departmental standards on a random basis. No other materials need be submitted with this assignment. This assignment should be submitted as Project Number 4. You can earn two bonus points per day (24 hours) for each day early you submit your program, up to three days (total of 6 points).