Thread: A problem in the middle of ... !

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    A problem in the middle of ... !

    Hi everyone, I was solving a problem about analysing solutions of quadratic equation, and of corse calculating the solutions. But when I enter the coefficients of equation 2,4,2 ,actually always when first one is bigger than one the results are incorrect. Here is the code:

    Code:
     #include<iostream>
    #include<cmath>
    using namespace std;
    int main(){
        double a,b,c;
        cout<<"Enter a :  ";
        cin>>a;
        cin.ignore(1000,'\n');
        cout<<"Enter b: ";
        cin>>b;
        cin.ignore(1000,'\n');
        cout<<"Enter c : ";
        cin>>c;
        double d=b*b-4*a*c;
                if(d>=0){
                double x1=((-b+sqrt(d))/2*a);
                double x2=((-b-sqrt(d))/2*a);
                cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
            }
            else{
                d=-d;
                double re=-b/2*a;
                double im=sqrt(d)/2*a;
                cout<<"x1="<<"("<<re<<","<<im<<")"<<" "<<"x2="<<"("<<re<<","<<-im<<")"<<endl;
            }
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you copy/paste your console session, showing what you type in and what gets printed?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    (-b-sqrt(d))/(2*a)

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Thank you very, very much for your answer ! A small detail, that i didn't notice at all !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Having problem deleting node in middle of stack
    By sballew in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 11:00 AM