COSC 2415 - Data Structures and
ITSE 2445 Data Structures
Bob Comer, Professor of Computer Studies
Program 1 - Quadratic Expressions
Be sure to read through Chapter 4 of the textbook before starting this assignment.
The objective is to construct a comprehensive class that can be used for many different applications in client programs. Your project should be divided into separate files - a class definition file, a class implementation file, and the client (driver) program. Be sure that your driver includes code to test all your class functions.
A quadratic expression has the form
ax2 + bx + c
where x is a variable that can take on different numeric values
and a, b and c are constants (fixed numeric values).
Create a class to store a quadratic expression.
Note that a quadratic expression is defined by 3 floating-point values - the 3 coefficients a, b, and c.
Your class should include:
- a default constructor that sets all 3 coefficients to 0
- a function member that sets all 3 coefficients of a quadratic expression to new values
- a separate function member for each coefficient that returns its value (these should be constant function members)
- a constant function member to evaluate the quadratic expression for a given value of x.
That is, the function should have 1 parameter, x, and should return the value of
ax2 + bx + c
- Overload the addition operator as follows using a non-member function
quadratic operator+( const quadratic& q1, const quadratic& q2 );
// Postcondition: The return value is the quadratic expression
// that is the sum of q1 and q2. The a coefficient
// of the return value is the sum of the a coefficients
// of q1 and q2. The b and c coefficients are similar.
- overload the multiplication operator as follows using a non-member function
quadratic operator*( double r, const quadratic& q );
// Postcondition: The return value is the quadratic expression
// obtained by multiplying each of q's coefficients by r.
You may want to thoroughly test your class at this point before continuing the assignment.
A real root of a quadratic expression is any double valuex such that
ax2 + bx + c = 0. A quadratic expression can have 0, 1, 2, or an infinite
number of roots. The following cases define the real roots for a quadratic expression. Note that these cases cover all possibilities and do not overlap (they are mutually exclusive).
- If a, b and c are all zero, then every value of x is a real root.
- If a and b are zero and c is non-zero, then there are no real roots.
- If a is zero and b is non-zero, then the only real root is x = -c/b.
- If a is non-zero and b2 < 4ac, then there are no real roots.
- If a is non-zero and b2 = 4ac, then there is one real root x = -b/2a.
- If a is non-zero and b2 > 4ac, then there are two real roots:
-b - sqrt( b2 - 4ac )
x = ________________
2a
-b + sqrt( b2 - 4ac )
x = ________________
2a
- Write a constant member function that returns the number of real roots of a quadratic expression. The possible values are 0, 1, 2, or infinity. For an infinite number of roots, your function should return 3.
- write two constant member functions that return the roots of a quadratic expression. The precondition of both functions should be that the quadratic expression has at least one real root. If the quadratic expression has one real root, both functions should return that root. If the quadratic expression has 2 real roots, one function should return the smaller root and the other should return the larger root. If the quadratic expression has an infinite number of roots, both functions should return zero.