> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c00/ft_print_combn.md).

# ft\_print\_combn

This exercise requires you to write a function `ft_print_combn` that displays all different combinations of `n` digits in ascending order. The value of `n` will be between `1` and `9` (inclusive). Each combination should be printed on a single line, separated by commas and spaces.

***

#### **Requirements:**

1. **Input:** An integer `n` (where `0 < n < 10`).
2. **Output:**
   * Print all unique combinations of `n` digits in ascending order.
   * Each combination should be separated by a comma and a space (`,` ).
   * The last combination should **not** be followed by a comma and space.
3. **Allowed Function:** Only `write` can be used for output.
4. **Return Type:** The function should not return anything (`void`).

***

#### **Steps to Solve:**

1. **Initialize an Array:**
   * Use an array to store the digits of the current combination.
   * Start with the smallest possible combination (e.g., for `n = 2`, start with `01`).
2. **Generate Combinations:**
   * Use a loop to generate all possible combinations of `n` digits.
   * Ensure that each digit is greater than the previous one to avoid repetition within a combination.
3. **Print the Combinations:**
   * Use the `write` function to print each combination.
   * Add a comma and space after each combination except the last one.
4. **Edge Case Handling:**
   * Ensure the last combination is printed without a trailing comma and space.

***

#### **Code Implementation:**

```c
#include <unistd.h>  // For the write function

void	print_combination(int *digits, int n)
{
	char	c;
	int		i;

	i = 0;
	while (i < n)
	{
		c = digits[i] + '0';  // Convert digit to ASCII
		write(1, &c, 1);  // Print the digit
		i++;
	}
}

void	ft_print_combn(int n)
{
	int	digits[9];  // Array to store the digits of the combination
	int	i;

	// Initialize the array with the smallest combination
	i = 0;
	while (i < n)
	{
		digits[i] = i;
		i++;
	}

	// Generate and print all combinations
	while (1)
	{
		print_combination(digits, n);  // Print the current combination

		// Find the rightmost digit that can be incremented
		i = n - 1;
		while (i >= 0 && digits[i] == 9 - (n - 1 - i))
		{
			i--;
		}

		// If no such digit is found, we've generated all combinations
		if (i < 0)
			break;

		// Increment the digit and reset the following digits
		digits[i]++;
		i++;
		while (i < n)
		{
			digits[i] = digits[i - 1] + 1;
			i++;
		}

		// Print a comma and space between combinations
		if (digits[0] != 10 - n)
		{
			write(1, ", ", 2);
		}
	}
}
```

***

#### **Explanation:**

1. **Initialization:**
   * The array `digits` is used to store the digits of the current combination.
   * The array is initialized with the smallest possible combination (e.g., for `n = 3`, it starts with `0, 1, 2`).
2. **Generating Combinations:**
   * The outer `while (1)` loop generates all combinations.
   * The inner `while` loop finds the rightmost digit that can be incremented without violating the ascending order constraint.
   * If no such digit is found, the loop breaks, indicating that all combinations have been generated.
3. **Printing the Combinations:**
   * The `print_combination` function prints the current combination.
   * A comma and space are printed between combinations, except after the last one.
4. **Edge Case Handling:**
   * The condition `if (digits[0] != 10 - n)` ensures that the last combination is printed without a trailing comma and space.

***

#### **Example Output:**

If you call `ft_print_combn(2)`, the output will look like this:

```
01, 02, 03, 04, 05, 06, 07, 08, 09, 12, ..., 89
```

If you call `ft_print_combn(3)`, the output will look like this:

```
012, 013, 014, 015, 016, 017, 018, 019, 023, ..., 789
```

***

#### **Key Points:**

* **Combination Generation:** The function generates combinations by incrementing digits and resetting subsequent digits to maintain ascending order.
* **Edge Case Handling:** The last combination is handled separately to avoid printing an unnecessary comma and space.
* **Efficiency:** The loops are optimized to avoid unnecessary iterations.

***

#### **Testing the Function:**

Here’s a simple `main.c` to test the `ft_print_combn` function:

cCopy

```c
#include <unistd.h>

// Function prototype
void ft_print_combn(int n);

int main(void)
{
    ft_print_combn(2);  // Call the function to print combinations for n = 2
    write(1, "\n", 1);
    return 0;
}
```

**Expected Output for `n = 2`:**

Copy

```
01, 02, 03, 04, 05, 06, 07, 08, 09, 12, ..., 89
```

***

#### **Conclusion:**

This exercise tests your ability to generate and print combinations of digits in ascending order. The use of loops and careful handling of edge cases ensures that all combinations are printed correctly. The function is efficient and adheres to the constraints of the exercise.\
\ <mark style="color:red;">**This exercise was not checked with moulinette, please bu sure before submitting**</mark>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c00/ft_print_combn.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
