The same way you write any other equation in C++. Do the algebra with
pencil and paper (if necessary) and type in the equation following the rules
of the language.
<sonhadorPR@gma il.comwrote in message
news:1156735467 .326839.112140@ p79g2000cwp.goo glegroups.com.. .
How do I write the quadratic equation in C++?
Thank You!
SonhadorPR
>
Quadradic Equation is
ax^2 + bx + c = 0 (where ^2 is squared).
>
For x this becomes:
>
-b +/- Sqrt( b^2 - 4ac )
x = ------------------------
2a
>
Which is known as the Quadratic Formula.
>
In C or C++ langauge terms this is:
x = (-b + sqrt( b*b - 4 * a * c ) ) / 2 * a;
x = (-b - sqrt( b*b - 4 * a * c ) ) / 2 * a;
>
(Yes, there are two possible answers.
More precisely: there are at most two solutions to the equation.
Depending on the inputs, there may be two, one or no solutions for a
particular equation. The somewhat tricky part is to make sure that the
program can handle all three of these outcomes.
"Greg" <greghe@pacbell .netwrote in message
news:1156740277 .976827.220010@ p79g2000cwp.goo glegroups.com.. .
>
Jim Langston wrote:
><sonhadorPR@gm ail.comwrote in message
>news:115673546 7.326839.112140 @p79g2000cwp.go oglegroups.com. ..
How do I write the quadratic equation in C++?
Thank You!
SonhadorPR
>>
>Quadradic Equation is
>ax^2 + bx + c = 0 (where ^2 is squared).
>>
>For x this becomes:
>>
> -b +/- Sqrt( b^2 - 4ac )
>x = ------------------------
> 2a
>>
>Which is known as the Quadratic Formula.
>>
>In C or C++ langauge terms this is:
>x = (-b + sqrt( b*b - 4 * a * c ) ) / 2 * a;
>x = (-b - sqrt( b*b - 4 * a * c ) ) / 2 * a;
>>
>(Yes, there are two possible answers.
>
More precisely: there are at most two solutions to the equation.
Depending on the inputs, there may be two, one or no solutions for a
particular equation. The somewhat tricky part is to make sure that the
program can handle all three of these outcomes.
>
Greg
"Jim Langston" <tazmaster@rock etmail.comwrote in message news:aAtIg.33
>
In C or C++ langauge terms this is:
x = (-b + sqrt( b*b - 4 * a * c ) ) / 2 * a;
x = (-b - sqrt( b*b - 4 * a * c ) ) / 2 * a;
>
Don't you need parentheses there, around the "2 * a" part? Division and
multiplication have the same precedence, so as written, the above would
divide by 2, then multiply by a, instead of dividing by (2 * a), right?
>
"Jim Langston" <tazmaster@rock etmail.comwrote in message news:aAtIg.33
>>
>In C or C++ langauge terms this is:
>x = (-b + sqrt( b*b - 4 * a * c ) ) / 2 * a;
>x = (-b - sqrt( b*b - 4 * a * c ) ) / 2 * a;
>>
>
Don't you need parentheses there, around the "2 * a" part? Division and
multiplication have the same precedence, so as written, the above would
divide by 2, then multiply by a, instead of dividing by (2 * a), right?
Comment