[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

Recursion 2

The document explains the concept of a call stack using an example of filling names into a stack and retrieving them in reverse order. It also provides a recursive function to calculate the factorial of a number, demonstrating how the call stack works during execution. Additionally, it includes practice questions and fun programming challenges related to recursion and random number generation.

Uploaded by

cynthiarumutsa03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Recursion 2

The document explains the concept of a call stack using an example of filling names into a stack and retrieving them in reverse order. It also provides a recursive function to calculate the factorial of a number, demonstrating how the call stack works during execution. Additionally, it includes practice questions and fun programming challenges related to recursion and random number generation.

Uploaded by

cynthiarumutsa03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

By TannyEXY

Recursion 2

Talking about the Call stack.


A call stack is like a storage areas where you store data.

Call stack explanation


 Think of a pipe and you are to fill the pipe with stones.
 Provided the pipe is standing vertically on the ground.
 The first stone goes to the bottom of the pipe and the rest piles up to that stone.
 The last stone into the pipe is the first to come out and the first stone in the pipe is the last to
come out.
Example
 The following list of names is to be placed into a call stack
Tawanda, Jimbo, Sam, Trevor, Smith and Jack

Trevor
Jack
 As you can see the list
Jack piles up from the when
Smith Smith being filled with names.
 This pilling up is called
Trevor Trevor
stacking.
Sam Sam Sam  The list is thus called a
Jimbo Jimbo Jimbo Stack.

Tawnda Tawanda Tawanda

Filling the names into the call stack

 Let’s say you are calling out the


First one out names. You start calling the last
name to go into the list going
Jack down the list to the last name in
the list.
Smith  This calling then advances the
Jack name of the list to Call Stack.
Smith Smith
Trevor Trevor Trevor Tawanda
Sam Sam Last one out
Sam
Jimbo Jimbo Jimbo
Tawanda Tawanda Tawanda
Collecting names from the call stack
By TannyEXY

An empty Cal Stack


stackLast one out

Question
So given a function 𝑈𝑛 = 𝑈𝑛−1 × 𝑛 𝑎𝑛𝑑 𝑈1 = 1

Write a program that returns a value when n = 5. The value should be displayed in a textbox.

Solution Code and Interface


Public Class Form1

Function U(ByVal n As Integer)


If n = 1 Then

Return 1

Else

Return U(n - 1) * n

End If

End Function

Private Sub Button1_Click() Handles Button1.Click

TextBox1.Text = U(5)

End Sub
End Class
Explanation to the solution By TannyEXY
 Note that it’s only code explanation.
 Note that the shaded parts are points being executed by the program.

So at first the value of n is set to 5.


 Since n = 5 the program executes the Call Stack
Function U(ByVal n As Integer)  n = 5 else statement.
If n = 1 Then
 The else statement is calling the
Return 1 function so the process is assumed to
have been stopped for a while.
Else
 The program then stores 𝑼 𝟓−𝟏 ×𝟓
Return U(n - 1) * n
in a Call Stack then the new value of n
End If is changed to (n – 1) which is 5 – 1 = 4
End Function 𝑈4 × 5

So now the value of n is 4.


Call Stack
 Since n = 4 the program executes the
Function U(ByVal n As Integer)  n = 4
If n = 1 Then else statement.
 The else statement is calling the
Return 1
function so the process is assumed to
Else have been stopped for a while.

Return U(n - 1) * n  The program then stores 𝑼 𝟒−𝟏 ×𝟒


in a Call Stack then the new value of n
End If 𝑈3 × 4
is changed to (n – 1) which is 4 – 1 = 3
End Function
𝑈4 × 5

So now the value of n is 3.


 Since n = 3 the program executes the Call Stack
Function U(ByVal n As Integer)  n = 3 else statement.
If n = 1 Then
 The else statement is calling the
Return 1 function so the process is assumed to

Else have been stopped for a while.


𝑈2 × 3
 The program then stores 𝑼 𝟑−𝟏 ×𝟑
Return U(n - 1) * n
in a Call Stack then the new value of n 𝑈3 × 4
End If is changed to (n – 1) which is 3 – 1 = 2
End Function 𝑈4 × 5
So now the value of n is 2.
 By
Since n = 2 the program executes the TannyEXY
Call Stack
Function U(ByVal n As Integer)  n = 2 else statement.
If n = 1 Then
 The else statement is calling the
Return 1 function so the process is assumed to
𝑈1 × 2
Else have been stopped for a while.
 The program then stores 𝑼 𝟐−𝟏 ×𝟐 𝑈2 × 3
Return U(n - 1) * n
in a Call Stack then the new value of n 𝑈3 × 4
End If is changed to (n – 1) which is 2 – 1 = 1
End Function 𝑈4 × 5

So now the value of n is 1.  Since n = 1 the program executes the if


Call Stack
Function U(ByVal n As Integer)  n = 1 statement.
If n = 1 Then
 It makes the function 𝑼𝟏 = 𝟏
Return 1  The program starts to call the
functions in the call stack from the top
𝑈1 × 2
Else
going down to the bottom and it will 𝑈2 × 3
Return U(n - 1) * n
be substitute the values for
𝑈3 × 4
End If 𝑼 𝒏−𝟏 and moving on to the next. See
End Function
the process below
𝑈4 × 5

𝑈1 = 1 𝑈𝑛 = 𝑈𝑛−1 × 𝑛 𝑈1 × 2
𝑈2 × 3
𝑈3 × 4
1. n = 2 𝑈2 = 𝑈1 × 2 =1 × 2 = 2 𝑈2 = 2
𝑈4 × 5
𝑈2 × 3
2. n = 3 𝑈3 = 𝑈2 × 3 =2 × 3 = 6 𝑈3 = 6 𝑈3 × 4
𝑈4 × 5
3. n = 4 𝑈4 = 𝑈3 × 4 =6 × 4 = 24 𝑈4 = 24 𝑈3 × 4
𝑈4 × 5
4. n = 5 𝑈5 = 𝑈5 × 5 =24 × 5 = 120 𝑈5 = 120 𝑈4 × 5

So when the program finishes up with the above process it then returns the final value to the site where the
function was called. On the given solution it sends 120 to the textbox and it’s displayed.

Private Sub Button1_Click() Handles Button1.Click

TextBox1.Text = U(5)

End Sub
Like over here in
the code
By TannyEXY

Practice Questions

1. Write a function that returns the total number of seconds, calculated from a whole number
of hours, minutes and seconds provided as 3 parameters

2. Write a program that can compute the Fibonacci sequence. The program should accept the
number of terms the user wants and it displays the sequence in a Richtextbox. Use the
following function: = = = − −

3. Write a program that accepts a number and displays the factorial of that number. Use this
as your function = × . The program must use a recursive approach.

4. Write a program that counts up to 100 using a recursive approach. It should display the
sequence from the counting. It should display values from 0, all even numbers up to 100.

5. Write a tables tester. The program chooses 2 random numbers and asks the user what is
the product of these 2 numbers. If the user gets the answer wrong, the correct answer
should be displayed. The program should ask 10 questions and then display a score out o f 10
and a suitable message.

Fun Time

 The game 'Last one loses' is played by two players and uses a pile of n counters. Players
take turns at removing 1, 2 or 3 counters from the pile. The game continues until there
are no counters left and the winner is the one who does not take the last coun ter. Using
procedures, write a program to allow the user to specify n in the range 10 — 50 inclusive
and act as one player, playing at random until fewer than 5 counters remain. Try playing
against your program, and then playing to win.

 Create a procedure GetLotteryNumbers that will supply 6 unique random numbers


between 1 and 49. One possible strategy, or algorithm, is: \

Initialise an array by using a for loop to store the values 1 to 49


Repeatedly select a random element from array until a non-zero value is selected
Display this value
Set that element to zero
Repeat the above three steps until six numbers have been selected.

Madanaime: Contact: 0771936890 /0718830036


Email: tanakasteven450@gmail.com
Email: tanakasteven450@outlook.com

You might also like