A problem in the middle of ... !

This is a discussion on A problem in the middle of ... ! within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I was solving a problem about analysing solutions of quadratic equation, and of corse calculating the solutions. But ...

  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
    32,852
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21