COSC 2415 - Data Structures and
ITSE 2445 Data Structures
Bob Comer, Professor of Computer Studies
Program 2 - Quadratic Expressions
Be sure to read through Chapter 4 of the textbook before starting this assignment.
Important: For programming assignment 2, be sure to follow these
program requirements.
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 initializes all 3 coefficients to 0
- a function member that sets all 3 coefficients of an existing quadratic expression to new values
- a separate function member for each coefficient that returns its value (these should be constant member functions)
- a constant member function called display that displays the quadratic object
in a format similar to 3x^2 + 2x + 5. Your function can display the object to cout.
Alternatively, you can add a parameter to the function to allow the user to specify an output stream
(similar to the display functions in the textbook examples).
- a constant function member to evaluate the quadratic expression for a given value of x.
That is, the function should have 1 parameter, a floating-point value called 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.
Your class member functions should have prototypes:
Quadratic();
void set( double newA, double newB, double newC );
double getA() const;
double getB() const;
double getC() const;
void display() const; -or- void display( ostream& out ) const;
double evaluate( double x ) const;
int getNumRoots() const;
double getSmallRoot() const;
double getLargeRoot() const;
The non-member functions associated with your class should have prototypes:
Quadratic operator+( const Quadratic& q1, const Quadratic& q2 );
Quadratic operator*( double r, const Quadratic& q );
Return to C++ Home Page
Copyright: © 2015 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: September 20, 2015