3 Replies - 381 Views - Last Post: 25 March 2011 - 06:46 AM Rate Topic: -----

#1 gbk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-March 11

Turn a recursive Function into a stack base

Posted 23 March 2011 - 05:03 PM

hi i have been asked to turn a recursive function into a stack base but i am not sure what that's mean but i choose the fibonacci recursive function and i turned it into the stack base ?? is this the right way or the right example to answer this question ?
Public Function fib(ByVal value As Integer) As Integer

        Dim x As Stack
        x = New Stack(30)
        Dim temp As Integer = 1
        Dim result As Integer = 0


        x.Push(value - 1)
        x.Push(value - 2)

        While (x.Empty() = False)

            temp = x.Pop()

            If (temp = 1 Or temp = 0) Then
                result = result + temp
             Else
                x.Push(temp - 1)
                x.Push(temp - 2)
            End If

        End While

        Return result


    End Function


This post has been edited by macosxnerd101: 23 March 2011 - 05:07 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Turn a recursive Function into a stack base

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,290
  • Joined: 12-June 08

Re: Turn a recursive Function into a stack base

Posted 23 March 2011 - 05:46 PM

What do you mean by "stack base"?

this?
http://www.artima.co...6&thread=167848

Just a heads up if you are not sure about a term or an assignment it pays to ask the teacher for clarification. I mean it is *your* education.

Hit me up with a bit clearer terms!

Edit: Help me, help you. Help ME... help YOU!

This post has been edited by modi123_1: 23 March 2011 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 gbk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-March 11

Re: Turn a recursive Function into a stack base

Posted 25 March 2011 - 06:12 AM

Thank you for your reply but actually i have not been asked by my teacher , i have interview it will take place after a week from now !! and they told to prepare really well !! one of the thing that i need to know is how to turn e recursive into a stack base no matter what the language that you will use so that's why i am not sure what exactly that's mean :)
Was This Post Helpful? 0
  • +
  • -

#4 MrPerq  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 01-March 11

Re: Turn a recursive Function into a stack base

Posted 25 March 2011 - 06:46 AM

Try implementing the QuickSort Algoritm in a single function,
without using recursion. You do this by keeping a list of ranges
you still need to divide, and repeat the algoritm in a while loop
till the list is empty.

This is usually done for increased performance, as recursive algoritms
put a really big pressure on the (Function)Stack.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1