Results 1 to 11 of 11

Thread: Looping

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41

    Looping

    i have 4 numbers ex(45,36,31,14)
    and i want to put all the posiblities that is lower or equal ex(100)
    so if the sum of it is lower or equal the program writes the qualification he solved
    for example we start with number 45
    100>45 so print "45"
    45+45=90
    100>90 so print "45 - 45"
    45+45+45=135
    100<135 so it goes to the next step
    45+36=81
    100>81 so print "45 - 36"
    45+36+36=117
    100<117 so it goes to the next step
    45+36+31=112
    100<112 so it goes to the next step
    45+36+31+14=126
    100<126 so it goes to the next step
    45+36+14=95
    100>95 so print " 45 - 36 14"

    and i don't know what is the way to make a loop like this

    and the result sould be like this
    Code:
    45
    45 - 45
    45 - 36
    45 - 36 - 14
    45 - 31
    45 - 31 - 14
    45 - 14
    45 - 14 - 14
    45 - 14 - 14 -14
    36
    36 - 36
    36 - 36 - 14
    36 - 36 - 14 - 14
    36 - 31
    36 - 31 - 31 
    36 - 31 - 14
    36 - 31 - 14 - 14
    36 - 14
    36 - 14 - 14
    36 - 14 - 14 - 14
    36 - 14 - 14 - 14 - 14
    31
    31 - 31
    31 - 31 - 31
    31 - 31 - 14
    31 - 31 - 14 - 14
    31 - 14
    31 - 14 - 14
    31 - 14 - 14 - 14
    31 - 14 - 14 - 14 - 14
    14
    14 - 14
    14 - 14 - 14
    14 - 14 - 14 - 14
    14 - 14 - 14 - 14 - 14
    14 - 14 - 14 - 14 - 14 - 14
    14 - 14 - 14 - 14 - 14 - 14 - 14
    and i tried and i've succeded to make all posiblities for no. 45
    and all fo no.36 except
    36 - 36 - 14
    36 - 36 - 14 - 14

    and it doesn't matter how the code is long or complecated all i want is to get the final result


    and if there is any point u understand tell me and i'll explain what u want




    thank u and good bye
    Walid M. Bahgat

  2. #2
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    VB Code:
    1. Private Function ret_Many(what As String, times As Integer) As String
    2. Dim i As Integer, ret As String
    3.  
    4. ret = ""
    5. For i = 1 To times
    6.     ret = ret + "- " + what
    7. Next i
    8. ret_Many = ret
    9.  
    10. End Function
    11.  
    12. Private Sub Form_Load()
    13. Dim X() As Integer, times As Integer
    14.  
    15. ReDim X(1 To 4)
    16. X(1) = 45
    17. X(2) = 36
    18. X(3) = 31
    19. X(4) = 14
    20.  
    21. Text1.Text = ""
    22.  
    23. For i = 1 To 4
    24.     If i <> 1 Then Text1.Text = Text1.Text & vbNewLine
    25.     Text1.Text = Text1.Text & X(i)
    26.     For j = i To 4
    27.         Sum = X(i)
    28.         times = 0
    29.         While Sum < 100
    30.             times = times + 1
    31.             Sum = Sum + X(j)
    32.             If Sum < 100 Then
    33.                 Text1.Text = Text1.Text & vbNewLine & X(i) & " " & ret_Many(Str(X(j)), times)
    34.             End If
    35.         Wend
    36.     Next j
    37. Next i
    38.  
    39. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41

    Talking

    thank you marnitzg for this great code
    and this is shorter and more simple than the code i did
    and it's general too

    but there still is the problem

    that i can't reach the posibility wich is
    36 - 36 - 14
    36 - 36 - 14 - 14

    and ur code is great but it's till 2 levels only
    like it didn't get the posibility
    45 - 36 - 14
    45 - 31 - 14

    and i'll try to fix it
    and if u can help me

    thankx marnitzg
    Walid M. Bahgat

  4. #4
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372

    Ooops!

    Slightly more complex than I thought . Will have a fix later, got to study for a semester test tomorrow though!

    Cheers

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41

    Thumbs up

    ok marnitzg
    and wish to u all success

    bye
    Walid M. Bahgat

  6. #6
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking OK...

    Firstly, good luck

    Anyway, to solve it, you need to use a recursive function (one that calls itself)

    OR, have an array (Nums(7)) That you fill up with each number (45,36,31 and 14), then check if the sum is less than 100. if so, and numbers are in right order, print result.
    (But also needs recursive sub)
    sql_lall

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41
    plz explain more
    i don't understand what u mean
    Walid M. Bahgat

  8. #8
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking OK...

    Firstly, have you heard of Permutations/Combinations?? (if not, just go to google and find out a bit about them)

    What you need for this is all the permutations of 7 numbers made up of the 4 you have. (i.e. every way you can have 7 numbers, all of which are either 45,36,31 or 14. Then you can check these for the correct sums.

    Now, the easiest way to get all possibilities like this is use a recursive sub:

    VB Code:
    1. Public Sub Recurse()
    2.   'The simplest recursive sub. Note that this, when called, will run
    3.   'for ever!!
    4.   Call Recurse
    5. End Sub

    Note that it calls itself. To make it useful for your purposes, change it a bit like this:

    VB Code:
    1. Public Sub Recurse(a as integer)
    2. If a < 7 then
    3.  for i(a) = 1 to 4  
    4.  numbers(a) = vals(i)
    5.  Call Recurse(a+1)
    6.  next i(a)
    7. Else:
    8. Bol = true
    9.  for k = 1 to 6
    10.  if numbers(k)>=numbers(k+1) then Bol = false
    11.  next k
    12.   If (Bol) then printthem()
    13. end if
    14. End Sub

    I haven't checked this yet, so it probably isn't right.

    Also, it isn't finished (you need other stuff, like a printthem() sub, and some public variables), but it's just the general idea.
    sql_lall

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41
    hey where r u guys

    hi sql_lall sorry i didn't also understand u plz explain more
    and the code didn't work

    hi marnitzg where r u

    anyone know how to make a loop to solve this problem
    Walid M. Bahgat

  10. #10
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Sorry, actually forgot about this . I don't like using recursive procedures (no particular reason just don't like them) but anyway decided to take sql_lall's advice so here it is. Sorry about not getting back sooner
    VB Code:
    1. Option Explicit
    2. Private Nums(1 To 4)
    3.  
    4. Public Sub Recurse(ByVal Total As Integer, ByVal prnt As String, ByVal position As Integer)
    5.     Total = Total + Nums(position)
    6.     If Total <= 100 Then
    7.        Text1.Text = Text1.Text & prnt & vbNewLine
    8.        While position <= 4
    9.         Recurse Total, prnt & " - " & Trim(Str(Nums(position))), position
    10.         position = position + 1
    11.        Wend
    12.     End If
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16. Dim i As Integer
    17.  
    18. Nums(1) = 45
    19. Nums(2) = 36
    20. Nums(3) = 31
    21. Nums(4) = 14
    22.  
    23. Text1.Text = ""
    24.  
    25. For i = 1 To 4
    26.     Call Recurse(0, Trim(Str(Nums(i))), i)
    27. Next i
    28.  
    29. End Sub

    Hope that works now. A quick check looks like it works, but unfortunately thats what I thought last time

    Cheers
    Marnitz

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41

    thankx marnitzg it worked
    thankx all of u guys
    Walid M. Bahgat

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width