Quadratic equation solver

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeyke
    New Member
    • Feb 2010
    • 15

    Quadratic equation solver

    Ask the user to enter the coefficients a, b and c. Have your program then solve the two (or possibly one) solution(s) for X. Be careful to make sure there is a solution by confirming that the square root is not executed on a negative number which is considered to be imaginary.

    Be sure to #include<cmath> and call the pow() and sqrt() functions when making your math calculations. Also note, the coefficients a b and c are to be declared as a double, as well as your two solutions for X.
    this is what i did so far:
    Code:
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {                       
    int a;
    int b;
    int c;
    int x;
    double posx, negx; //declare positive x and negative y
    
    cout<<"Enter the value of a: ";
    cin>>a;
    cout<<"Enter the value of b: ";
    cin>>b;
    cout<<"Enter the value of c: ";
    cin>>c;
    
    int check;
    check=(b*b)-(4*a*c);      
    if (check>0)
    {
       posx=(-b+sqrt(check))/2*a;
       negx=(-b-sqrt(check))/2*a;
    
    system ("pause");
    return 0;
    
    }
    
    }
    Last edited by Banfa; Feb 26 '10, 09:43 AM. Reason: Added code tags
  • grayMist
    New Member
    • Feb 2010
    • 12

    #2
    I am assuming your program attempts to solve a quadratic equation.
    please mention the issue you are facing in more details. From the problem description i could not make out any issues at all !!

    Here are a few pointers:
    1) You have not declared a,b,c as double.
    2) Declare 'check' as double as well or sqrt() will complain of an ambiguous call.

    Do this and then let us know exactly where you are getting stuck.

    And , most important , don't forget to put your code snippets inside the code-tags.

    Comment

    • joeyke
      New Member
      • Feb 2010
      • 15

      #3
      in my output it doesn't go beyond entering a value

      i don't know how the output should look like because from what i get it shows this:
      Last login: Thu Feb 25 21:57:40 on ttys000
      /Volumes/JOANNEKEO/C++1.5/HWkeoAssignment 2-3A
      joanne-keos-macbook:~ joanne$ /Volumes/JOANNEKEO/C++1.5/HWkeoAssignment 2-3A
      Enter the value of a: 5
      Enter the value of b: 6
      Enter the value of c: 3
      joanne-keos-macbook:~ joanne$

      the picture is the quadratic equation
      Attached Files

      Comment

      • grayMist
        New Member
        • Feb 2010
        • 12

        #4
        I think you forgot to display the values of posx and negx .

        that i think is quite important. ;)

        Comment

        • joeyke
          New Member
          • Feb 2010
          • 15

          #5
          yes

          oh yeah ! how do i go about that in my program? thank for you help btw!

          Comment

          • grayMist
            New Member
            • Feb 2010
            • 12

            #6
            try cout<<" posx ="<<posx<<" negx = "<<negx;
            that should do a neat job of printing the values.

            Comment

            • joeyke
              New Member
              • Feb 2010
              • 15

              #7
              where do i put that? after this:
              posx=(-b+sqrt(check))/2*a;
              negx=(-b-sqrt(check))/2*a;

              Comment

              • grayMist
                New Member
                • Feb 2010
                • 12

                #8
                When do you think the output should be displayed?
                before the calculation or after it ? :)

                Comment

                • joeyke
                  New Member
                  • Feb 2010
                  • 15

                  #9
                  after i think. is it like this:

                  if (check>0)
                  {
                  cout<<" posx ="<<posx<<" negx = "<<negx;

                  cin>>posx, negx;

                  posx=(-b+sqrt(check))/2*a;

                  negx=(-b-sqrt(check))/2*a;


                  system ("pause");

                  return 0;

                  }

                  }

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Have you tried to compile that?
                    If it compiled what result did you get printed on the screen?
                    Was that the result you expected?

                    grayMist question "When do you think the output should be displayed? Before the calculation or after it ?" is still relevant but perhaps you should also answer the question

                    Which piece of my code actually does the calculation.

                    Comment

                    • joeyke
                      New Member
                      • Feb 2010
                      • 15

                      #11
                      i still get this in my output:
                      Enter the value of a: 1
                      Enter the value of b: 2
                      Enter the value of c: 3

                      should there a calculation done in my output? if so then why isn't it printing that?

                      Comment

                      • joeyke
                        New Member
                        • Feb 2010
                        • 15

                        #12
                        wait i get it!
                        Enter the value of a: 1
                        Enter the value of b: 4
                        Enter the value of c: 6
                        posx =-3.81582e-232 negx = 0

                        is this right?

                        Comment

                        • joeyke
                          New Member
                          • Feb 2010
                          • 15

                          #13
                          that's what prints now after i did
                          double check;
                          check=(b*b)-(4*a*c);
                          cout<<" posx ="<<posx<<" negx = "<<negx;
                          cin>>posx, negx;

                          Comment

                          • johny10151981
                            Top Contributor
                            • Jan 2010
                            • 1059

                            #14
                            you have a huge mistake in the below coldes

                            posx=(-b+sqrt(check))/2*a;

                            negx=(-b-sqrt(check))/2*a;



                            please rethink. you must get a result.

                            Regards,
                            Johny

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              Originally posted by joeyke
                              Enter the value of a: 1
                              Enter the value of b: 4
                              Enter the value of c: 6
                              posx =-3.81582e-232 negx = 0

                              is this right?
                              You should know if it is right the first time you run the program. You do this by using the co-efficients of a function that you know the roots of. For a quadratic (or any polynomial) this is relatively easy because many quadratics can be written in the form

                              (m.x + n).(o.x + p)

                              Set m and o to 1 to make things easy, set n to a small negative integer and p to a small postive integer and you get

                              a = 1
                              b = p + n
                              c = p * n

                              With roots -p and -n

                              So you have a known equation with known roots and you can plug those values into your program and you will know it it has output the right value.

                              Comment

                              Working...