C++ Code Example: passing array to function

The program you posted defines a function doubles that takes an integer array and its length as input and doubles each element of the array. In the main function, the doubles function is called with a test array myArray and its length, which is calculated using the sizeof operator. The resulting array is then displayed on the console.

#include <iostream>
using namespace std;

void doubles(int array[], int length) {
    for (int i = 0; i < length; i++) {
        array[i] = 2 * array[i];
    }
}

int main() {
    int length, myArray[] = {6, 12, 30, 14, 66, 6, 7};

    length = sizeof myArray / sizeof myArray[0];
    doubles(myArray, length);

    cout << "values from array doubled: " << endl;
    for (int i = 0; i < length; i++) {
        cout << myArray[i] << " ";
    }

    return 0;
}
Output
values from array doubled: 
12 24 60 28 132 12 14