Thread: Problem with Quad equation solver program

  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    Question Problem with Quad equation solver program

    hey guys..I'm new here,and I need some help in this program I'm making..

    Code:
    //Program to show the nature of roots,solve,etcetc
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <windows.h>
    #include <cmath>
    #include <conio.h>
    
    using namespace std;
    void setcolor(unsigned short color)                 
    {                                                   
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hcon,color);
    }
    
    int main()
    {
        float a,x,b,c,alpha,beta,D;
        int y;
        alpha = (-b - sqrt(pow(b,2) - 4*a*c))/2*a ;
        beta = (-b + sqrt(pow(b,2) - 4*a*c))/2*a ;
        D = pow(b,2) - 4*a*c;
        
            cout << "\nWelcome to the Equation Solver!\n";
            cout << "\nChoose one of the following -:\n";         
            cout << "\n1. Find a quadratic equation's nature of roots\n";
            cout << "\n2. Find a quadratic equation's roots\n";
            cout << "\n3. Establish the Vi&#232;te relation of the roots and coefficients\n";
            cout << "\n4. Quit,noobs >_>\n";
            cin >> y;
            
            switch(y)
            {
                     case 1:
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..\n";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          setcolor(8);
                          if (D = 0)
                                {
                                cout << "The roots of this equation are real and equal!";
                                }
                          if (D > 0)
                                {
                                cout << "The roots of this equation are real and unequal!";
                                }
                          if (D < 0)
                                {
                                cout << "There are no roots to this equation..";
                                }
                          setcolor(7);
                          system("PAUSE");
                          break;
                      case 2:
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          cout << "And the solutions are as follows -:\n";
                          setcolor(14);
                          cout << "x = " << alpha << endl;
                          cout << "x = " << beta << endl;
                          setcolor(7);
                          system("PAUSE");
                          break;
                      case 3:
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          setcolor(8);
                          cout << "The sum of the roots is " << alpha + beta << endl;
                          cout << "The value of -b/a is " << -b/a << endl;
                          cout << "The product of the roots is " << alpha * beta << endl;
                          cout << "The value of c/a is " << c/a << endl;
                          setcolor(7);
                          system("PAUSE");
                          break;
                      case 4: break;
                      default: "Enter a number in the list\n";break;
            }
          
          return 0;
    }
    Can't understand..there are no syntax errors,however there are logical ones.
    In case 1, the Discriminant outputs(The ones with D) don't come.
    In case 2, the equations always solve to some retarded value(lol?)
    Case 3 doesn't work at all...
    Last edited by SVXX; 09-29-2007 at 12:04 PM.

  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
    > alpha = (-b - sqrt(pow(b,2) - 4*a*c))/2*a
    > beta = (-b + sqrt(pow(b,2) - 4*a*c))/2*a ;
    > D = pow(b,2) - 4*a*c;
    These need to be performed AFTER you have input the values, not before.

    Also, you need to check that
    pow(b,2) - 4*a*c
    isn't negative before trying to get the square root of it.
    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
    Join Date
    Sep 2007
    Posts
    37
    I'd replace the...
    Code:
    if(D=0)
    ...with a...
    Code:
    if(D==0)
    ...if I were you. Single '=' means assigning a value, double '==' means a check statement @_@

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You can get rid of pow(). Just use b*b instead of pow(b, 2). Also, the 2*a in the denominator needs to be in parentheses, otherwise it gets interpreted as "divide by 2, then multiply by a" instead of "divide by 2*a" as you intend.

  5. #5
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Code:
    switch(y)
            {
                     case 1:
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..\n";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          D = pow(b,2) - (4*a*c);
                          setcolor(8);
                          if (D == 0)
                                {
                                cout << "The roots of this equation are real and equal!";
                                }
                          if (D > 0)
                                {
                                cout << "The roots of this equation are real and unequal!";
                                }
                          if (D < 0)
                                {
                                cout << "There are no roots to this equation..";
                                }
                          setcolor(7);
                          system("PAUSE");
                          break;
                      case 2:
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          cout << "And the solutions are as follows -:\n";
                          setcolor(14);
                          D = pow(b,2) - (4*a*c);
                          alpha = (-b - sqrt(D))/2*a ;
                          beta =  (-b + sqrt(D))/2*a ;
                          setcolor(8);
                          if (D < 0)
                          {
                                cout << "This equation has no roots!";
                          }
                          else
                          cout << "x = " << alpha << endl;
                          cout << "x = " << beta << endl;
                          setcolor(7);
                          system("PAUSE");
                          break;
                      case 3:
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          D = pow(b,2) - (4*a*c);
                          alpha = (-b - sqrt(D))/2*a ;
                          beta =  (-b + sqrt(D))/2*a ;
                          setcolor(8);
                          if (D < 0)
                          {
                                cout << "This equation has no roots!";
                          }
                          else             
                          cout << "The sum of the roots is " << alpha + beta << endl;
                          cout << "The value of -b/a is " << -b/a << endl;
                          cout << "The product of the roots is " << alpha * beta << endl;
                          cout << "The value of c/a is " << c/a << endl;
                          setcolor(7);
                          system("PAUSE");
                          break;
                      case 4: break;
                      default: "Enter a number in the list\n";break;
            }
    This is it now. The roots come in decimals..is there a way to show them in rational(fraction) or whole numbers instead? And thanks for the previous help..

  6. #6
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Ah thanks..the parantheses thingy worked..the roots are coming correct now!

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A few things:

    Code:
    You have a lot of duplicate code there. You should separate it into functions.
    
    Else requires a {} block after it. Other wise only the next line is considered part of the else condition.
    
    Use descriptive variable names. y and D are not descriptive names. Neither are alpha and beta because they do not a person reading the code what the variable actually does. a, b, and c, are iffy as well.
    This is it now. The roots come in decimals..is there a way to show them in rational(fraction) or whole numbers instead? And thanks for the previous help..
    You can round the fraction to a whole number. But there is no easy way to print out a fraction. You would have to write your own fraction class to do it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Hmm thanks a lot...a fraction class omg..@_@ might take a while..

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Most square roots cannot be represented by a fraction. To estimate them using fractions would be misleading, since the presence of whole numbers would seem to indicate perfect precision.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Thanks to ultrabot90,I was reminded of using classes...so I remade the crap, and added two more sections. Here it is...
    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <windows.h>
    #include <cmath>
    #include <conio.h>
    
    using namespace std;
    void setcolor(unsigned short color)                 
    {                                                   
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hcon,color);
    }
    
    class Program
    {
           private:
                   float a,x,b,c,D,d;
                   int n;
                   
           public:
                   void Case1();
                   void Case2();
                   void Case3();
                   void Case4();
                   void Case5();
    };
                   
    
    void Program::Case1()
    {
                          
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..\n";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          D = pow(b,2) - (4*a*c);
                          setcolor(8);
                          if (D == 0)
                                {
                                cout << "The roots of this equation are real and equal!\n";
                                }
                          if (D > 0)
                                {
                                cout << "The roots of this equation are real and unequal!\n";
                                }
                          if (D < 0)
                                {
                                cout << "There are no roots to this equation..\n";
                                }
                          setcolor(7);
                          Sleep(3000);
    }
    
    void Program::Case2()
    {
                          
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..\n";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          cout << "And the solutions are as follows -:\n";
                          setcolor(14);
                          D = pow(b,2) - (4*a*c);
                          float alpha = (-b - sqrt(D))/(2*a) ;
                          float beta =  (-b + sqrt(D))/(2*a) ;
                          setcolor(8);
                          if (D < 0)
                          {
                                cout << "This equation has no roots!\n";
                          }
                          else
                          {   cout << "x = " << alpha << endl;
                               cout << "x = " << beta << endl;
                          }
                          setcolor(7);
                          Sleep(3000);
    }
          
    void Program::Case3()
    {
                          cout << "Enter coefficient 'a' - ";
                          cin  >> a;
                          cout << "\n";
                          cout << "Enter coefficient 'b' - ";
                          cin  >> b;
                          cout << "\n";
                          cout << "Enter constant 'c' - ";
                          cin  >> c;
                          cout << "\n";
                          cout << "The equation is..\n";
                          cout << a << "x^2 +" << b << "x +" << c << endl; 
                          cout << "\n";
                          D = pow(b,2) - (4*a*c);
                          float alpha = (-b - sqrt(D))/(2*a) ;
                          float beta =  (-b + sqrt(D))/(2*a) ;
                          setcolor(8);
                          if (D < 0)
                          {
                                cout << "This equation has no roots!\n";
                          }
                          else             
                          { cout << "The sum of the roots is " << alpha + beta << endl;
                            cout << "The value of -b/a is " << -b/a << endl;
                            cout << "The product of the roots is " << alpha * beta << endl;
                            cout << "The value of c/a is " << c/a << endl;
                          }
                          setcolor(7);
                          Sleep(3000);
    }
    
    void Program::Case4()
    {
                          cout << "Enter the first term - ";
                          cin >> a;
                          cout << "\nEnter the common difference - ";
                          cin >> d;
                          cout << "\nEnter the term number(must be an integer,not a decimal) - ";
                          cin >> n;
                          float t = a + (n-1) * d;
                          setcolor(14);
                          cout << "\nThe term t" << n << " is -> " << t << endl;
                          setcolor(7);
                          Sleep(3000);
    }
    
    void Program::Case5()
    {
                          cout << "Enter the first term - ";
                         cin >> a;
                         cout << "\nEnter the common difference - ";
                         cin >> d;
                         cout << "\nEnter the term number(must be an integer,not a decimal) - ";
                         cin >> n;
                         float t = a + (n-1) * d;
                         float S = (n/2) * (a + t);
                         setcolor(13);
                         cout << "\nThe sum of the terms of this AP is " << S << endl;
                         setcolor(7);
                         Sleep(3000);
    }
         
    int main()
    {
         Program OMGDOIT;   
         int y;
         do{
        
    
        
            cout << "\nWelcome to the Equation Solver!\n";
            cout << "\nChoose one of the following -:\n";         
            cout << "\n1. Find a quadratic equation's nature of roots\n";
            cout << "\n2. Find a quadratic equation's roots\n";
            cout << "\n3. Establish the Vi&#232;te relation of the roots and coefficients\n";
            cout << "\n4. Find a term of an arithmetic progression(AP)\n";
            cout << "\n5. Find the sum of the terms of an AP\n";
            cout << "\n6. Quit,noobs >_>\n";
            cin >> y;
            
            switch(y)
            {         
                      case 1:
                          OMGDOIT.Case1();
                          system("PAUSE");
                          break;
                      case 2:
                          OMGDOIT.Case2();
                          system("PAUSE");
                          break;
                      case 3:
                          OMGDOIT.Case3();
                          system("PAUSE");
                          break;
                      case 4: 
                          OMGDOIT.Case4();
                          system("PAUSE");
                          break;
                      case 5:
                          OMGDOIT.Case5(); 
                          system("PAUSE");
                          break;  
                      case 6:
                          break; 
                      default: 
                          cout << "Enter a number in the list\n";
                          break;
            }
            }while (y != 6); 
          return 0;
    }
    Last edited by SVXX; 09-30-2007 at 01:48 AM.

  11. #11
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Quote Originally Posted by CodeMonkey View Post
    Most square roots cannot be represented by a fraction. To estimate them using fractions would be misleading, since the presence of whole numbers would seem to indicate perfect precision.
    I see...so we can let the whole number thingy be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  3. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM