0% found this document useful (0 votes)
10 views13 pages

Dsa 16-17

Uploaded by

Jane Mandolado
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)
10 views13 pages

Dsa 16-17

Uploaded by

Jane Mandolado
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/ 13

LESSON 16 SAMPLE PROGRAMS

1. Lesson 16 Program 1

2. Lesson 16 Program 2
3. Lesson 16 Program 3

4. Lesson 16 Program 4
5. Lesson 16 Program 5

6. Lesson 16 Program 6
LESSON 17 EXERCISES

1.

2.
3.

4.
5.

6. What is the use of recursion in a program?


- In C, recursion allows a function to call itself to solve problems by breaking
them into smaller subproblems, simplifying tasks like factorials, tree traversals, and
divide-and-conquer algorithms. It’s powerful for hierarchical or repetitive tasks but
requires a base case to avoid infinite loops and can risk stack overflow or inefficiencies
for large inputs.
7. Explain the use of stack in recursion.
-In recursion, the program uses a call stack to manage function calls. Each time
a recursive function is called, its state (local variables, return address) is pushed onto
the stack. When the base case is reached, the function starts returning, and the stack is
popped to restore the previous state. This stack mechanism allows the program to track
multiple recursive calls.
8. What do you mean by winding and unwinding phase?
-The winding phase occurs when recursive calls are made, pushing function
states onto the stack, and the problem is divided into smaller subproblems. The
unwinding phase starts when the base case is reached, and the function begins
returning results, popping states off the stack and combining solutions.
9. How to write a recursive function?
• Define the base case: This stops the recursion when a specific condition is met.
• Write the recursive case: Call the function itself with modified arguments to
reduce the problem size.
• Ensure progress toward the base case: Avoid infinite recursion.

10. What is the difference between tail and non-tail recursion?


Tail Recursion
• The recursive call is the last operation performed in the function.
• No computation happens after the recursive call.
• It is more memory-efficient as the compiler can optimize it (e.g., tail call
optimization), potentially replacing recursive calls with a loop.
Non-Tail Recursion
• The recursive call is not the last operation; further computation or operations are
performed after it.
• It uses more stack space as each recursive call must remain active until the
computation is completed.
10B
11. What is indirect recursion?
-Indirect recursion occurs when a function calls another function, which in turn
calls the original function, creating a cycle. This chain of function calls continues until a
base case is reached.
12. What is the difference between iteration and recursion?
-Iteration uses loops like for or while to repeat a block of code, does not require
extra memory, is controlled by a loop condition, and is generally faster and more
memory-efficient. Recursion involves a function calling itself, uses stack memory for
each call, relies on a base case for termination, and is easier to express hierarchical or
repetitive problems but may be less efficient.

13.

14.
15.

16.
17.

18.
19.

20.
21.

You might also like