C Exercise Example28
# C Programming Classic Exercise 28: Solving Age Relations Using Recursion
In computer science and programming, recursion is a powerful technique where a function calls itself to solve a smaller instance of the same problem. This tutorial demonstrates how to use recursion in C to solve a classic word problem involving age relationships.
---
## 1. Problem Description
Five people are sitting together. You want to find out the age of the fifth person.
* The **fifth** person says they are 2 years older than the **fourth** person.
* The **fourth** person says they are 2 years older than the **third** person.
* The **third** person says they are 2 years older than the **second** person.
* The **second** person says they are 2 years older than the **first** person.
* Finally, you ask the **first** person, and they say they are **10** years old.
**Question:** How old is the fifth person?
---
## 2. Problem Analysis & Mathematical Modeling
To solve this problem programmatically, we can define a mathematical relationship for the age of the $n$-th person, denoted as $f(n)$:
1. **Base Case:**
When $n = 1$, we know the exact age:
$$f(1) = 10$$
2. **Recursive Step:**
For any person $n > 1$, their age is 2 years older than the $(n-1)$-th person:
$$f(n) = f(n-1) + 2$$
### The Two Phases of Recursion:
* **Winding (Pushing/Going Down):** To find the age of the 5th person, the program requests the age of the 4th, which requests the 3rd, then the 2nd, and finally the 1st.
* **Unwinding (Returning/Coming Up):** Once the base case ($n=1$, age is 10) is reached, the function returns the value back up the call stack.
* $f(2) = 10 + 2 = 12$
* $f(3) = 12 + 2 = 14$
* $f(4) = 14 + 2 = 16$
* $f(5) = 16 + 2 = 18$
---
## 3. C Source Code Implementation
Below is the complete C program.
> **Note on Code Style:** The original code used the legacy K&R C style for function parameter declarations. The code below has been updated to modern ANSI C standards for better compatibility with modern compilers, while preserving the core logic.
```c
#include
/**
* Recursive function to calculate the age of the n-th person.
* @param n The position of the person (1 to 5)
* @return The age of the n-th person
*/
int get_age(int n) {
int current_age;
// Base Case: The first person is 10 years old
if (n == 1) {
current_age = 10;
}
// Recursive Case: n-th person is 2 years older than the (n-1)-th person
else {
current_age = get_age(n - 1) + 2;
}
return current_age;
}
int main() {
int target_person = 5;
int result_age = get_age(target_person);
printf("The age of the 5th person is: %d\n", result_age);
return 0;
}
```
---
## 4. Output and Execution
When you compile and run the program, it produces the following output:
```text
The age of the 5th person is: 18
```
---
## 5. Key Considerations and Best Practices
When working with recursive functions in C, keep the following points in mind:
* **Base Case is Mandatory:** Every recursive function must have a well-defined base case (in this example, `if (n == 1)`). Without a base case, the function will call itself infinitely, leading to a **Stack Overflow** error.
* **Stack Overhead:** Each recursive call adds a new stack frame to the call stack. For very deep recursions, an iterative approach (using a `for` or `while` loop) is often more memory-efficient.
* **Input Validation:** In production code, you should validate the input parameter `n`. If a user passes a value less than 1 (e.g., `0` or `-1`), the current recursive logic would result in infinite recursion. Adding a guard clause like `if (n < 1) return -1;` is highly recommended.
YouTip