Question

In: Computer Science

IN C++ The Quadratic equation is used to solve for the variable x where the function...

IN C++

The Quadratic equation is used to solve for the variable x where the function evaluates to zero. However there is actually two answers returned by the quadratic equation. Can we solve for the quadratic equation using just one call to a function you write?

Hint : create a function that does not 'return' anything. Takes three parameters a,b,c as double.s  

And had two more parameters that are passed by reference to two doubles that are the two answers from the quadratic equation.  

Look on line or in your old math books to who that you tested this with at least three test cases.

this is my code so far I don't know how I should do the next function or if i should just call it in the main already.

#include <iostream>

#include <cmath>

#include <math.h>

using namespace std;

void Quad (double a, double b, double c, double &plusValue, double &minusValue)

{

plusValue = (-b + sqrt(b * b - 4 * a * c)) / (2 *a);

minusValue = (-b - sqrt(b * b - 4 * a * c)) / (2 *a);

}

void Quad(double a, double b, double c, double minus, double plus)

{

}

int main()

{

}

Solutions

Expert Solution

You should just call it in the main already. Your code is fine but it will not work in the case where (b * b - 4 * a * c) becomes negative as you will get complex roots so i have also added that case in code no 2:

code 1 :

#include <iostream>

#include <cmath>

#include <math.h>

using namespace std;

void Quad (double a, double b, double c, double &plusValue, double &minusValue)

{

plusValue = (-b + sqrt(b * b - 4 * a * c)) / (2 *a);

minusValue = (-b - sqrt(b * b - 4 * a * c)) / (2 *a);


}

int main()
{

double plusValue=0,minusValue=0,a,b,c;
cout << "Enter coefficients a, b and c of quadratic Equation (ax2 + bx + c = 0   ): \n";
cin >> a >> b >> c;
Quad(a,b,c,plusValue,minusValue);
cout<<"Roots of Euation : "<<plusValue<<" "<<minusValue;
}

code Snippet:-

OUTPUT:-

CODE CONSIDERING THE CASE WHERE WE GET NEGATIVE discriminant:-

Code no 2:-

#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
void Quad (double a, double b, double c, double &plusValue, double &minusValue,int &flag)
{
plusValue = (-b + sqrt(b * b - 4 * a * c)) / (2 *a);
minusValue = (-b - sqrt(b * b - 4 * a * c)) / (2 *a);
if((b * b - 4 * a * c)<0)
{
flag=1;
plusValue=-b/(2*a);
minusValue=sqrt(-(b * b - 4 * a * c));
}
}
int main()
{
double plusValue=0,minusValue=0,a,b,c;
int flag=0;
cout << "Enter coefficients a, b and c of quadratic Equation (ax2 + bx + c = 0   ): \n";
cin >> a >> b >> c;
Quad(a,b,c,plusValue,minusValue,flag);
if(flag==1)
{
cout << "Roots are complex and different." << endl;
cout << plusValue << "+" << minusValue << "i" << endl;
cout << plusValue << "-" << minusValue << "i" << endl;
}
else{
cout<<"Roots of Euation : "<<plusValue<<" "<<minusValue;}
}

Code snippet:-

Code Output:-


Related Solutions

1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) /...
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) / 2a) and output the two solution with clear explanation could you please do it in MATLAB
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula ( you must do it both ways). Show all steps for each method and put your answer in simplest radical form possible. 2) Which part of the Quadratic Formula can help you to find the Nature of the roots for the Quadratic Equation. Explain how you can find the nature of the roots and provide one Example for each possible case with solution.
Solve the following differential equation: y''+e^{-y}=0 where y is only a function of x.
Solve the following differential equation: y''+e^{-y}=0 where y is only a function of x.
Solve an equation where the input is a complex number. s = x + j(3)^0.5*x Equation:...
Solve an equation where the input is a complex number. s = x + j(3)^0.5*x Equation: s^3+3*s^2+2s+k = 0 Solve for k and x
Say we have a continuous random variable X with density function f(x)=c(1+x3) (where c is a...
Say we have a continuous random variable X with density function f(x)=c(1+x3) (where c is a constant)with support SX =[0,3]. a.) What value of c will make f(x) a valid probability density function. b. )What is the probability that X=2? What is the probability that X is greater than 2? Now say we have an infinite sequence of independent random variables Xi (that is to say X1, X2, X3, ....) with density f(x) stated earlier. c. What is the probability...
Given f(x) = ax2 + k (where f(x) is a quadratic), describe when its reciprocal function...
Given f(x) = ax2 + k (where f(x) is a quadratic), describe when its reciprocal function would and would not have vertical asymptotes. Refer to the values (+,-,0) of a and k in your description. What are all the possible scenarios?
Write a program to input the coefficients of a quadratic equation and solve roots for all...
Write a program to input the coefficients of a quadratic equation and solve roots for all cases (including complex roots). VBA. x=(b ^2) - (4ac) I have it for 2 complex and 2 real and repeated. Is that all cases?
Quadratic Equation.
If a² + b² + c² = 1, then ab + bc + ac lies in which interval
Quadratic Equation
If one root is square of the other root of the equation x²+px+q=0, then find the relation between p and q.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT