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

1. ### Zer0-OneExperienced 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();
double AValue = Convert.ToDouble(A);
Console.WriteLine("Please enter value of linear coefficient: ");
double BValue = Convert.ToDouble(B);
Console.WriteLine("Please enter value of constant: ");
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));
}
}
}
}
```

Replies:
7
Views:
230
3. ### Haxalot88VIPVIP

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

4. ### Zer0-OneExperienced 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.

5. ### Haxalot88VIPVIP

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 x 1
6. ### Jac0bMore Dubstep

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

btw can i get this when it's completed?

7. ### Zer0-OneExperienced 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?

8. ### Jac0bMore 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

9. ### Zer0-OneExperienced Member

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

Messages:
1,197
Ratings:
168

11. ### Zer0-OneExperienced 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 x 1
12. ### Jac0bMore 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

13. ### Zer0-OneExperienced 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 x 1
14. ### Jac0bMore Dubstep

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

15. ### Haxalot88VIPVIP

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...

16. ### Zer0-OneExperienced Member

Messages:
2,233
Ratings:
245
fractions < decimals

17. ### Jac0bMore Dubstep

Messages:
1,197
Ratings:
168
Fractions> Decimals......

18. ### Zer0-OneExperienced Member

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

19. ### Jac0bMore Dubstep

Messages:
1,197
Ratings:
168
what kind of things you have in mind?

20. ### Zer0-OneExperienced Member

Messages:
2,233
Ratings:
245
nvm. i PMed it to you.

21. ### Haxalot88VIPVIP

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