CrazyEngineers
  • imaginary number in c++

    maria flor

    Member

    Updated: Oct 23, 2024
    Views: 1.6K
    could someone help me on what to input on C++ so that i can get the imaginary roots of a quadratic equation....
    I really don't know what to do...😭
    help me please..😕😕😭
    thank you!!
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • vinci

    MemberFeb 25, 2012

    [​IMG]
    try this equation. let me know if it doesnt work still
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberFeb 25, 2012

    Use a variable to indicate whether the delta (B[sup]2[/sup]-4AC) is positive or negative.

    Instead of
    pro = sqrt((b*b)-(4*a*c));

    here, use
    pro = sqrt(abs(B*B)-(4*A*C))

    And then, if delta is positive, use it as is. If delta is negative, just print an 'i' or 'j' along with the result, to indicate that the root is imaginary...
    Are you sure? This action cannot be undone.
    Cancel
  • maria flor

    MemberFeb 25, 2012

    vinci
    [​IMG]
    try this equation. let me know if it doesnt work still
    it doesn't work
    Are you sure? This action cannot be undone.
    Cancel
  • maria flor

    MemberFeb 25, 2012

    silverscorpion
    Use a variable to indicate whether the delta (B[sup]2[/sup]-4AC) is positive or negative.

    Instead of
    pro = sqrt((b*b)-(4*a*c));

    here, use
    pro = sqrt(abs(B*B)-(4*A*C))

    And then, if delta is positive, use it as is. If delta is negative, just print an 'i' or 'j' along with the result, to indicate that the root is imaginary...
    i just tried it but when i try to input an equation with imaginary roots.. it doesn't give me an answer...
    Are you sure? This action cannot be undone.
    Cancel
  • maria flor

    MemberFeb 25, 2012

    silverscorpion
    Use a variable to indicate whether the delta (B[sup]2[/sup]-4AC) is positive or negative.

    Instead of
    pro = sqrt((b*b)-(4*a*c));

    here, use
    pro = sqrt(abs(B*B)-(4*A*C))

    And then, if delta is positive, use it as is. If delta is negative, just print an 'i' or 'j' along with the result, to indicate that the root is imaginary...
    thanks for that but when i try to input an equation with imaginary roots.. it still doesn't give an answer...
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberFeb 25, 2012

    After you have calculated delta, (variable "pro" in your program), calculate the individual roots in the following way..

    pro = sqrt(abs(B*B-4*A*C))
     
    if ((B*B-4*A*C) >= 0) {
        x1 = (-B+pro)/2*A
        x2 = (-B-pro)/2*A
        cout << "The first root is " << x1 << endl
        cout << "The second root is " << x2 << endl
    } else {
        real = -B/2*A
        imag = pro/2*A
        cout << "The first root is "<<real<<"+i"<<imag<<endl
        cout << "The first root is "<<real<<"-i"<<imag<<endl
    }
    Try using this..
    Are you sure? This action cannot be undone.
    Cancel
  • vinci

    MemberFeb 25, 2012

    you need value of of a,b,c ?i found that equation i posted here gives complex roots .
    Are you sure? This action cannot be undone.
    Cancel
  • maria flor

    MemberFeb 25, 2012

    silverscorpion
    After you have calculated delta, (variable "pro" in your program), calculate the individual roots in the following way..

    pro = sqrt(abs(B*B-4*A*C))
     
    if ((B*B-4*A*C) >= 0) {
        x1 = (-B+pro)/2*A
        x2 = (-B-pro)/2*A
        cout << "The first root is " << x1 << endl
        cout << "The second root is " << x2 << endl
    } else {
        real = -B/2*A
        imag = pro/2*A
        cout << "The first root is "<<real<<"+i"<<imag<<endl
        cout << "The first root is "<<real<<"-i"<<imag<<endl
    }
    Try using this..
    it still don't work... when i try to run it and input values with imaginary roots it suddenly return...
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberFeb 25, 2012

    What is the input you are giving and what is the output?

    Give the full dump..
    Are you sure? This action cannot be undone.
    Cancel
  • maria flor

    MemberFeb 26, 2012

    here: when i input those numbers...it returns without giving any roots...
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register