Discussion in 'Computer Support' started by Sunny, Feb 25, 2006.

1. ### SunnyGuest

Hello

does anyone know how to write a code in Borland C++ to find the
solutions of a quadratic ax^2 + bx + c = 0

for any parameter a, b, and c.

the solutions of which can either be Real or Complex.

(It is really the part of how to incorporate the Complex Number system
into the code that i can not figure out.)

any help would be much appreciated.

thanks.

Sunny, Feb 25, 2006

2. ### Mike EasterGuest

Sunny wrote:

> does anyone know how to write a code in Borland C++ to find the
> solutions of a quadratic ax^2 + bx + c = 0

Is this a homework problem, or what?

--
Mike Easter

Mike Easter, Feb 25, 2006

3. ### 7Guest

Sunny wrote:

> Hello
>
> does anyone know how to write a code in Borland C++ to find the
> solutions of a quadratic ax^2 + bx + c = 0
>
> for any parameter a, b, and c.
>
> the solutions of which can either be Real or Complex.
>
> (It is really the part of how to incorporate the Complex Number system
> into the code that i can not figure out.)
>
> any help would be much appreciated.
>
> thanks.

For students to lecturers and professors;
boot up quantian or knoppix live dvd edition,
run xmaxima, and it will solve your equations
algebraically.
Hundreds of livecds and livedvds to choose from here..
http://www.livecdlist.com
All open source, free and comes with source code.

7, Feb 25, 2006
4. ### old jonGuest

"Sunny" <> wrote in message
news:...
> Hello
>
> does anyone know how to write a code in Borland C++ to find the
> solutions of a quadratic ax^2 + bx + c = 0
>
> for any parameter a, b, and c.
>
> the solutions of which can either be Real or Complex.
>
> (It is really the part of how to incorporate the Complex Number system
> into the code that i can not figure out.)
>
> any help would be much appreciated.
>
> thanks.
>

Try this one Sunny:

--
bw..OJ

old jon, Feb 25, 2006
5. ### Rich WilsonGuest

"Sunny" <> wrote in message
news:...
> Hello
>
> does anyone know how to write a code in Borland C++ to find the
> solutions of a quadratic ax^2 + bx + c = 0
>
> for any parameter a, b, and c.
>
> the solutions of which can either be Real or Complex.

What's wrong with the old
(-b +/- sqrt (b^2 - 4*a*c)) / 2*a
formula?

I'd calculate the bit inside the sqrt() and see if it's negative or not,
then write different code to handle the real and complex solutions. IIRC the
complex solutions are still going to be of the form
A +/- Bi
where A and B are pretty easy to calculate using regular (real) arithmetic
so it's mostly a question of how your code is going to display the
solutions.

Rich Wilson, Feb 25, 2006
6. ### why?Guest

On 25 Feb 2006 07:56:23 -0800, Sunny wrote:

>Hello
>
>does anyone know how to write a code in Borland C++ to find the
>solutions of a quadratic ax^2 + bx + c = 0

Yes, it doesn't matter if it's Borland.

<snip>

Me

why?, Feb 25, 2006
7. ### SunnyGuest

i think i have found a solution, but i keep on getting the wrong answer
can somebody see where the error is??????????????????/

int main()
{
double a, b, c;
cout << "Please enter the three coefficients a, b and c :";
cin >> a >> b >> c;

double temp = b*b - 4*a*c; // temporary variable - determines
whether roots are real or complex.

if (temp > 0.0) //temp is positive - real roots
{
double Root1 = (-b + sqrt(temp)) / (2 * a);
double Root2 = (-b - sqrt(temp)) / (2 * a);
cout << "There are two real roots." << endl <<
"The roots are " << Root1 << " and " << Root2 << endl;
}
else if (temp < 0.0) //temp is negative - complex roots
{
double Real = -b / (2 * a);
double Imag = sqrt(c / a - b * b / (4 * a * a));
cout << "There are two complex roots." << endl <<
"The roots are " << Real << " +/- " << Imag << " j" << endl;
}
else //roots are both the same
{
double Root = -b / (2 * a);
cout << "There are two identical roots." << endl <<
"Both roots are " << Root << endl;
}
return 0;
}

Sunny, Feb 25, 2006
8. ### Rich WilsonGuest

"Sunny" <> wrote in message
news:...
>i think i have found a solution, but i keep on getting the wrong answer
> can somebody see where the error is??????????????????/
>
> int main()
> {
> double a, b, c;
> cout << "Please enter the three coefficients a, b and c :";
> cin >> a >> b >> c;
>
> double temp = b*b - 4*a*c; // temporary variable - determines
> whether roots are real or complex.
>
> if (temp > 0.0) //temp is positive - real roots
> {
> double Root1 = (-b + sqrt(temp)) / (2 * a);
> double Root2 = (-b - sqrt(temp)) / (2 * a);
> cout << "There are two real roots." << endl <<
> "The roots are " << Root1 << " and " << Root2 << endl;
> }
> else if (temp < 0.0) //temp is negative - complex roots
> {
> double Real = -b / (2 * a);
> double Imag = sqrt(c / a - b * b / (4 * a * a));

Shouldnt't that be sqrt(-temp)/(2*a) ?

Rich Wilson, Feb 25, 2006
9. ### why?Guest

On 25 Feb 2006 10:09:38 -0800, Sunny wrote:

>i think i have found a solution, but i keep on getting the wrong answer
>can somebody see where the error is??????????????????/

So you make this easy by pointing out
a) it's the real , complex or same root answer that is wrong
b) it's a programming fault.
c) you don't cout variables in intermediate steps to check the root1,
root2 , real , imag , temp or anything is correct before you go on to
use it later.
d) no sample input with expected / actual output. This most likely will
fix the error you have somewhere.

>int main()
>{
> double a, b, c;
> cout << "Please enter the three coefficients a, b and c :";
> cin >> a >> b >> c;
>
> double temp = b*b - 4*a*c; // temporary variable - determines
>whether roots are real or complex.
>
> if (temp > 0.0) //temp is positive - real roots
> {
> double Root1 = (-b + sqrt(temp)) / (2 * a);
> double Root2 = (-b - sqrt(temp)) / (2 * a);
> cout << "There are two real roots." << endl <<
> "The roots are " << Root1 << " and " << Root2 << endl;
> }
> else if (temp < 0.0) //temp is negative - complex roots
> {
> double Real = -b / (2 * a);
> double Imag = sqrt(c / a - b * b / (4 * a * a));

It would help to have comments to make sure what you want / are aiming
for as an answer is the same as the formula you are using.

> cout << "There are two complex roots." << endl <<
> "The roots are " << Real << " +/- " << Imag << " j" << endl;
> }
> else //roots are both the same
> {
> double Root = -b / (2 * a);
> cout << "There are two identical roots." << endl <<
> "Both roots are " << Root << endl;
> }
> return 0;
>}

Me

why?, Feb 25, 2006
10. ### ClogwogGuest

7 wrote:
> Sunny wrote:
>
>> Hello
>>
>> does anyone know how to write a code in Borland C++ to find the
>> solutions of a quadratic ax^2 + bx + c = 0
>>
>> for any parameter a, b, and c.
>>
>> the solutions of which can either be Real or Complex.
>>
>> (It is really the part of how to incorporate the Complex Number
>> system into the code that i can not figure out.)
>>
>> any help would be much appreciated.
>>
>> thanks.

>
>
> For students to lecturers and professors;
> boot up quantian or knoppix live dvd edition,
> run xmaxima, and it will solve your equations
> algebraically.
> Hundreds of livecds and livedvds to choose from here..
> http://www.livecdlist.com
> All open source, free and comes with source code.

You fucking spamtard!

Clogwog, Feb 25, 2006
11. ### Rick MerrillGuest

why? wrote:
> On 25 Feb 2006 10:09:38 -0800, Sunny wrote:
>
>
>>i think i have found a solution, but i keep on getting the wrong answer
>>can somebody see where the error is??????????????????/

>
>
> So you make this easy by pointing out
> a) it's the real , complex or same root answer that is wrong
> b) it's a programming fault.
> c) you don't cout variables in intermediate steps to check the root1,
> root2 , real , imag , temp or anything is correct before you go on to
> use it later.
> d) no sample input with expected / actual output. This most likely will
> fix the error you have somewhere.
>
>
>>int main()
>>{
>> double a, b, c;
>> cout << "Please enter the three coefficients a, b and c :";
>> cin >> a >> b >> c;
>>
>> double temp = b*b - 4*a*c; // temporary variable - determines
>>whether roots are real or complex.
>>
>> if (temp > 0.0) //temp is positive - real roots
>> {
>> double Root1 = (-b + sqrt(temp)) / (2 * a);
>> double Root2 = (-b - sqrt(temp)) / (2 * a);
>> cout << "There are two real roots." << endl <<
>> "The roots are " << Root1 << " and " << Root2 << endl;
>> }
>> else if (temp < 0.0) //temp is negative - complex roots
>> {
>> double Real = -b / (2 * a);
>> double Imag = sqrt(c / a - b * b / (4 * a * a));

>
>
> It would help to have comments to make sure what you want / are aiming
> for as an answer is the same as the formula you are using.

Yes,and as someone else pointed out the above line should be
double Imag = sqrt(-temp)/2*a
for speed, accuracy and clarity.
To fix the error add parenthesis!

>
>
>> cout << "There are two complex roots." << endl <<
>> "The roots are " << Real << " +/- " << Imag << " j" << endl;
>> }
>> else //roots are both the same
>> {
>> double Root = -b / (2 * a);
>> cout << "There are two identical roots." << endl <<
>> "Both roots are " << Root << endl;
>> }
>> return 0;
>>}

>
>
>
> Me

Rick Merrill, Feb 25, 2006
12. ### why?Guest

On Sat, 25 Feb 2006 15:47:40 -0500, Rick Merrill wrote:

>why? wrote:
>> On 25 Feb 2006 10:09:38 -0800, Sunny wrote:
>>
>>
>>>i think i have found a solution, but i keep on getting the wrong answer
>>>can somebody see where the error is??????????????????/

>>
>>
>> So you make this easy by pointing out
>> a) it's the real , complex or same root answer that is wrong
>> b) it's a programming fault.
>> c) you don't cout variables in intermediate steps to check the root1,
>> root2 , real , imag , temp or anything is correct before you go on to
>> use it later.
>> d) no sample input with expected / actual output. This most likely will
>> fix the error you have somewhere.
>>
>>
>>>int main()
>>>{
>>> double a, b, c;
>>> cout << "Please enter the three coefficients a, b and c :";
>>> cin >> a >> b >> c;
>>>
>>> double temp = b*b - 4*a*c; // temporary variable - determines
>>>whether roots are real or complex.
>>>
>>> if (temp > 0.0) //temp is positive - real roots
>>> {
>>> double Root1 = (-b + sqrt(temp)) / (2 * a);
>>> double Root2 = (-b - sqrt(temp)) / (2 * a);
>>> cout << "There are two real roots." << endl <<
>>> "The roots are " << Root1 << " and " << Root2 << endl;
>>> }
>>> else if (temp < 0.0) //temp is negative - complex roots
>>> {
>>> double Real = -b / (2 * a);
>>> double Imag = sqrt(c / a - b * b / (4 * a * a));

>>
>>
>> It would help to have comments to make sure what you want / are aiming
>> for as an answer is the same as the formula you are using.

>
>Yes,and as someone else pointed out the above line should be
> double Imag = sqrt(-temp)/2*a
>for speed, accuracy and clarity.
>To fix the error add parenthesis!

Hence my remark placed at a *strategic* point, correcting the errors in
the maths doesn't help the OP really solve the problem, it's a fix sure
but a little double checking / understanding of the equation works much
better.
More so since the programming aspect isn't an issue, it's only a wrapper
around the maths and is so simple it's less likely to be the cause of

<snip>

Me

why?, Feb 25, 2006
13. ### 7Guest

Clogwog wrote:

> 7 wrote:
>> Sunny wrote:
>>
>>> Hello
>>>
>>> does anyone know how to write a code in Borland C++ to find the
>>> solutions of a quadratic ax^2 + bx + c = 0
>>>
>>> for any parameter a, b, and c.
>>>
>>> the solutions of which can either be Real or Complex.
>>>
>>> (It is really the part of how to incorporate the Complex Number
>>> system into the code that i can not figure out.)
>>>
>>> any help would be much appreciated.
>>>
>>> thanks.

>>
>>
>> For students to lecturers and professors;
>> boot up quantian or knoppix live dvd edition,
>> run xmaxima, and it will solve your equations
>> algebraically.
>> Hundreds of livecds and livedvds to choose from here..
>> http://www.livecdlist.com
>> All open source, free and comes with source code.

>
> You fucking spamtard!

But you are not of course?

7, Feb 26, 2006