Thread: Quadratic Equation

  1. #16
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    so the exceptions i came up with is.
    Exceptions:
    1.if a=0… there are infinite solutions
    2.if b^2-(4ac)<0… YOu get an imaginary number
    Is that the only exceptions i need to modify my program for? That i wont get regular integers. Its just the math part of it im confused on.

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If a is zero then you are just doing a regular linear function, correct? And read my last post. Use the complex data type that your compiler may supply you.

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Besides, you are also forgetting that if "a" is zero, then invariably you end up dividing by zero. Which isn't technically a limitation of the quadratic formula since it is designed to only deal with quadtratics and not linear functions.

  4. #19
    Registered User
    Join Date
    Sep 2008
    Posts
    101

    YAY

    Code:
    #include <FPT.h>
    
    
    int main()
    {
      double a,b,c,d,x1,x2,rp,ip ;
    
      a = inD() ;
      b = inD() ;
      c = inD() ;
    
    
      if (a == 0.0) {
    
        // linear equation?
        if ((b == 0.0) && (c == 0.0)) {
            outS("Any number is a solution.\n") ;
        } else if (b == 0.0) {
            outS("There are no solutions.\n") ;
        } else {
            outS("There is a single real solution, ") ;
            x1 = -c/b ;
            outD(x1) ;
            outS("\n") ;
        }
    
    
      } else 
      {
    
        d = b*b - 4*a*c ;
    
        if (d == 0) {
           x1 = -b/(2*a) ;
           outS("There are two identical real solutions, ") ;
           outD(x1) ;
           outS(" and ") ;
           outD(x1) ;
           outS("\n") ;
        } else if (d > 0) {
           d = sqrt(d) ;
           x1 = (-b + d)/(2*a) ;
           x2 = (-b - d)/(2*a) ;
           outS("There are two real solutions, ") ;
           outD(x1) ;
           outS(" and ") ;
           outD(x2) ;
           outS("\n") ;
         } else {
           d = sqrt(-d) ;
           rp = -b/(2*a) ;
           ip =  d/(2*a) ;
           outS("There are two complex answers, ") ;
    
           outD(rp) ;
           if (ip >= 0) {
    	 outS(" + ") ;
             outD(ip) ;
             outS("i") ;
           } else {
    	 outS(" - ") ;
             outD(-ip) ;
             outS("i") ;
           }
    
           outS(" and ") ;
    
           ip = -ip ;
           outD(rp) ;
           if (ip >= 0) {
    	 outS(" + ") ;
             outD(ip) ;
             outS("i") ;
           } else {
    	 outS(" - ") ;
             outD(-ip) ;
             outS("i") ;
           }
    
           outS("\n") ;
         }
    
      }
    
    
    
    }
    I believe that i got a working program! Someone shoot me some numbers so that i know it solves all numbers!

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I believe your code looks good. What happens if you pass in NaN or positive (or negative) infinity? Your program should at least test for those values and throw them out if they are in use. It should actually just treat all of those as NaN and spit out an IO error.

  6. #21
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    sorry im confused as to what you're stating. Please Explain (its been a long day of classes)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  2. quadratic equation cpp program problems
    By faluiretoread in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2008, 03:22 PM
  3. Replies: 8
    Last Post: 03-10-2008, 07:15 PM
  4. Quadratic Equation problems
    By Rachel228 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 02:42 AM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM