> 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_comb.md).

# ft\_print\_comb

This exercise requires you to write a function `ft_print_comb` that displays all unique combinations of three different digits in ascending order. The combinations should be listed in ascending order, and no repetition of digits is allowed within a single combination.

***

#### **Requirements:**

1. **Input:** None (the function takes no parameters).
2. **Output:**
   * Print all unique combinations of three 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 Digits:**
   * Use three variables (or an array) to represent the three digits in the combination.
   * Start with the smallest possible combination: `012`.
2. **Nested Loops:**
   * Use nested loops to iterate through all possible combinations of three 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 (`789`) is printed without a trailing comma and space.

***

#### **Code Implementation:**

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

void	print_number(char *n)
{
	if (n[0] == '7' && n[1] == '8' && n[2] == '9')
		write(1, n, 3);  // Print the last combination without a comma
	else
	{
		write(1, n, 3);  // Print the combination
		write(1, ", ", 2);  // Print a comma and space
	}
}

void	ft_print_comb(void)
{
	char	n[3];  // Array to store the three digits

	n[0] = '0';  // Start with the smallest digit
	while (n[0] <= '7')  // First digit can go up to '7'
	{
		n[1] = n[0] + 1;  // Second digit starts one above the first
		while (n[1] <= '8')  // Second digit can go up to '8'
		{
			n[2] = n[1] + 1;  // Third digit starts one above the second
			while (n[2] <= '9')  // Third digit can go up to '9'
			{
				print_number(n);  // Print the current combination
				n[2]++;  // Increment the third digit
			}
			n[1]++;  // Increment the second digit
		}
		n[0]++;  // Increment the first digit
	}
}
```

***

#### **Explanation:**

1. **Initialization:**
   * The array `n[3]` is used to store the three digits of the combination.
   * The first digit (`n[0]`) starts at `'0'` and goes up to `'7'` because the last valid combination starts with `'7'` (`789`).
2. **Nested Loops:**
   * The outer loop iterates through the first digit (`n[0]`).
   * The middle loop iterates through the second digit (`n[1]`), starting one above the first digit.
   * The inner loop iterates through the third digit (`n[2]`), starting one above the second digit.
3. **Printing the Combinations:**
   * The `print_number` function is called to print the current combination.
   * If the combination is `789`, it prints without a trailing comma and space.
   * For all other combinations, it prints the combination followed by `,` .
4. **Edge Case Handling:**
   * The condition `if (n[0] == '7' && n[1] == '8' && n[2] == '9')` ensures that the last combination (`789`) is printed without a trailing comma and space.

***

#### **Example Output:**

If you run the function, the output will look like this:

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

***

#### **Key Points:**

* **Nested Loops:** The use of nested loops ensures that all combinations are generated in ascending order without repetition.
* **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_comb` function:

cCopy

```c
#include <unistd.h>

// Function prototype
void ft_print_comb(void);

int main(void)
{
    ft_print_comb();  // Call the function to print combinations
    return 0;
}
```

**Expected Output:**

Copy

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

***

#### **Conclusion:**

This exercise tests your ability to work with nested loops and handle edge cases in output formatting. The key is to ensure that all combinations are generated in ascending order and that the output is formatted correctly. The use of the `write` function ensures that the output is efficient and adheres to the constraints of the exercise.


---

# 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_comb.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.
