7 Replies - 2710 Views - Last Post: 02 December 2008 - 04:20 PM Rate Topic: -----

#1 SweetMusicsSolstace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 08

Quadratic Equation factoring

Posted 22 November 2008 - 03:43 PM

There is a Bug in here some where but I can't find it

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



Is This A Good Question/Topic? 0
  • +

Replies To: Quadratic Equation factoring

#2 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

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
Was This Post Helpful? 0
  • +
  • -

#3 SweetMusicsSolstace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 08

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
OutputBoxA.Text = -B + (Sqr(BSqrd) - (FourAC)) / (TwoA) 
OutputBoxB.Text = -B - (Sqr(BSqrd) - (FourAC)) / (TwoA)


Was This Post Helpful? 0
  • +
  • -

#4 VB Crash  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 130
  • Joined: 28-November 08

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

'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

Was This Post Helpful? 0
  • +
  • -

#5 DempZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-December 08

Re: Quadratic Equation factoring

Posted 02 December 2008 - 12:23 PM

View PostVB Crash, on 1 Dec, 2008 - 05:55 PM, said:

I did not understand the math but any way I think this is the problem

'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
Was This Post Helpful? 0
  • +
  • -

#6 VB Crash  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 130
  • Joined: 28-November 08

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
Was This Post Helpful? 0
  • +
  • -

#7 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

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
Was This Post Helpful? 0
  • +
  • -

#8 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1