0% found this document useful (0 votes)
19 views16 pages

Lec 3

The document discusses using a stack to convert decimal numbers to binary. It explains that the remainders from successive divisions of the decimal number by 2 are pushed onto a stack. Once the divisions are complete, the remainders are popped off the stack in reverse order to produce the binary number. As an example, it shows how 26 in decimal converts to 11010 in binary using this stack method.

Uploaded by

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

Lec 3

The document discusses using a stack to convert decimal numbers to binary. It explains that the remainders from successive divisions of the decimal number by 2 are pushed onto a stack. Once the divisions are complete, the remainders are popped off the stack in reverse order to produce the binary number. As an example, it shows how 26 in decimal converts to 11010 in binary using this stack method.

Uploaded by

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

Stack - 2

Instructor: Javeria Naz


Decimal to Binary conversion using Stack
 Decimals numbers can be converted into the equivalent
binary numbers using stack.
 Figure gives the visual process of conversion of decimal
no 26 into binary.
 The stack data structure can be used to convert decimal
number to binary.
 The push operation of stack is used to store the remainder
of each step of the division operation.
 After division process, the remainders of all the steps can
be popped. In this way, the remainders will be removed in
reverse order.
Cont.
 For example, the decimal number 26 can be converted into binary number using stack:
 Divide 26 by 2. The quotient of this step is 13, and remainder is 0. Push remainder i.e. 0 onto
the stack.
 Divide 13 by 2. The quotient of this step is 6, and remainder is 1. Push remainder i.e. 1 onto the
stack.
 Divide 6 by 2. The quotient of this step is 3, and remainder is 0. Push remainder i.e. 0 onto the
stack.
 Divide 3 by 2. The quotient of this step is 1, and remainder is 1. Push remainder i.e. 1 onto the
stack.
 Divide 3 by 2. The quotient of this step is 1, and remainder is 1. Push remainder i.e. 1 onto the
stack.
 POP all values from the stack and print in the same sequence i.e. 11010
1
1 1
0 0 0
1 1 1 1
0 0 0 0 0
Algorithm (Decimal to Binary Conversion)
Code (Decimal to Binary Conversion)
Cont.
Recursion
 Recursion is a powerful programming technique in which either a single
function calls itself again and again (direct recursion) or two functions call
each other again and again (indirect recursion) till the final calculation of
the result is obtained.
 The involved function in the recursion is called recursive function. The
number of times a function is called recursively is called depth of
recursion.
 Recursion is a fundamental concept in computer science. The feature of
calling a function from itself was not supported in older programming
languages like FORTRAN, COBOL, and BASIC. However, all the latest
modern programming languages(such as C++and Java)support recursion.
Conditions for Recursive Function
There are two important conditions that must be satisfied by any recursive
function. These conditions are necessary to prevent the recursive function
from running infinitely. The conditions are as follows:
 A recursive function must have a non-recursive part, called the base
criteria. It is the criteria or condition used to terminate the execution of the
recursive function.
 Each time the recursive function is called directly or indirectly, the condition
must get closer to the base criteria.
 A recursive function that fulfills these two conditions is called a well-defined
recursive function.
Implementation of Recursive Function
through Stacks
 A recursive function can be executed several times depending on the
situation. It receives values from the calling function and transmits results
back to it.
 For its proper functioning, it must save the parameters and variables upon
execution and restore these parameters and variables at completion.
While returning values, the function must keep track of the return address
in the calling function.
 This return address is used to transfer the control back to its proper place
in the calling function. It saves the return address of each calling function
in the same order in which it is called and returns the control to proper
place in the calling function each time the control is returned.
Cont.

 The recursive function terminates execution when the base criteria is


reached. Then it returns parameters and variables to the calling function. It
returns these values such that the values from the last execution are
returned first.
 This last-in and first-out ( LIFO) characteristics of a recursive function
shows that stack is the most suitable data structure for its implementation.
 At each function call, the stack is pushed to save the necessary values.
Upon exit from the function, the stack is popped to retrieve the values.
Parts of Recursive Function
General algorithm for a recursive function contains the following three parts:

 Prologue: It is the opening part of the recursive function. Its purpose is to


save the formal parameters, local variables and return address.
 Body: It contains definition of the function, including the test criteria, in
which the function calls itself. Each time the function calls itself, the
prologue of the function saves all necessary information required for its
functioning.
 Epilogue: It is the last part of the recursive function. It restores the most
recently saved parameters, local variables and return addresses.
Factorial Concept

n n!

1 1 1 1

2 2×1 = 2 × 1! =2

3 3×2×1 = 3 × 2! =6

4 4×3×2×1 = 4 × 3! = 24

5 5×4×3×2×1 = 5 × 4! = 120
Example
Algorithm
Code
Difference between Recursion and Iteration
Property Recursion Iteration

A set of instructions repeatedly


Definition Function calls itself.
executed.

Application For functions. For loops.

Through base case, where there will When the termination condition for
Termination
be no function call. the iterator ceases to be satisfied.

Used when code size needs to be Used when time complexity needs to
Usage small, and time complexity is not an be balanced against an expanded
issue. code size.

Code Size Smaller code size Larger Code Size.

Relatively lower time


Very high(generally exponential)
Time Complexity complexity(generally polynomial-
time complexity.
logarithmic).

You might also like