Simple Array Echo
/* * Read some integers into an array, then echo them. */ #include <iostream> using namespace std; const int ARRMAX = 100; int main() { /* Read them in. */ int arr[ARRMAX]; /* Array of numbers. */ 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; }

Here we simply read in an array and echo the contents. Notice that we declare the array, but don't have to new. The size is a constant, which is required.

The size of the array is given in brackets after the array name (don't put it after the type), and it must be a constant.

Subscripting is the same as in Java, and the legal subscripts are from zero to the array size less one.