Why does my Loop end after 11 Loops?
#41
So ... Holey Smoke ... some very informative stuff here. I'm going to print this off and keep it in my Basic Learning How To book.

On the issue of Recursion and a Counter, Tempodi remarks


Quote:DataCount = DataCount + 7 ' this is another alone variable that does not pass to the next recursive calling

If DataCount < 14 Then Recur ' this condition it will never happen because DataCount at each run is always 0 and becomes 7


But Steve's code, in his Recur subroutine, appears to have a successful counter working. Is there a reset back to zero for all Recursive calls as a normal use of Recursion or this reset to zero happening just in Tempodi's structure of his Recursion subroutine? I seem be getting my counters resetting only when I exit my Recursive routine but working ok within the Recursive calls but, while messing with a redesign of my Recursion call routine, I was able to jump from 11 loops to the 4000 loops, I then did a little more tinkering and now have endless looping going on. I never thought to look at an issue with the counter.
Reply
#42
Quote:@Dimster - Is there a reset back to zero for all Recursive calls as a normal use of Recursion . . .


Not that I know. The classic example of a recursion is the determination of the Fibonacci number.

As to the result: In Wikipedia (German), the zero is not taken into account. On the other hand, I orientated myself on the Matheduden.

Code: (Select All)
$Console:Only

Option _Explicit

Declare Function Fibonacci(eingabe As Integer) As _Integer64

Dim As Integer eingabe

Locate 2, 3
Print "Rekursive Berechnung der Fibonaccizahl"

Locate 4, 3
Input "Ihre Eingabe: ", eingabe

Locate 6, 3
Print Using "Die Fibonaccizahl von ### ist: #######"; eingabe, Fibonacci(eingabe)

'Fuer -Press Any Key-
Locate 10, 1

End


Function Fibonacci (eingabe As Integer)

  Dim fibonaccizahl As _Integer64

  If eingabe = 0 Or eingabe = 1 Then
    Fibonacci = 1
  Else
    fibonaccizahl = Fibonacci(eingabe - 1) + Fibonacci(eingabe - 2)
    Fibonacci = fibonaccizahl
  End If

End Function
Reply
#43
@Dimster

Quote:But Steve's code, in his Recur subroutine, appears to have a successful counter working. 


Yes because he uses a SHARED variable declared into the main... in other words he uses one of the way to build a controlled recursive calling.

at 38# you can see
Code: (Select All)
DIM SHARED AS LONG DataCount

and  in recursive SUB
Code: (Select All)
IF DataCount = 0 THEN SEEK #1, 1 'move to the start of the file 'may need to change to 0 for 0 index counting

I hope to be clear and understandable
Reply
#44
Ah.. so in a Do Loop the counter doesn't need to be Dimensioned or Shared but in a Recursive call it does.
Reply
#45
I still think this is not something to solve with recursion; find another example to play with if you want to experiment with recursion.

This scenario could be solved with a static sub that only reads the first time to an array in memory (7*4000 strings is not that much) and subsequent call to the function just reads from the array...

my 2ct
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#46
(02-09-2023, 05:28 PM)Dimster Wrote: Ah.. so in a Do Loop the counter doesn't need to be Dimensioned or Shared but in a Recursive call it does.

You don't need a counter. A recursive function calls itself until: n - n = 0. Applies: 0! = 1. This ends the recursive call, and the value is popped off the stack and displayed. (That's how I understand it.)

Recursive calculation using the factorial as an example.
Code: (Select All)
'Fakultaet rekursiv - 10. Feb. 2023

$Console:Only
Option _Explicit

Declare Function Fakultaet(n As Integer) As _Integer64

Dim As Integer n

Locate 2, 3
Print "Rekursive Berechnung der Fakultaet - (n!)"

Locate 4, 3
Input "Fakultaet von (n): ", n

Locate 5, 3
Print Using "Die Fakultaet von ### ist: ###,###,###"; n, Fakultaet(n)

End 'Hauptprogramm


Function Fakultaet (n As Integer)

  Dim As _Integer64 fakul

  If n = 0 Or n = 1 Then
    fakul = 1
  Else
    fakul = Fakultaet(n - 1) * n
  End If
  Fakultaet = fakul
End Function

[Image: Rekursion-Beispiel.jpg]
Reply
#47
Thanks for this great advice - and calling a spade a spade in terms of the degree of frustration one should put up with before scraping it all and starting again. The limits and nuisances of Recursion is what I'm playing with. With the help you guys have provided here, my program no longer stops after 11 loops. I'm messing with stacking Recursion - computer is humming. Thanks again
Reply
#48
(02-10-2023, 07:32 PM)Dimster Wrote:  With the help you guys have provided here, my program no longer stops after 11 loops. I'm messing with stacking Recursion - computer is humming. Thanks again

The IF . . . ELSE condition is mandatory, otherwise the program goes into an endless loop!

Example: 4! (n)
If n = 1 -> No --> Else  next
If n = 1 -> No --> Else
and so on ...
At last:
If n = 1 Yes -> Finish

Now the values will be pop from the stack and output.
Reply
#49
I'm not sure exactly what you mean KernelPanic. I'm not using an IF ...ELSE to end the Recursion but using a counter to Exit Sub. This seems to be working. It's a method I learnt from bPlus and his Recursion by Subroutine. Are you able to clarify what you mean by "mandatory". Will I have a stack issue just using a counter to Exit the Recursion Sub?
Reply
#50
I don't know how things are going for you now. Is this really a recursive processing, or is something just called after a counter?

From Herbert Schildt, C command library, recursion page 102:
When developing recursive functions, there have to be an IF statement somewhere that makes the function return without executing another recursive call. Without this provision, the function never returns after it is called. Writing recursive functions without an IF is a common mistake.

The same program as above, only without the IF. You have to break it off with Strng-C.

Code: (Select All)
$Console:Only
Option _Explicit

Declare Function Fakultaet(n As Integer) As _Integer64

Dim As Integer n

Locate 2, 3
Print "Rekursive Berechnung der Fakultaet - (n!)"

Locate 4, 3
Input "Fakultaet von (n): ", n

Locate 5, 3
Print Using "Die Fakultaet von ### ist: ###,###,###"; n, Fakultaet(n)

End 'Hauptprogramm


Function Fakultaet (n As Integer)

  Dim As _Integer64 fakul

  fakul = Fakultaet(n - 1) * n

  Fakultaet = fakul
End Function
Reply




Users browsing this thread: 2 Guest(s)