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:

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).

  1. If a, b and c are all zero, then every value of x is a real root.
  2. If a and b are zero and c is non-zero, then there are no real roots.
  3. If a is zero and b is non-zero, then the only real root is x = -c/b.
  4. If a is non-zero and b2 < 4ac, then there are no real roots.
  5. If a is non-zero and b2 = 4ac, then there is one real root x = -b/(2a).
  6. 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

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