- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey! :giggle:
I am looking at the following:
a) Create a class QuadraticPolyonym that describes a polynomial of second degree, i.e. of the form $P(x)=ax^2+bx+c, a\neq 0$.The coefficients have to be givenas arguments at the construction ofan instance of the class. Implement a method Discriminant that calculates the discriminant $D=b^2-4ac$.
b) Implement a method Roots that returns a dynamic array (std::vector) with the real roots of $p(x)=0$.
c) Support addition of polynomials with the operator $+$. With elements QuadraticPolyonym p1, p2; thepolynomial QuadraticPolyonym p3 = p1 + p2; should describe a polynomial where the coefficients isteh sum of the corresponding coefficients of the polynomials p1 and p2.
d) Write a program that creates two elements of the class QuadraticPolyonym P(x) and Q(x) reading the coefficinets from the input, adding these to get the polyomial R(x) and printing the roots of R(x)=0$ at the output.I have done the following :
It doesn't work as it should yet, but am I thinking in a correct way for the program? Or is this completely wrong? :unsure:
I am looking at the following:
a) Create a class QuadraticPolyonym that describes a polynomial of second degree, i.e. of the form $P(x)=ax^2+bx+c, a\neq 0$.The coefficients have to be givenas arguments at the construction ofan instance of the class. Implement a method Discriminant that calculates the discriminant $D=b^2-4ac$.
b) Implement a method Roots that returns a dynamic array (std::vector) with the real roots of $p(x)=0$.
c) Support addition of polynomials with the operator $+$. With elements QuadraticPolyonym p1, p2; thepolynomial QuadraticPolyonym p3 = p1 + p2; should describe a polynomial where the coefficients isteh sum of the corresponding coefficients of the polynomials p1 and p2.
d) Write a program that creates two elements of the class QuadraticPolyonym P(x) and Q(x) reading the coefficinets from the input, adding these to get the polyomial R(x) and printing the roots of R(x)=0$ at the output.I have done the following :
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
//Question(a)
class QuadraticPolyonym
{
private:
double a, b, c;
public:
double Discriminant(double a, double b, double c) { return b*b-4*a*c; }
//Question (b)
double Roots(double a, double b, double c)
{
int x1, x2;
if (Discriminant(a,b,c) > 0) {
x1 = (-b + sqrt(Discriminant(a,b,c))) / (2*a);
x2 = (-b - sqrt(Discriminant(a,b,c))) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (Discriminant(a,b,c) == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
cout << "Roots are complex" << endl;
}
return 0;
}
//Question (c)
double* OperatorPlus(double x[3], double y[3])
{
double* z=new double[3];
int i;
for( i = 0; i < 3; ++i) {
z[i] = x[i] + y[i];
}
return z;
}};//Question (d)
int main()
{
QuadraticPolyonym R;
int i;
double coefP[3], coefQ[3],result[3];
cout<<"Enter the coefficients of P:\n";
for(i = 0; i < 3; i++) {
cin>>coefP[i];
}
cout<<"Enter the coefficients of Q:\n";
for(i = 0; i < 3; i++){
cin>>coefQ[i];
}
double* result = R.OperatorPlus(coefP, coefQ); //Question (c)
R.Roots(result[0],result[1], result[2]);
return 0;
}
Last edited by a moderator: