Linear Diophantine Equation Solver (Loops Help)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Linear Diophantine Equation Solver (Loops Help)

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    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

  2. #2
    Join Date
    Apr 2011
    Posts
    3

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,981

    Re: Linear Diophantine Equation Solver (Loops Help)

    Welcome to the forums!

    Please use CODE TAGS in the future...
    Code:
    ' Like This
    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
    Code:
    Print 23 + 2 ' 25
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Apr 2011
    Posts
    3

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center