Velocity Reviews > Quadratic Solutions in C++

Sunny
Guest
Posts: n/a

 02-25-2006
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.

Mike Easter
Guest
Posts: n/a

 02-25-2006
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

7
Guest
Posts: n/a

 02-25-2006
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.

old jon
Guest
Posts: n/a

 02-25-2006

"Sunny" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> 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:
http://www.overclock.net/application...s-c-style.html

--
bw..OJ

Rich Wilson
Guest
Posts: n/a

 02-25-2006

"Sunny" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> 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.

why?
Guest
Posts: n/a

 02-25-2006

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

Sunny
Guest
Posts: n/a

 02-25-2006
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;
}

Rich Wilson
Guest
Posts: n/a

 02-25-2006

"Sunny" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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) ?

why?
Guest
Posts: n/a

 02-25-2006

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

Clogwog
Guest
Posts: n/a

 02-25-2006
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 ****ing spamtard!

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Vinodh Kumar C++ 6 12-01-2010 12:30 PM Totti Java 5 12-22-2007 09:17 PM amitsoni.1984@gmail.com Python 3 11-22-2006 10:45 PM Philip Smith Python 2 05-30-2006 10:29 PM fb C Programming 6 09-01-2004 08:38 AM