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

# ft\_rev\_int\_tab

**Objective:**

Create a function that reverses the contents of an array of integers (e.g., the first element becomes the last, the second becomes the second to last, etc.).

**Turn-in Directory:** `ex07/`

**Files to Turn In:** `ft_rev_int_tab.c`

**Allowed Functions:** None

**Prototype:**

```c
void ft_rev_int_tab(int *tab, int size);
```

***

#### **Detailed Explanation:**

**1. Functionality:**

* The `ft_rev_int_tab` function takes an array of integers and its size as input and reverses the order of the elements within the array **in place**.
* This means it modifies the original array without creating a new one.

**2. Input Parameters:**

* `int *tab`:
  * A pointer to the first element of the array.
* `int size`:
  * The number of integers in the array.

**3. Output:**

* No return value (`void` function).
* The array passed as input is modified directly.

**4. Implementation Steps:**

1. Use two variables to represent indices:
   * One pointing to the beginning of the array (`start`).
   * One pointing to the end of the array (`end`).
2. Swap the values at these indices.
3. Increment the `start` index and decrement the `end` index.
4. Continue swapping until the `start` index meets or surpasses the `end` index.

***

#### **Key Points to Note:**

* The process involves **swapping elements** symmetrically from both ends.
* You need a temporary variable to perform the swap.
* This algorithm has a time complexity of **O(n/2)**, where `n` is the size of the array.

***

#### **Code Implementation:**

Here’s the implementation for `ft_rev_int_tab`:

```c
void ft_rev_int_tab(int *tab, int size)
{
    int start = 0;
    int end = size - 1;
    int temp;

    while (start < end) // Continue until start index is less than end index
    {
        // Swap the values at the start and end indices
        temp = tab[start];
        tab[start] = tab[end];
        tab[end] = temp;

        // Move towards the center of the array
        start++;
        end--;
    }
}
```

***

#### **Example Usage:**

**Code Example:**

```c
#include <stdio.h>

void ft_rev_int_tab(int *tab, int size);

int main(void)
{
    int array[] = {1, 2, 3, 4, 5};
    int size = 5;

    printf("Original array: ");
    for (int i = 0; i < size; i++)
        printf("%d ", array[i]);

    ft_rev_int_tab(array, size);

    printf("\nReversed array: ");
    for (int i = 0; i < size; i++)
        printf("%d ", array[i]);

    return 0;
}
```

**Expected Output:**

```c
Original array: 1 2 3 4 5 
Reversed array: 5 4 3 2 1
```

***

#### **Edge Cases to Test:**

1. **Empty Array:**
   * Input: `[]`
   * Expected Output: No changes; the function does nothing.
2. **Single Element Array:**
   * Input: `[42]`
   * Expected Output: `[42]` (No change since there’s nothing to reverse).
3. **Even Number of Elements:**
   * Input: `[1, 2, 3, 4]`
   * Expected Output: `[4, 3, 2, 1]`
4. **Odd Number of Elements:**
   * Input: `[1, 2, 3, 4, 5]`
   * Expected Output: `[5, 4, 3, 2, 1]`
5. **Array with Negative Numbers:**
   * Input: `[-1, -2, -3, -4, -5]`
   * Expected Output: `[-5, -4, -3, -2, -1]`
6. **Array with Repeated Numbers:**
   * Input: `[1, 2, 2, 3, 3]`
   * Expected Output: `[3, 3, 2, 2, 1]`

***

#### **Explanation of the Swapping Mechanism:**

The `while` loop ensures elements are swapped symmetrically from both ends. Here's a step-by-step example:

**Input:**

`tab = [1, 2, 3, 4, 5]`

* `start = 0`, `end = 4`

**Step 1: First Swap**

* Swap `tab[0]` and `tab[4]`.
* Result: `[5, 2, 3, 4, 1]`
* Increment `start` to 1 and decrement `end` to 3.

**Step 2: Second Swap**

* Swap `tab[1]` and `tab[3]`.
* Result: `[5, 4, 3, 2, 1]`
* Increment `start` to 2 and decrement `end` to 2.

**Step 3: Termination**

* Since `start` is now equal to `end`, the loop stops. The array is fully reversed.


---

# 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/c01/ft_rev_int_tab.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.
