Question

Using C++ For this program you are going to create a class that helps to solve...

Using C++

For this program you are going to create a class that helps to solve the quadratic equation. The equation has the following form:

ax2 + bx + c = 0

The roots of the equation are:

x1 = -b + sqrt(b2 - 4 * a * c) and x2 = -b - sqrt(b2 - 4 * a * c)

The phrase in the parenthesis is called the discriminant. If the value of the discriminant is positive, the equation has two roots. If it is zero, the equation has one root. If it is negative, the equation has no roots.

Create a class named Quadratic that finds the roots of a quadratic equation when the coefficients a, b, and c are given.

Hint: You should create three individual coefficient private members.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include<iostream>
#include<math.h>
using namespace std;
class Quadratic
{
/**************************************
PRIVATE MEMBERS
***************************************/
private:
float a,b,c;
/**************************************
PUBLIC MEMBERS
***************************************/
public:
Quadratic(float a,float b,float c)
{
this->a = a;
this->b = b;
this->c = c;
}
void result()
{
float discriminant = pow(b,2)-4*a*c;
if(discriminant<0)
{
cout<<"No roots.\nDiscriminant is less than 0 so the roots are not real.";
return;
}
else if(discriminant==0)
{
cout<<"Root of the Equation is : "<<(-b/(2*a))<<"\nRoot is single and unique.";
}
else
{
cout<<"Roots of the Equation are : "<<(sqrt(discriminant)-b)/(2*a)<<" and "<<(-sqrt(discriminant)-b)/(2*a);
}
}
};
int main()
{
float a,b,c;
cout<<"Enter the coefficients of x^2, x and the constant respectively : \n";
cin>>a>>b>>c;
Quadratic obj(a,b,c);
obj.result();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using C++ For this program you are going to create a class that helps to solve...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • reword m the program into a design document diregard the m Write a C++ program that...

    reword m the program into a design document diregard the m Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic equation ax2 + bx + c = 0 (where a is not zero) are given by the formula -b + b2 - 4ac 2a The value of the discriminant b2 - 4ac determines the nature of roots. If the value of the discriminant is zero, then the equation has a single...

  • Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c...

    Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c 0. The class contains: • The data fields a, b, and c that represent three coefficients. . An initializer for the arguments for a, b, and c. • Three getter methods for a, b, and c. • A method named get Discriminant() that returns the discriminant, which is b- 4ac The methods named getRoot 1() and getRoot 2() for returning the two roots of...

  • Pseudocode and PYTHON source code, thanks! Program 1: Design (pseudocode) and implement (source code) a class...

    Pseudocode and PYTHON source code, thanks! Program 1: Design (pseudocode) and implement (source code) a class (name it QuadraticEquation) that represents a quadratic equation of the form of ax2+ bx + x = 0. The class defines the following variables and methods: Private data field a, b, and c that represent three coefficients. A constructor for the arguments for a, b, and c. Three get methods for a, b, and c. Method getDiscriminant()returns the discriminant value, which is disc =...

  • In Python. The two roots of a quadratic equation ax^2 + bx + c = 0...

    In Python. The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r2 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...

  • The two roots of the quadratic equation ax2 + bx + c = 0 can be...

    The two roots of the quadratic equation ax2 + bx + c = 0 can be found using the quadratic formula as -b+ v62 – 4ac . -b-v6² – 4ac 1 X1 = - and x2 = 2a 2a When b2 – 4ac < 0 this yields two complex roots - -b V4ac – 62 -b Vac – 6² x1 = = +. . 2a 2a i. and x2 = . za 2al Using the quadratic formula the roots of...

  • java code Design a class named QuadraticEquation for a quadratic equation with real coefficients ax? +...

    java code Design a class named QuadraticEquation for a quadratic equation with real coefficients ax? + bx + x = 0 where a = 0. The class contains: (a) Private data fields a, b, and c that represent three coefficients. (b) A constructor taking arguments for a, b, and c. (c) Getter and setter methods for each attribute field. (d) A method named getDiscriminant() that returns the discriminant, 62 - 4ac. (e) A method named hasReal Solution that determines If...

  • 4-6 on matlab 4. Write a program in a script file that determines the real roots...

    4-6 on matlab 4. Write a program in a script file that determines the real roots of a quadratic equation ax2+bx+c 0 When the file runs, it asks the user to enter the values of the constants a, b, and c. To calculate the roots of the equation the program calculates the discriminant D, given by: D b2-4ac When D 0, the program displays message "The equation has two roots," and the roots are displayed in the next line. When...

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • help KKKL1164 4. The C program presented below has some syntax and logic errors. Analyse it...

    help KKKL1164 4. The C program presented below has some syntax and logic errors. Analyse it and highlight the errors, explaining how the errors can be corrected. This program attempts to solve a quadratic equation. #include <stdio.h> #include <math.h> int Main() const int a, b, Ci double doro, disc printf ("Lets find the root (s) of a printf ("ax 2 bx c. \n"); printf ("Please enter the coefficient a: scanf ("d", &c); printf ("Please enter the coefficient b: "); scanf...

  • Java Programming Question. I am writing a code to calculate the roots of the quadratic equation...

    Java Programming Question. I am writing a code to calculate the roots of the quadratic equation based on the values of the coefficients A, B, and C. I am supposed to enter 5 values for each coefficient, and the output should be five different answers (see example) using a for loop and if else statements. However, when I run my code, I am only able to enter one value for each coefficient and the output is one answer repeated five...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT