COSC 2436 – Data Structures Program 2 – Quadratic Expressions Be sure to read th
ID: 3668243 • Letter: C
Question
COSC 2436 – Data Structures Program 2 – Quadratic Expressions Be sure to read through Chapter 4 of the textbook before starting this assignment. There is additional material in Blackboard/Chapter Materials. Be sure to read all of the Blackboard/Program Requirements. Important: For programming assignments 2 and on you must follow all Blackboard/Program Requirements. The objective is to construct a comprehensive class that can be used for many different applications in client programs. You will create a Dev-C++ project (separate compilation). Your project should be divided into separate files - a class header file, a class implementation file, and the client (driver) program. Be sure that your driver includes code to test all your class functions and non-member functions. I have provided the attached driver that you should use as a template for your client program. Please include menu items 1 thru 8 as described and in the order described so that it will make your code easier to test. For this assignment, I want you to give me a copy of your program output (output.txt). Print enough information to show that your program is working correctly. Quadratic Class 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 have the following member functions: Quadratic - a default constructor that initializes all 3 coefficients to 0 set - a function member that sets all 3 coefficients of an existing quadratic expression to new values getA, getB, getC - a separate function member for each coefficient that returns its value (these should be constant function members) evaluate - 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 Write the following overloaded non-member functions (you may not use friend) : 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 value x 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 Add the follow member functions to your class: numRoots - 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. minRoot, maxRoot - 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. Deliverables See Blackboard/Program Requirements 12. Deliverables
Explanation / Answer
main.cpp
#include <iostream>
using namespace std;
#include "Quadratic.h"
// function prototype
void menu();
int main()
{
//declare variables
Quadratic q1;
char choice;
cout << "A quadratic called q1 has been created and initialized to a default value." << endl;
do
{
menu();
// choice
cout << " Enter your choice: ";
cin >> choice;
if (choice == '1')
{
// code to test display() function (if you had them write one)
q1.display();
cout << " ";
}
else if (choice == '2')
{
// code to test get functions
cout << " a = " << q1.getA() << endl;
cout << " b = " << q1.getB() << endl;
cout << " c = " << q1.getC() << endl;
cout << " ";
}
else if (choice == '3')
{
// code to test set function
float qa, qb, qc;
cout << "Please enter three number for a, b and c. ";
cout << "a, b, c = ";
cin >> qa >> qb >>qc;
q1.set(qa, qb, qc);
cout << " ";
}
else if (choice == '4')
{
// code to test evaluate function
float X;
cout << "Please enter a number for x." << endl;
cout << "x = ";
cin >> X;
cout << "The answer is: " << q1.evaluate(X) << endl;
cout << " ";
}
else if (choice == '5')
{
// code test number of roots function
cout << "The equation: ";
q1.display();
cout << "has " << q1.numRoots() << " real roots." << endl;
cout << " ";
}
else if (choice == '6')
{
// code to test small root and large root functions
if (q1.numRoots() == 0)
{
cout << "No real roof." << endl;
}
else
{
cout << "The maxroot is: " << q1.maxRoot() << endl;
cout << "The minroot is: " << q1.minRoot() << endl;
}
cout << " ";
}
else if (choice == '7')
{
// create a second quadratic from user inputted coefficients
// and display the sum of q1 and the second quadratic
float q2a, q2b, q2c;
Quadratic q2;
cout << "Please enter other three number for second quadratic`s coefficients." << endl;
cout << "2nd a, b, c = ";
cin >> q2a >>q2b >> q2c;
q2.set(q2a, q2b, q2c);
Quadratic sumofco = q1 + q2;
cout << "The sum of a is: " << sumofco.getA() << endl;
cout << "The sum of b is: " << sumofco.getB() << endl;
cout << "The sum of c is: " << sumofco.getC() << endl;
cout << " ";
}
else if (choice == '8')
{
// code to test multiplication operator
double R;
cout << "Please enter a number for r." << endl;
cout << "r = ";
cin >> R;
Quadratic proofco = R * q1;
cout << "The product of a is: " << proofco.getA() << endl;
cout << "the product of b is: " << proofco.getB() << endl;
cout << "the product of c is: " << proofco.getC() << endl;
cout << " ";
}
else
cout << "Not a valid option ";
} while (choice != '9');
return 0;
}
void menu()
{
cout << "Please choose one of the following: "
<< " 1 - Display q1 "
<< " 2 - Display the coefficients of q1 "
<< " 3 - Modify the coefficients of q1 "
<< " 4 - Display the value of q1 at a given value of x "
<< " 5 - Display the number of roots for q1 "
<< " 6 - Display the roots of q1 "
<< " 7 - Display the sum of q1 and a second quadratic "
<< " 8 - Display the product of q1 and a given floating-point value "
<< " 9 - Exit" << endl;
}
Quadratic.h
#ifndef QUADRATIC_H
#define QUADRATIC_H
#include <iostream>
#include <cmath>
using namespace std;
class Quadratic
{
private:
float a,b,c;
public:
//constructor
Quadratic();
void display();
//Set the value to a, b, c
void set(float Numa, float Numb, float Numc);
//return value
float getA() const;
float getB() const;
float getC() const;
//evaluate the function by giving values
float evaluate(float) const;
int numRoots();
float minRoot();
float maxRoot();
};
Quadratic operator+(const Quadratic &q1, const Quadratic &q2);
Quadratic operator*(double r, const Quadratic &q);
#endif
Quadratic.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "Quadratic.h"
Quadratic::Quadratic()
{
a = b = c = 0;
}
void Quadratic::set(float Numa, float Numb, float Numc)
{
a = Numa;
b = Numb;
c = Numc;
}
void Quadratic::display()
{
cout << a << "(x^2) + " << b << "(x) + " << c << endl;
}
float Quadratic::evaluate(float x) const
{
return a*(x*x) + b*x + c;
}
float Quadratic::getA() const
{
return a;
}
float Quadratic::getB() const
{
return b;
}
float Quadratic::getC() const
{
return c;
}
int Quadratic::numRoots()
{
int numR = -999;
if (a == 0 && b == 0 && c == 0)
{
numR = 3;
}
else if ((a ==0 && b == 0 && (c != 0)) || ((a != 0) && ((b*b) < (4*a*c))))
{
numR = 0;
}
else if ((a == 0) && (b != 0))
{
numR = 1;
}
else if ((a != 0) && ((b*b) == (4*a*c)))
{
numR = 1;
}
else if ((a != 0) && ((b*b) > (4*a*c)))
{
numR = 2;
}
else
{
numR = -2;
};
return numR;
}
float Quadratic::maxRoot()
{
float Mx = -999;
if (a == 0 && b == 0 && c == 0)
{
Mx = 0;
}
else if ((a == 0) && (b != 0))
{
Mx = -c/b;
}
else if ((a != 0) && ((b*b) == (4*a*c)))
{
Mx = -b/(2*a);
}
else if ((a > 0) && ((b*b) > (4*a*c)))
{
Mx = (-b + sqrt((b*b)-(4*a*c)))/(2*a);
}
else if ((a < 0) && ((b*b) > (4*a*c)))
{
Mx = (-b - sqrt((b*b)-(4*a*c)))/(2*a);
}
else
{
cout << "No real roots." << endl;
};
return Mx;
}
float Quadratic::minRoot()
{
float Mn = -999;
if (a == 0 && b == 0 && c == 0)
{
Mn = 0;
}
else if ((a == 0) && (b != 0))
{
Mn = -c/b;
}
else if ((a != 0) && ((b*b) == (4*a*c)))
{
Mn = -b/(2*a);
}
else if ((a < 0) && ((b*b) > (4*a*c)))
{
Mn = (-b + sqrt((b*b)-(4*a*c)))/(2*a);
}
else if ((a > 0) && ((b*b) > (4*a*c)))
{
Mn = (-b - sqrt((b*b)-(4*a*c)))/(2*a);
}
else
{
cout << "No real roots." << endl;
};
return Mn;
}
Quadratic operator+ (const Quadratic &q1, const Quadratic &q2)
{
Quadratic q3;
q3.set(q1.getA() + q2.getA(), q1.getB() + q2.getB(), q1.getC() +q2.getC());
return q3;
}
Quadratic operator* (double r, const Quadratic &q)
{
Quadratic tempq;
tempq.set(r * q.getA(), r * q.getB(), r * q.getC());
return tempq;
}
Output
A quadratic called q1 has been created and initialized to a default value.
Please choose one of the following:
1 - Display q1
2 - Display the coefficients of q1
3 - Modify the coefficients of q1
4 - Display the value of q1 at a given value of x
5 - Display the number of roots for q1
6 - Display the roots of q1
7 - Display the sum of q1 and a second quadratic
8 - Display the product of q1 and a given floating-point value
9 - Exit
Enter your choice: 1
0(x^2) + 0(x) + 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.