> 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/rushes/rush00/01.md).

# 01

#### **Code Structure Explanation**

1. **Printing characters (`ft_putchar`)**: This function handles single-character printing.
2. **First and Last Row (`first_line`, `last_row`)**:
   * Prints **first row** with `/` at the start and `\` at the end.
   * Prints **last row** with `\` at the start and `/` at the end.
   * Middle characters (`*`) are filled when width is more than 1.
3. **Middle Rows (`middle_rows`)**:
   * Uses `*` for left and right edges.
   * Fills spaces inside for widths greater than 1.
4. **Main Control Function (`display_chars`)**:
   * Calls `first_line`, `middle_rows`, and `last_row` in order.
5. **Main Function (`main`)**:
   * Calls `display_chars(5,1)` to print a test rectangle.

Code explanation

#### **1. Printing a Single Character (`ft_putchar`)**

```c
#include <unistd.h> // Includes write function for output

// Function to print a single character to standard output
void	ft_putchar(char c)
{
	write(1, &c, 1); // Writes character 'c' to standard output
}
```

✅ **Explanation:**

* `write(1, &c, 1);` → Writes the character `c` to the terminal.
* Used throughout the program to print individual characters.

#### **2. Handling the Last Character of a Line (`last_slash`)**

```c
// Function to handle the last character of the first and last row
void	last_slash(int a)
{
	if (a == 1) // If 'a' is 1, print '/' and move to a new line
	{
		ft_putchar('/');
		ft_putchar('\n');
	}
	else // Otherwise, print '\' and move to a new line
	{
		ft_putchar('\\');
		ft_putchar('\n');
	}
}
```

✅ **Explanation:**

* This function prints either `/` or `\` based on the parameter `a` and moves to a new line.
* **Used in the first and last rows** of the rectangle to ensure correct corner placement.

#### **3. Printing the First Row (`first_line`)**

```c
// Function to print the first row of the rectangle
void	first_line(int a)
{
	int	i;

	i = 0;
	if (a == 1) // If width is 1, just print '/' and return
	{
		last_slash(1);
	}
	while (i <= (a - 1) && a != 1) // Loop through characters in the first row
	{
		if (i == 0) // Print '/' as the first character
		{
			ft_putchar('/');
			i++;
		}
		if (i < (a - 1)) // Print '*' for the middle characters
		{
			ft_putchar('*');
			i++;
		}
		if (i == (a - 1)) // Print '\' as the last character
		{
			last_slash(2);
			i++;
		}
	}
}
```

✅ **Explanation:**

* Prints the **first row** of the rectangle.
* Starts with `/`, fills the middle with `*`, and ends with `\`.
* If width is **1**, only prints `/` (handled by `last_slash(1)`).

***

#### **4. Printing the Last Row (`last_row`)**

```c
// Function to print the last row of the rectangle
void	last_row(int a)
{
	int	i;

	i = 0;
	if (a == 1 && i == 0) // If width is 1, print '\' and return
	{
		last_slash(2);
	}
	while (i <= (a - 1) && a != 1) // Loop through characters in the last row
	{
		if (i == 0) // Print '\' as the first character
		{
			ft_putchar('\\');
			i++;
		}
		if (i < (a - 1)) // Print '*' for the middle characters
		{
			ft_putchar('*');
			i++;
		}
		if (i == (a - 1)) // Print '/' as the last character
		{
			last_slash(1);
			i++;
		}
	}
}
```

✅ **Explanation:**

* Similar to `first_line()`, but starts with `\` and ends with `/`.
* If width is **1**, only prints `\` (handled by `last_slash(2)`).

***

#### **5. Printing the Middle Rows (`middle_rows`)**

```c
// Function to print the middle rows of the rectangle
void	middle_rows(int a, int b)
{
	int	i;
	int	j;

	i = 0;
	j = 0;
	while (j < (b - 1)) // Loop through each middle row
	{
		if (i == 0 && a != 1) // Print '*' as the first character
		{
			ft_putchar('*');
			i++;
		}
		while (i < (a - 1)) // Print spaces in between
		{
			ft_putchar(' ');
			i++;
		}
		if (i == (a - 1) || i == 0) // Print '*' as the last character and move to the next line
		{
			ft_putchar('*');
			ft_putchar('\n');
			j++; // Move to the next row
			i = 0; // Reset `i` for the next row
		}
	}
}
```

✅ **Explanation:**

* Prints the **middle rows** of the rectangle.
* Uses `*` for the left and right edges.
* Fills spaces inside when width is greater than **1**.
* Loops through `b - 1` times (excluding the first and last row).

***

#### **6. Controlling the Printing Process (`display_chars`)**

```c
// Function to generate the rectangle and call respective functions
void	display_chars(int a, int b)
{
	first_line(a); // Print the first row
	middle_rows(a, (b - 1)); // Print middle rows
	last_row(a); // Print the last row
}
```

✅ **Explanation:**

* Calls the **three row functions** in order:
  1. `first_line(a)`: Prints the top row.
  2. `middle_rows(a, b - 1)`: Prints the middle rows (if height > 2).
  3. `last_row(a)`: Prints the bottom row.
* Ensures the correct sequence of printing.

***

#### **7. Main Function (`main`)**

```c
// Main function to test the implementation
int	main(void)
{
	display_chars(5, 1); // Calls the function with width = 5, height = 1
	return (0);
}
```

✅ **Explanation:**

* Calls `display_chars(5,1)` to **generate a rectangle of width 5 and height 1**.
* You can change values to test different rectangle sizes.

***

### **Summary of Functions and Responsibilities**

| Function Name     | Responsibility                                                 |
| ----------------- | -------------------------------------------------------------- |
| `ft_putchar()`    | Prints a single character                                      |
| `last_slash()`    | Prints `/` or `\` at the correct position                      |
| `first_line()`    | Prints the first row                                           |
| `last_row()`      | Prints the last row                                            |
| `middle_rows()`   | Prints the middle rows with `*` on the sides and spaces inside |
| `display_chars()` | Calls all the row functions in order                           |
| `main()`          | Starts the program and calls `display_chars()`                 |


---

# 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/rushes/rush00/01.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.
