Option Explicit Dim A As Double Dim B As Double Dim C As Double Dim BSqrd As Double Dim FourAC As Double Dim TwoA As Double Dim OutputBoxBA As String Dim OutputBoxAB As String ' Private Sub CalculateButton_Click() 'My values A = Val(InputBox.Text) B = Val(InputBox.Text) C = Val(InputBox.Text) 'The squares BSqrd = (B * B) FourAC = (4 * (A * C)) TwoA = (A * A) 'The if/then statement x2 Containing an error trap If A < 0 Then MsgBox ("Runtime Error 8675309") Else OutputBoxA.Text = B + (Sqr(BSqrd)  (FourAC)) / (TwoA) OutputBoxB.Text = B  (Sqr(BSqrd)  (FourAC)) / (TwoA) End If If OutputBoxA.Text < 0 Or OutputBoxB.Text < 0 Then MsgBox ("Runtime Error 8675309") MsgBox ("Square Root Of A Negitive NumBer!!!") OutputBoxA.Text = "" OutputBoxB.Text = "" End If End Sub Private Sub ExitButton_Click() Unload Me End Sub Private Sub ResetButton_Click() InputBox.Text = "" OutputBoxA.Text = "" OutputBoxB.Text = "" End Sub
Quadratic Equation factoring
Page 1 of 17 Replies  2805 Views  Last Post: 02 December 2008  04:20 PM
#1
Quadratic Equation factoring
Posted 22 November 2008  03:43 PM
There is a Bug in here some where but I can't find it
Replies To: Quadratic Equation factoring
#2
Re: Quadratic Equation factoring
Posted 22 November 2008  03:56 PM
Well, if you run your app from within VB6 it will halt at the erraneous code. Where does it halt?
Where is OutputBoxA declared?
The statement OutputBoxA.Text < 0 seems a little strange. "0" is a number and you are comparing it to a string...
Why such nasty error boxes? Better to tell the user what they did wrong.
You should test if there's a negative number before you try to calculate the square root of it.
To calculate the square root I think you should use a function sqrt() not sqr, but I'm a little unsure.
/Jens
Where is OutputBoxA declared?
The statement OutputBoxA.Text < 0 seems a little strange. "0" is a number and you are comparing it to a string...
Why such nasty error boxes? Better to tell the user what they did wrong.
You should test if there's a negative number before you try to calculate the square root of it.
To calculate the square root I think you should use a function sqrt() not sqr, but I'm a little unsure.
/Jens
#3
Re: Quadratic Equation factoring
Posted 01 December 2008  04:48 PM
OutputBoxA Is Declared as OutputBoxAB in the Dim statement
Very last Dim statement
I guess my error messages are harsh
also my teacher told us not to worry about negitive numbers
Hence the "Square Root Of A Negitive Number!!" error message
My Teacher said The problem is in The math section of my code
Very last Dim statement
I guess my error messages are harsh
also my teacher told us not to worry about negitive numbers
Hence the "Square Root Of A Negitive Number!!" error message
My Teacher said The problem is in The math section of my code
OutputBoxA.Text = B + (Sqr(BSqrd)  (FourAC)) / (TwoA) OutputBoxB.Text = B  (Sqr(BSqrd)  (FourAC)) / (TwoA)
#4
Re: Quadratic Equation factoring
Posted 01 December 2008  06:55 PM
I did not understand the math but any way I think this is the problem
Vace
'My values A = Val(InputBox.Text) B = Val(InputBox.Text) C = Val(InputBox.Text) 'The squares BSqrd = (B * B ) FourAC = (4 * (A * C ) ) TwoA = (A * A) 'If is two A it should be => TwoA = (2*a)
Vace
This post has been edited by VB Crash: 01 December 2008  06:56 PM
#5
Re: Quadratic Equation factoring
Posted 02 December 2008  12:23 PM
VB Crash, on 1 Dec, 2008  05:55 PM, said:
I did not understand the math but any way I think this is the problem
Vace
'My values A = Val(InputBox.Text) B = Val(InputBox.Text) C = Val(InputBox.Text) 'The squares BSqrd = (B * B ) FourAC = (4 * (A * C ) ) TwoA = (A * A) 'If is two A it should be => TwoA = (2*a)
Vace
Spot on Vace, first thing i noticed!
a * a = a squared
a + a = 2a
Very common problem with math code, people just forget and the simple things slide into their code
DempZ
#6
Re: Quadratic Equation factoring
Posted 02 December 2008  01:09 PM
Yeah, it can be A*2 or A+A .... I did't understand the name of the varible. Anyway, it's ok now?
Vace
Vace
#7
Re: Quadratic Equation factoring
Posted 02 December 2008  02:47 PM
My guess is that you are trying to solve somthing like this:
Ax2 + Bx + C = 0 (Where x2 indicates x*x)
One way to do it:
1 ) Divide all of it with A => x2 + (B/A)x + C/A = 0
2 ) The equation x2 + px + q = 0 has (usually) two solutions, one of them is...
3 ) X = p/2 + sqrt((p/2)*(p/2)q) 'You probably recognize this one, the other solution is just "a sign" away.
4 ) Apply this on your problem...
5a) p = B/A
5b) q = C/A
6 ) underTheSquareRoot = (p/2) * (p/2)  q = (p*p/4)  q
7 ) IF underTheSquareRoot < 0 THEN msgbox("No solutions") 'More stuff here that you'll have to think about
8a) X1 = p/2 + sqrt(underTheSquareRoot)
8b) X2 = p/2....
9 ) Present your solutions in e.g. two textboxes.
10) Done
Regards
/Jens
Ax2 + Bx + C = 0 (Where x2 indicates x*x)
One way to do it:
1 ) Divide all of it with A => x2 + (B/A)x + C/A = 0
2 ) The equation x2 + px + q = 0 has (usually) two solutions, one of them is...
3 ) X = p/2 + sqrt((p/2)*(p/2)q) 'You probably recognize this one, the other solution is just "a sign" away.
4 ) Apply this on your problem...
5a) p = B/A
5b) q = C/A
6 ) underTheSquareRoot = (p/2) * (p/2)  q = (p*p/4)  q
7 ) IF underTheSquareRoot < 0 THEN msgbox("No solutions") 'More stuff here that you'll have to think about
8a) X1 = p/2 + sqrt(underTheSquareRoot)
8b) X2 = p/2....
9 ) Present your solutions in e.g. two textboxes.
10) Done
Regards
/Jens
#8
Re: Quadratic Equation factoring
Posted 02 December 2008  04:20 PM
according to you
delta = b^2  4*a*c 'where delta determine the result of the equation if delta < 0 then msgbox "roots are imaginary" elseif delta>=0 then x= b +((delta)^(0.5))/2*a y= b ((delta)^(0.5))/2*a end if
This post has been edited by thava: 02 December 2008  04:20 PM
Page 1 of 1
