Quadratic Solver HELP

Discussion in 'Programming & Scripting' started by Zer0-One, Feb 10, 2009 with 19 replies and 400 views.

  1. Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    I'm making a console app that solves quadratics. so far, it can correctly identify the number of roots, but when it actually tries to calculate them,
    i get the wrong numbers. WHAT AM I DOING WRONG? this is
    supposed to be SIMPLE.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    class Program
    {
    public static double QuadraticEquationPositive(double a, double b, double c)
    {
    double Solution1 = (-b + Math.Sqrt((b * b) - (4 * a * c))) / 2 * a;
    return Solution1;
    }
    
    public static double QuadraticEquationNegative(double a, double b, double c)
    {
    double Solution2 = (-b - Math.Sqrt((b * b) - (4 * a * c))) / 2 * a;
    return Solution2;
    }
    
    static void Main(string[] args)
    {
    Console.WriteLine("Ax^2 + Bx + C");
    Console.WriteLine();
    Console.WriteLine("Please enter value of quadratic coefficient (Must be non-zero): ");
    string A = Console.ReadLine();
    double AValue = Convert.ToDouble(A);
    Console.WriteLine("Please enter value of linear coefficient: ");
    string B = Console.ReadLine();
    double BValue = Convert.ToDouble(B);
    Console.WriteLine("Please enter value of constant: ");
    string C = Console.ReadLine();
    double CValue = Convert.ToDouble(C);
    double D = (BValue * BValue) - (4 * AValue * CValue);
    if (D < 0) 
    {
    Console.WriteLine("There are no solutions for this equation.");
    }
    if (D == 0)
    {
    Console.WriteLine("This equation has one solution: " + QuadraticEquationPositive(AValue, BValue, CValue));
    }
    if (D > 0)
    {
    Console.WriteLine("This equation has 2 solutions: " + QuadraticEquationPositive(AValue, BValue, CValue) + " and " + QuadraticEquationNegative(AValue, BValue, CValue));
    }
    Console.ReadLine();
    }
    }
    }
    
     
  2. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    o god, i HATED quadratics :@ Give me a problem to use, and it's solutions. I'll try to debug tomo
     
  3. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    2x^2 + 8x + 2

    that should give you -0.2679491924311228 and -3.732050807568877

    but my program gives like, -.something and -14.something.
     
  4. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    your order of operations is off. I just followed it, and I see where you got it wrong.

    Code:
    public static double QuadraticEquationPositive(double a, double b, double c)
    {
    double Solution1 = (-b + Math.Sqrt((b * b) - (4 * a * c))) [B]/ 2 * a;[/B]
    return Solution1;
    }
    
    public static double QuadraticEquationNegative(double a, double b, double c)
    {
    double Solution2 = (-b - Math.Sqrt((b * b) - (4 * a * c))) [B]/ 2 * a;[/B]
    return Solution2;
    }
    

    look at the bold, what it's going to do is divide the whole thing by 2, and then multiply THAT by a.

    corrected functions:
    Code:
    public static double QuadraticEquationPositive(double a, double b, double c)
    {
    double Solution1 = (-b + Math.Sqrt((b * b) - (4 * a * c))) /( 2 * a);
    return Solution1;
    }
    
    public static double QuadraticEquationNegative(double a, double b, double c)
    {
    double Solution2 = (-b - Math.Sqrt((b * b) - (4 * a * c))) /( 2 * a);
    return Solution2;
    }
    
     
    • Like Like x 1
  5. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    Woot! leave it to Hax to save tha day :tongue:

    btw can i get this when it's completed?
     
  6. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    ALL RIGHT! THANKS! I never would have caught that tiny mistake on my own. It works good now.

    and Jac0b, as soon as i'm finished, you can get the first copy.
    why do you want it anyway?
     
  7. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    It looks like it would make factoring quadratics quite easy. I'm in math 252 atm. just thought it'd be neat :tongue:
     
  8. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    mm... this thing doesn't put out nice neat factors. it gives decimals.
     
  9. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    thats fine. they're answers.
     
  10. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    ok. now, is it ok as a console app, or would you like a windows form?

    windows form would probly be more comfortable. you could clear and start over without exiting, and stuff like that.
     
    • Like Like x 1
  11. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    windows app for sure. Idk if this is possible for you but what about it working on a mac? i run both OS's so jw
     
  12. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    i wouldn't know anything about getting it to run on a mac. I only use macs for design. I'm strictly Windows when it comes to programming. but windows form it is then. i'll have it ready in a few days, because i'm probably going to be super-busy this week.
     
    • Like Like x 1
  13. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    Alright, dude now I'm excited. Thank you! :thumbup: This'll be cool
     
  14. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    erm, you CAN keep it in fractions if you want. Just check to see if sqrt gives decimals, and if so, just output something such as "sqrt 10" etc...
     
  15. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
  16. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    Fractions> Decimals......
     
  17. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    so, besides calculating and clearing, do you want it to have any other special functions?
     
  18. Jac0b

    Jac0b More Dubstep

    Messages:
    1,197
    Ratings:
    168
    what kind of things you have in mind?
     
  19. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
  20. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    With quadratics, you want precision. In fact, decimal answers are counted as improper.