/*
   Defines an array. See Nyhoff P457-460

   Note arrays in C/C++ are always 0,..,N-1

   Issue: There are no checks that you do not read / write to array elements
   that are not allocated. This creats bugs that can be very difficult to find.

   Note the memory for the array elements are allocated by the compiler when
   the code is compiled. Hence you can not make an array with a dynamic size
   that is first determined when the code is run. (dynamic arrays require
   that we should also handling memory allocation, which is another topic).   

*/

#include <iostream>
using namespace std;

int main()
{
   const int DIM=3;

   double x[DIM];                        // Declare an array of 3 items.
   x[0]=1.0; x[1]=2.0; x[2]=3.0;         // Assign values to array elements

   cout << "x=" << x[0] << " "
                << x[1] << " "
                << x[2] << " "
                << endl;

   double y[DIM]={42.0};                 // Declare and initialize.
   cout << "y=" << y[0] << " "
                << y[1] << " "
                << y[2] << " "
                << endl;


   int z[5]={2,3,5};                     // Declare and initialize.

   cout << "z=" << z[0] << " "
                << z[1] << " "
                << z[2] << " "
                << z[3] << " "
                << z[4] << " "
                << endl;

// Very typical bugs, reading or writing past the end of an array!

   for (int i=0;i<10;i++)
        cout << "z["<<i<<"]=" << z[i] << endl;   // BAD!!

   for (int i=0;i<100;i++)  x[i]=42;             // VERY BAD!!

   return 0;
}

