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

# ft\_print\_comb2

This exercise requires you to write a function `ft_print_comb2` that displays all unique combinations of two two-digit numbers (in the format `XX XX`) between `00` and `99`. The combinations should be listed in ascending order, and no repetition of numbers is allowed within a single combination.

***

#### **Requirements:**

1. **Input:** None (the function takes no parameters).
2. **Output:**
   * Print all unique combinations of two two-digit numbers 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 Numbers:**
   * Use two variables to represent the two two-digit numbers in the combination.
   * Start with the smallest possible combination: `00 01`.
2. **Nested Loops:**
   * Use nested loops to iterate through all possible combinations of two two-digit numbers.
   * Ensure that the second number is always greater than the first to avoid repetition within a combination.
3. **Print the Combinations:**
   * Use the `write` function to print each combination in the format `XX XX`.
   * Add a comma and space after each combination except the last one.
4. **Edge Case Handling:**
   * Ensure the last combination (`98 99`) is printed without a trailing comma and space.

***

#### **Code Implementation:**

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

void	print_number(int n)
{
	char	c;

	if (n < 10)
	{
		c = n + 48;  // Convert single-digit number to ASCII
		write(1, "0", 1);  // Print leading zero
		write(1, &c, 1);  // Print the digit
	}
	else
	{
		c = n / 10 + 48;  // Convert tens digit to ASCII
		write(1, &c, 1);  // Print the tens digit
		c = (n % 10) + 48;  // Convert units digit to ASCII
		write(1, &c, 1);  // Print the units digit
	}
}

void	ft_print_comb2(void)
{
	int	col[2];  // Array to store the two numbers

	col[0] = 0;  // Start with the smallest number
	while (col[0] <= 98)  // First number can go up to 98
	{
		col[1] = col[0] + 1;  // Second number starts one above the first
		while (col[1] <= 99)  // Second number can go up to 99
		{
			print_number(col[0]);  // Print the first number
			write(1, " ", 1);  // Print a space
			print_number(col[1]);  // Print the second number
			if (col[0] == 98 && col[1] == 99)
			{
				write(1, "", 1);  // Print nothing for the last combination
			}
			else
			{
				write(1, ", ", 2);  // Print a comma and space
			}
			col[1]++;  // Increment the second number
		}
		col[0]++;  // Increment the first number
	}
}
```

***

#### **Explanation:**

1. **Initialization:**
   * The array `col[2]` is used to store the two two-digit numbers.
   * The first number (`col[0]`) starts at `0` and goes up to `98` because the last valid combination starts with `98` (`98 99`).
2. **Nested Loops:**
   * The outer loop iterates through the first number (`col[0]`).
   * The inner loop iterates through the second number (`col[1]`), starting one above the first number.
3. **Printing the Combinations:**
   * The `print_number` function is called to print each two-digit number.
   * If the combination is `98 99`, 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 (col[0] == 98 && col[1] == 99)` ensures that the last combination (`98 99`) is printed without a trailing comma and space.

***

#### **Example Output:**

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

```
00 01, 00 02, 00 03, ..., 00 99, 01 02, ..., 97 99, 98 99
```

***

#### **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.
* **Number Formatting:** The `print_number` function ensures that single-digit numbers are printed with a leading zero (e.g., `00`, `01`, etc.).

***

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

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

cCopy

```c
#include <unistd.h>

// Function prototype
void ft_print_comb2(void);

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

**Expected Output:**

```
00 01, 00 02, 00 03, ..., 00 99, 01 02, ..., 97 99, 98 99
```

***

#### **Conclusion:**

This exercise builds on the previous one by introducing two-digit numbers and more complex formatting. It tests your ability to work with nested loops, handle edge cases, and format output 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_comb2.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.
