#include <iostream>
using namespace std;
int main ()
{
const int MAXST = 5 ; // Use whenever the size of an array is important
int i ;
// Declaring and initializing an array : data type , name , size
// Initialization is optional
double store[MAXST] = { 1.2 , 4.5e+1 , 7.9 , -0.2 , 1100. } ;
int help[3] = { 4 , 2 } ; // Partial initialization
char name[6] ; // No initialization, 5 char long C-string
char month[] = "June" ; // month[] will be 6 bytes long !!!
int friday = 17 ;
cout << month[0] << endl ;
cout << month << endl ;
for ( i=0;i<=MAXST;i++ ) // Index of array starts with 0 (zero)
// Error : Should be i < MAXST, but error
// demonstrates how careful the programmer
// has to be.
{
cout << "store[" << i << "]= " << store[i] << endl ;
}
for ( i=0;i<3;i++ )
{ // Observe that help[2] has not been given a value as of yet.
// Hence, you never know what value it has.
cout << "help[" << i << "]= " << help[i] << endl ;
}
// Different ways to access array elements. Basically, whenever
// you were using a simple variable in the past you could use
// and array element. The index of an array element can be
// either an integer value, an integer variable or a mathematical
// expression resulting in an integer value.
help[2] = friday ; // help[2] carries now the value of friday = 17
friday = help[0] ; // now friday has the value of help[0]=4
i = 0 ;
help[i+1] = ( help[2] + help[0] ) / 2 ; // help[1] = .....
}
Zig Herzog; hgn@psu.edu