Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads 3 integer values representing the integer coefficient

ID: 3626748 • Letter: W

Question


Write a program that reads 3 integer values representing the integer coefficients of a quadratic polynomial. The first number represents the coefficient of X2 and must not be zero, the second number represents the coefficient of X, and the third number represents the constant coefficient.

The purpose of the program is to compute the roots of the square polynomial.

If the polynomial has real roots it computes the two roots, otherwise it writes a message indicating that the polynomial has no real roots.

If the user enters zero for the first integer, warn the user that the coefficient must not be zero, and set it to 1.

Design and implement the program using as many functions as necessary. The main consists only of data declaration and invocation of functions. Also write a function that displays the purpose of the program when the user starts its execution.

To compute the roots for the polynomial aX2 + bX + c use the formula
-b +/- sqrt( b2 - 4ac)
-------------------------------
2a

You must include the library <cmath> which contains the sqrt function.

Example of Input: 1 0 1 (represents the polynomial X2+ 1)
Example of Output: The polynomial X**2 + 1 has no real roots.
Example of Input: 2 1 -1 (represents the polynomial 2X2+ X - 1)
Example of Output: the polynomial 2X**2 + X -1 has two real roots. The roots are 1/2 and -1.
Example of Input: 1 -25
Output: the polynomial X**2 - 1 has one root. The root is 5

Explanation / Answer

please rate - thanks

#include<iostream>
#include<cmath>
using namespace std;
void getcoef(int&,int&,int&);
double getd(int,int,int);
void getroots(int,int,int,double,double);
double getden(int);
int main()
{int a,b,c;
double discriminant,denom;
getcoef(a,b,c);
discriminant=getd(a,b,c);
denom=getden(a);
getroots(a,b,c,denom,discriminant);
system("pause");
return 0;
}
void getroots(int a,int b,int c ,double denom,double discriminant)
{double r1,r2;
cout<<"The roots of "<<a<<"x^2+"<<b<<"x+"<<c<<" are: ";
if(a==0)
    cout<<"undefined ";
else if(discriminant<0)
     cout<<"imaginary ";
else
    {r1=(-b+sqrt(discriminant))/denom;
    r2=(-b-sqrt(discriminant))/denom;
    if(r1==r2)
        cout<<"the same. the root is "<<r1<<endl;
    else
        cout<<r1<<" and "<<r2<<endl;
    }
}
double getden(int a)
{return 2.*a;
}
double getd(int a,int b,int c)
{return pow(b,2.)-4.*a*c;
}
void getcoef(int& a,int& b,int& c)
{cout<<"For the equation ax^2+bx+c: ";
cout<<"Enter a: ";
cin>>a;
if(a==0)
    {cout<<"must not be 0, setting to 1 ";
    a=1;
    }
cout<<"Enter b: ";
cin>>b;
cout<<"Enter c: ";
cin>>c;
}