-
April 28th, 2011, 05:58 AM
#1
Linear Diophantine Equation Solver (Loops Help)
Hi Guys,
I am new to using Visual Basic. (I am using VB 6)
I am wanting to create a simple program that solves equations in the form
A(x) + B(y) = C
Where A, B and C have already been inputted into text boxes.
Ideally the program would start with a y value of -500, and then try x = -500, -499, - 498 etc etc until it reached 500.
Then y would increase by 1 and try all the x values again, and so on. If anything matches the value of C, it would all be added to a List Box.
Here is the code I have put together (forgive me if the syntax is horrendous!)
**I have three input text boxes, a list box and a command button.**
Private Sub Command1_Click()
Dim a As Variant, b As Variant, c As Variant, x As Variant, y As Variant
a = Text1
b = Text2
c = Text3
For y = -500 To 500
For x = -500 To 500
If (a * x) + (b * y) = c Then
List1.AddItem Trim$(a) + Trim$(x) + "+" + Trim$(b) + Trim$(y) + "=" + Trim(c)
x = x + 1
Loop
y = y + 1
End If
Loop
End Sub
I am getting a lot of 'loop without do' errors. However If I place a 'do' at the top of each loop it seems to make no difference.
Is it an issue that I have a loop system within another loop? (i'm not really sure how else I would do this problem)
Any help would be much appreciated.
Cheers Guys,
Phil
-
April 28th, 2011, 06:57 AM
#2
Re: Linear Diophantine Equation Solver (Loops Help)
I have changed my code.
I don't get any error messages. However noting gets printed in the list box (I am using values I know there are solutions for)
It also crashes Visual Basic when it runs.
Updated code:
Private Sub Command1_Click()
Dim a As Variant, b As Variant, c As Variant, x As Variant, y As Variant
a = Text1
b = Text2
c = Text3
y = -20
Do While y < 20
x = -20
Do While x < 20
If (a * x) + (b * y) = c Then
List1.AddItem Trim$(a) + Trim$(x) + "+" + Trim$(b) + Trim$(y) + "=" + Trim(c)
x = x + 1
Exit Do
End If
Loop
y = y + 1
Exit Do
Loop
End Sub
-
April 28th, 2011, 10:08 AM
#3
Re: Linear Diophantine Equation Solver (Loops Help)
Welcome to the forums!
Please use CODE TAGS in the future...
As far as your code goes, you need to learn about TYPES. There are three main types. STRING, NUMERIC, and VARIANT. VB6 is all about using the right type. You cannot add two STRINGs without converting them to NUMBERIC.
If you are using math, you should have variables for the calculated values, and also string values for the output.
Also, to CONCATENATE two strings you use "&" rather than "+"
Code:
Print "23" & "2" ' 232
while
-
April 28th, 2011, 02:09 PM
#4
Re: Linear Diophantine Equation Solver (Loops Help)
Apologies for posing my code wrong The problem resolved now, my code is working! thanks for the advice.
Phil
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|