C++ Code Example: length of an array

The length of an array is output here. There are several ways to determine the length of an array.

First Option

#include <iostream>
using namespace std;

int main () {
    int numbers[] = {2, 6, 4, 8, 1};
    int arrayLength = sizeof numbers / sizeof numbers[0];

    for(int i = 0; i < arrayLength; i++) {
        cout << numbers[i] << endl;
    }
}
Output
2
6
4
8
1

Second Option

#include &lt;iostream&gt;
using namespace std;

int main () {
    int numbers[] = {2, 6, 4, 8, 1};
    int arrayLength = end(numbers) - begin(numbers);

    cout << "Length of array: " << arrayLength << endl;

    for(int i = 0; i < arrayLength; i++) {
        cout << numbers[i] << endl;
    }
}
Output
Length of array: 5
2
6
4
8
1