> 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/03.md).

# 03

### **Complete Solution and 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:**

* Uses `write(1, &c, 1);` to print a character to the terminal.
* This function is used throughout the program to print characters.

***

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

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

	i = 0;
	while (i < width) // Loop through the entire width
	{
		if (i == 0) // Print 'A' at the beginning
			ft_putchar('A');
		else if (i == width - 1) // Print 'C' at the end
			ft_putchar('C');
		else // Print 'B' for the middle characters
			ft_putchar('B');
		i++;
	}
	ft_putchar('\n'); // Move to the next line
}
```

✅ **Explanation:**

* **First character** is `'A'`.
* **Last character** is `'C'`.
* **Middle characters** are `'B'`, forming the horizontal border.
* Moves to a **new line** after printing the first row.

***

#### **3. Printing the Last Row (`last_line`)**

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

	i = 0;
	while (i < width) // Loop through the entire width
	{
		if (i == 0) // Print 'A' at the beginning
			ft_putchar('A');
		else if (i == width - 1) // Print 'C' at the end
			ft_putchar('C');
		else // Print 'B' for the middle characters
			ft_putchar('B');
		i++;
	}
	ft_putchar('\n'); // Move to the next line
}
```

✅ **Explanation:**

* **Same logic as `first_line()`**, because in **Rush 03**, the first and last rows are identical.
* Ensures the **first and last characters** are `'A'` and `'C'`.
* Moves to a **new line** after printing the last row.

***

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

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

	for (j = 1; j < height - 1; j++) // Loop through each middle row
	{
		for (i = 0; i < width; i++) // Loop through characters in the row
		{
			if (i == 0 || i == width - 1) // Print 'B' for left and right borders
				ft_putchar('B');
			else // Print spaces in the middle
				ft_putchar(' ');
		}
		ft_putchar('\n'); // Move to the next line after finishing the row
	}
}
```

✅ **Explanation:**

* **Left and right borders** use `'B'`, ensuring vertical borders.
* **Middle section** consists of spaces `' '`, keeping the inside empty.
* Runs **only if height > 2**, ensuring enough space for a middle section.

***

#### **5. Printing the Full Rectangle (`rush`)**

```c
// Function to print the entire rectangle
void	rush(int width, int height)
{
	if (width <= 0 || height <= 0) // Handle invalid input (negative or zero values)
		return;

	first_line(width); // Print the first row

	if (height > 1) // Print middle rows only if height is greater than 1
		middle_rows(width, height);

	if (height > 1) // Print the last row only if height is greater than 1
		last_line(width);
}
```

✅ **Explanation:**

* **Handles edge cases** (if `width` or `height` is 0 or negative, it does nothing).
* Calls `first_line()`, `middle_rows()`, and `last_line()` **in sequence** to generate the full rectangle.
* The **middle rows and last row are printed only if `height > 1`**.

***

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

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

✅ **Explanation:**

* Calls `rush(5,3)`, which prints a rectangle **5 characters wide and 3 characters high**.
* You can change values to test different rectangle sizes.

***

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

| Function Name   | Responsibility                                                            |
| --------------- | ------------------------------------------------------------------------- |
| `ft_putchar()`  | Prints a single character                                                 |
| `first_line()`  | Prints the first row with 'A' at start, 'C' at end, and 'B' in the middle |
| `last_line()`   | Prints the last row (identical to `first_line()`)                         |
| `middle_rows()` | Prints the middle rows with 'B' on edges and spaces inside                |
| `rush()`        | Calls all the row functions in order and checks for invalid input         |
| `main()`        | Starts the program and calls `rush()` with test dimensions                |

***

### **Example Outputs**

#### **Example 1: `rush(5, 3)`**

```
ABBBC
B   B
ABBBC
```

#### **Example 2: `rush(1, 1)`**

```
A
```

#### **Example 3: `rush(4, 4)`**

```
ABBC
B  B
B  B
ABBC
```


---

# 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/03.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.
