42 Piscine/42 Piscine: Preparation Day 3/Array Functions

# Array Functions

This program introduces a function, `printArray`, which takes an integer array and its size as arguments and prints the array elements. The `main` function exemplifies the usage of this function to print the elements of the original array. It serves as a C program that defines a function (`printArray`) for printing integer array elements. The `main` function showcases the utilization of this function for displaying the original array elements and can be extended for array modifications or additional operations as required.

```
#include <stdio.h>

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int length = sizeof(numbers) / sizeof(numbers[0]);

    printf("Original Array: ");
    printArray(numbers, length);

    // You can modify the array in the main function or call other functions to do so.

    printf("Modified Array: ");
    printArray(numbers, length);

    return 0;
}

```

## Code Breakdown

### #include <stdio.h>

* This line includes the standard input-output library (`stdio.h`), which provides functions for reading input and displaying output. It's essential to use functions like `printf`.

### void printArray(int arr[], int size) {

* This line defines a function named `printArray`. The function takes two arguments: an integer array `arr` and an integer `size` representing the size of the array. The function's purpose is to print the elements of the array.

### for (int i = 0; i < size; i++) {

* This line initiates a `for` loop. The loop variable `i` is initialized to `0`, and the loop continues as long as `i` is less than the `size` of the array. This loop iterates through the array to access and print each element.

### printf("%d ", arr[i]);

* Within the `for` loop, this line uses the `printf` function to print the value of the element at index `i` of the array. The `%d` format specifier is used to print an integer. The elements are printed with spaces in between.

### printf("\n");

* After printing all the elements, this line adds a newline character (`\n`) to move the cursor to the next line in the console, creating a line break.

### int main() {

* This line is the beginning of the `main` function, which is the entry point of a C program. It returns an integer (`int`) and takes no arguments in this case.

### int numbers[] = {1, 2, 3, 4, 5};

* Here, an integer array named `numbers` is declared and initialized with values. This array contains five integers: 1, 2, 3, 4, and 5.

### int length = sizeof(numbers) / sizeof(numbers[0]);

* This line calculates the size of the array, `numbers`, by dividing the total size of the array (`sizeof(numbers)`) by the size of a single element (`sizeof(numbers[0]`). This gives you the length of the array, which is `5` in this case.

### printf("Original Array: ");

* This line uses `printf` to display the text "Original Array: " without a newline character, preparing for the array elements to be printed on the same line.

### printArray(numbers, length);

* Here, the `printArray` function is called with `numbers` (the array) and `length` (the size) as arguments. This function prints the original array elements.

### // You can modify the array in the main function or call other functions to do so.

* This is a comment line, indicating that you can modify the array in the `main` function or call other functions to do so. It's a placeholder for demonstrating how to modify the array, but this code example doesn't include such modifications.

### printf("Modified Array: ");

* This line uses `printf` to display the text "Modified Array: " without a newline character, preparing for the array elements to be printed on the same line.

### printArray(numbers, length);

* Similar to the previous call, this line invokes the `printArray` function to print the array elements. The array remains unmodified in this example.

### return 0;

* The `return` statement is used to indicate that the `main` function is finished executing. It also returns an exit status of `0`, which typically indicates that the program terminated successfully.