Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, I kinda suck at coding recursive functions !
the problem I need to solve is this :
in a dart game a person can score :7,15,19,23,29 or 37 points in each shot
find the combinations in which a player scores 100 points with exactly six shots.
so I wrote this : (vb.net )

VB
arr(0) = 7
     arr(1) = 15
     arr(2) = 19
     arr(3) = 23
     arr(4) = 29
     arr(5) = 37

     For i = 0 To 5

         For j = 0 To 5
             For k = 0 To 5

                 For l = 0 To 5

                     For m = 0 To 5


                         For n = 0 To 5

                             sum = arr(i) + arr(j) + arr(k) + arr(l) + arr(m) + arr(n)
                             If sum = 100 Then
                                 My.Computer.FileSystem.WriteAllText("C:\dar.txt", arr(i) & "   " & arr(j) & "   " & arr(k) & "   " & arr(l) & "   " & arr(m) & "   " & arr(n) & vbNewLine, True)
                             End If
                         Next
                     Next

                 Next


             Next


         Next

     Next


but it is a very poor solution as you can see !
how can I do this using recursion ?
I first tried to use recursion but it computer only the first combination (7 7 7 7 7 7 ) and then it would do (15 7 7 7 7 7 ) forever

thank you very much !!
Posted
Updated 25-Apr-14 3:04am
v2
Comments
Sergey Alexandrovich Kryukov 25-Apr-14 7:52am    
This is not C++, why this tag? Please remove it.<br>
Looking at the code does not make sense before you explain what you are going to achieve. "So I wrote this" does not seem to be related to your explanation.<br>
This is not called "recursion".
—SA
EbolaHost 25-Apr-14 9:06am    
you're right about the c++
what I want to achieve is to 'find the combinations in which a player scores 100 points with exactly six shots.' and it's preety obvious from the code even for a rookie like me .
I know this code isn't recursive but uses iteration instead !
but I want the recursive version of the code...that's all :)

That isn't recursion, that's iteration.
To do this recursively, you need a method which accepts a list of possible shots and a target value.
The method then takes one of the shots from the list, and passes the remainder and the new target to itself, by calling itself - it uses itself to work out the rest of the combinations.

I'm not going to go into full details - that would be a lot of work for me (and frankly I can't be bothered to do that at the moment, plus it wouldn;t help you to be given teh solution) but look at a simpler example, and it might make sense:
VB
Private Function factorial(value As Integer) As Integer
    If value <= 1 Then
        Return 1
    End If
    Return value * factorial(value - 1)
End Function
5! is 5 * 4!, which is 5 * (4 * 3!) which is 5 * (4 * (3 * 2!)) and so forth, so it calls itself to work out the other "iterations".

When you understand how that works, then start looking at your problem - it's more complicated, but not a lot when you get your head round it.
 
Share this answer
 
OriginalGriff gives a good description of recursion, but this problem is actually much more similar to the 8-queens problem and can be solved recursively using Backtracking[^]
Here is a link to a description of the recursive backtracking for Solving 8 queen problem[^]
Once you understand this, your problem should be pretty easy to code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900