Simple Array Echo
/* * Read some integers into an array, then echo them. */ #include <iostream> using namespace std; // It's usual to give a name to array sizes. const int ARRMAX = 100; int main() { // This creates a native array. int arr[ARRMAX]; int sub = 0; cout << "Enter some integers: " << endl; while(cin >> arr[sub]) { ++sub; } int num = sub; /* Print them back out again. */ cout << "===============" << endl; for(int sub = 0; sub < num; ++sub) { cout << arr[sub] << " "; } cout << endl; }

This program does the same thing as the first array container example, but using native arrays. Most of the code is the same, but for the declaration.

A native array is declared as shown here. It is similar to Java, but we don't have to new, and we give the size in the brackets following the name. The size must be a constant. Here we use a named constant, which is good practice, but the language does not require it.

Legal subscripts have not changed: from zero to the array size less one.