Closed Thread
Results 1 to 5 of 5

Thread: Finding real roots of a quadratic eq. in c++

  1. #1
    rioters block is offline Newbie
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Finding real roots of a quadratic eq. in c++

    So, I'm 2 weeks into my first computer science course and we have an assignemt to write a program which accepts 3 real coefficients a, b, c of the quadratic equation: ax^2 + bx + c = 0 and outputs the real roots of the equation without failing using the quadratic formula:
    x = (-b + or - sqrt(b^2 -4ac))/2a

    So, I've come up with 5 cases where I need to give the input special attention:

    case 1: a=0 and b=0 Here, ax^2 + bx + c = 0 can't be solved for x

    case 2: a =0 and b !=0 Here, ax^2 + bx + c = 0 becomes a linear equation bx + c = 0 which can be solved by letting x = -c/b

    case 3: a !=0 and b*b < 4ac Here, x would have complex roots which we're not messing with today!

    case 4:if b is too big, b*b-4ac might equal b*b cuz if a real number is only allowed to have so many digits in c++ when you
    subtract a very small number from it the only diffrence from b*b might be in the last digit and the computer will have cut it off. Our professor said that to avoid this, we should choose b such that b*b<=40ac.

    case 5: a!=0 and b*b -4ac >= 0 and b*b<=40ac Here, everything is good to go and we can apply the quadratic formula to get 2 real roots.

    So, to deal with these cases, I try to use just if statements. Everything complies, the program even outputs the right answers if you give it the correct input. The problem is that sometimes, when you give input that produces a case 1, 3 or 4 situation, it'll ask you to give it new input but then won't know what to do with it and the program just ends. Is it because I'm using just if statements and not if...else statements? Does the sequence of the statements matter if you're only using if statements? Sorry if this is really too much to be asking but I'm very stuck here! I thank everybody so much for trying to help me on this.

    Here's the code:
    #include<iostream>
    #include<iomanip>
    #include<cmath>
    using namespace std;

    int main()
    {
    double a, b, c, x1, x2;
    cout<<"Enter 3 real numbers from the keyboard."<<endl;
    cin>>a>>b>>c;
    if(a==0 && b==0)
    {
    cout<<"You have entered coefficients which cannot be solved for x."<<endl;
    cout<<"So enter two new real numbers for a and b."<<endl;
    cin>>a>>b;
    }
    if(a==0 && b!=0)
    {
    x1 = -c/b;
    x2 = x1;
    cout<<"x = "<<x1<<endl;
    }
    if((b*b-4*a*c)<0)
    {
    cout<<"The coefficients you entered would yield complex roots."<<endl;
    cout<<"Try again by entering new values of a, b, and c."<<endl;
    cin>>a>>b>>c;
    }
    if(b*b > abs(40*a*c))
    {
    cout<<"The value you entered for b is too large for the computer to"<<endl;
    cout<<"produce an accurate answer. So enter a new value for b."<<endl;
    cin>>b;
    }
    if(a!=0 && (b*b-4*a*c)>=0 && b*b<= abs(40*a*c))
    {
    x1 = (-b + sqrt(b*b - 4*a*c))/2*a;
    x2 = (-b - sqrt(b*b - 4*a*c))/2*a;
    cout<<"x = "<<x1<<endl<<"and"<<endl<<"x = "<<x2<<endl;
    }
    return 0;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Finding real roots of a quadratic eq. in c++

    I would strongly recommend that you put the input within a do while loop. Otherwise, you can provide enough bad input to never get caught.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    rioters block is offline Newbie
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Re: Finding real roots of a quadratic eq. in c++

    Quote Originally Posted by WingedPanther View Post
    I would strongly recommend that you put the input within a do while loop. Otherwise, you can provide enough bad input to never get caught.
    I don't know what a "do while loop" is. I've only been at programming for around a few weeks. Can this program be written using just 'if' and/or 'if..else' statements and perhaps user-defined functions? If so, how do I re-enter the input if my original choices for a, b, and c won't allow the equation to be solved for x for whatever reason?

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Finding real roots of a quadratic eq. in c++

    Your program executes sequentially. Once it reaches the first case the user is prompted for more input, and continues to the end of the program. There is nothing causing the first case to be reevaluated since, at this point, it was already evaluated. I suppose you could do it recursively (with user defined functions), but a loop would be the easiest. You cannot do this with just if/else statements.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Finding real roots of a quadratic eq. in c++

    If you don't know how to use loops, the other option is to perform the evaluation appropriate for the provided input (if possible). For example, if a=0 and b!=0 then x=-c/b is an easy calculation.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. real escape for pdo
    By lol33d in forum PHP Development
    Replies: 1
    Last Post: 08-27-2011, 06:36 AM
  2. Square Roots
    By Olympiaus in forum C and C++
    Replies: 6
    Last Post: 04-30-2009, 07:45 AM
  3. Needing a real help :(
    By Moody_87 in forum C and C++
    Replies: 9
    Last Post: 03-17-2009, 06:12 PM
  4. Access App to real App
    By makenoiz in forum General Programming
    Replies: 2
    Last Post: 01-16-2009, 08:03 PM
  5. Quadratic Expressions
    By Brandon W in forum Tutorials
    Replies: 30
    Last Post: 10-23-2008, 02:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts