Question

A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...

A quadratic equation is generally represented as,

ax^2 + bx + c

The root(s) of the above quadratic equation is computed using the following formula,

root1 = (-b + sqrt(D))/ 2a
root2 = (-b - sqrt(D))/2a

Where D is the discriminant of the quadratic equation and is computed as,
D = b^2 - 4ac

Given the value of D, the roots of a quadratic equation can be categorized as follows,
D > 0 : Two distinct real roots
D = 0 : Two equal real roots
D < 0 : Two distinct complex roots

WAP in C++ to develop a quadratic equation solver using functions. The users will input the values of a, b, and c. Your program must validate that the value of a is not equal to zero (If the input is invalid, user should be prompted an error message and asked to renter only the invalid input). Functions should be written to carry out
a. the input process
b. validate the inputs
c. solve the equation
d. the output process

Also note that all of the above functions, with the exception of validate function (bullet "b") should be coded using "call by reference" method. You are free to use any other function you might need/want (call by value or call by reference).

Output should have the information on what type of roots (two real distinct, two real equal or two complex distinct) and the values of the roots. In this regard, computation of complex roots should not result in a "nan" and "-nan" output and it should be solved and printed in "x + yi" and "x-yi" format (x is the real part and yi is the imaginary part).

The whole program should be able to run as many times the user wants.

Sample output format (You do not have to follow the structure given here but the required information should be present):
Your quadratic equation, 2x^2 + 4x + 2 has two real equal roots.
The value of the roots are,
root1 = -1
root2 = -1

Inputs to test (a, b, c):
1. (0 , 5 ,2)
2. (4, 7 ,3)
3. (2, 9, 11)
4. (3, 6, 3)

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

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

//Function declaration
int getInput(char coeff);
int validate(int &n);
pair<double, double> solve(int a, int b , int c);
void display(int a, int b , int c, double root1, double root2);

//Gets the user input for the coefficients
int getInput(char coeff)
{
int n = 0;
do
{
cout << "Enter the value of " << coeff << ": ";
cin >> n;
} while(validate(n));
}

//Checks if the value of n is zero
int validate(int &n)
{
if(n == 0)
{
cout << "The value cannot be zero. Please try again" << endl;
return 1;
}
return 0;
}

//Solves the equation and returns the root(s)
pair<double, double> solve(int a, int b , int c)
{
double root1 = 0, root2 = 0;
  
//Calculations
int discriminant = pow(b, 2) - (4 * a * c);
if(discriminant < 0)
{
root1 = NAN;
root2 = -NAN;
}
else if(discriminant == 0)
{
root1 = ((-1) * b) / (2 * a);
root2 = root1;
}
else
{
root1 = (((-1) * b) + sqrt(discriminant)) / (2 * a);
root2 = (((-1) * b) - sqrt(discriminant)) / (2 * a);
}
  
return make_pair(root1, root2);
}

//Displays the result
void display(int a, int b , int c, double root1, double root2)
{
cout << endl;
cout << "Your quadratic equation, " << a << "x^2 ";
if(b < 0)
cout << "- " << b;
else
cout << "+ " << b;
  
if(c < 0)
cout << "x - " << c << " has ";
else
cout << "x + " << c << " has ";
  
//Chek if roots are real or complex
if(root1 == -1)
cout << "two distinct complex roots.";
else if(root1 == root2)
cout << "two equal real roots.";
else if(root1 != root2)
cout << "two distinct real roots.";
  
//Display roots
cout << endl << "The value of the roots are, " << endl;
cout << "root1 = " << root1 << endl;
cout << "root2 = " << root2 << endl << endl;
}

int main()
{
char ch = 'y';
  
while((ch == 'y') || (ch == 'Y')) {
int a, b, c;
  
//Accept values of a, b, c from user
a = getInput('a');
b = getInput('b');
c = getInput('c');
  
//Solve Equation
pair<double, double> roots = solve(a, b, c);
  
//Display results
display(a, b, c, roots.first, roots.second);
  
//Ask if the user wants to continue
cout << "Do you want to continue [y/n]: ";
cin >> ch;
  
cout << endl;
}
}

SAMPLE OUTPUT:

Enter the value of a: 0 The value cannot be zero. Please try again Enter the value of a:4 Enter the value of b: 7 Enter the v

Add a comment
Know the answer?
Add Answer to:
A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...
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
  • 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...

  • C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠...

    C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...

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

  • write a C programming code for the following prompt. please use stucture concept and test if...

    write a C programming code for the following prompt. please use stucture concept and test if the code works perfectly. Project Description: In this project, you will write a program to calculate the roots of a quadratic equation. Structure concepts will be used in this project. Your program will prompt the user to enter the coefficients of a quadra coefficientsType. Then it will compute the roots of the quadratic equation and store the result in a structure variable of type...

  • I need help writing the test case in C++ code can you help? This is apart...

    I need help writing the test case in C++ code can you help? This is apart of my professors pseudo code for the project. // TEST CASE 1 // // DESCRIPTION // Coefficients a and c are > 0 and b < 0 so the equation should be output as: // // p(x) = 122.50000x^2 - 6.70000x + 3.00000 = 0 // // where we note that the operator printed before b is subtraction and we actually output -b rather...

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

  • please answer this question with python. Write a program to solve the quadratic equation ax^2 +...

    please answer this question with python. Write a program to solve the quadratic equation ax^2 + bx + c = 0 using the standard quadratic formula x = -b plusminus Squareroot b^2 - 4ac/2a or the alternative formula x = 2c/-b Squareroot b^2 - 4ac. Your program should accept values for the coefficients a, b, and c as input and produce the two roots of the equation as output. Your program should detect when the roots are imaginary, but need...

  • Question 4: Provide at least 4 test cases. Prompt the user to input 3 doubles, a,...

    Question 4: Provide at least 4 test cases. Prompt the user to input 3 doubles, a, b and c. Which will represent the coefficients in the quadratic equation ax2 + bx + c = 0. Print out the solutions (if any) of the quadratic equation. If no root exists (this happens if a == 0, or b2 <4ac) print the message No real root. Sample input/ user entries shown in red Corresponding output Enter a, b and c which represent...

  • Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does...

    Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does not equal zero are as follows. r1=( −b+√b(squared)−4ac)/2a r2=(−b−√b(squared)−4ac)/2a if b(squared)−4ac <0, equation doesn’t have real roots. If it is 0 there is one root(r1=r2). Write a Java program to read a,b and c from keyboard and find the roots, if they exist. Note: You need to have a method that takes 3 real values as arguments

  • Help with MATLAB. i did like input('enter the coefficients of a quadratic equation "Ax2 + Bx...

    Help with MATLAB. i did like input('enter the coefficients of a quadratic equation "Ax2 + Bx + C = 0"') fx=(-B+sqrt(B^2+4*A*C))/(2*A); i just dont know how i can ask the user to input three (A,B,C)? thanks! EXERCISE 6 Ask user to enter the coefficients of a quadratic equation, Ax² + Bx + C = 0, i.e. A, B, and C, and calculate the roots of the equation using the quadratic formula, ., --B+VB? - 4AC 2A

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
ADVERTISEMENT