2. ï Arrays
ï Structures of related data items
ï Static entity (same size throughout program)
ï A few types
ï Pointer-based arrays (C-like)
ï Arrays as objects (C++)
2
3. ï Array
ï Consecutive group of memory locations
ï Same name and type (int, char, etc.)
ï To refer to an element
ï Specify array name and position number (index)
ï Format: arrayname[ position number ]
ï First element at position 0
ï N-element array c
c[ 0 ], c[ 1 ] ⊠c[ n - 1 ]
ï Nth element as position N-1
3
4. ï Array elements like other variables
ï Assignment, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
ï Can perform operations inside subscript
c[ 5 â 2 ] same as c[3]
4
6. ï When declaring arrays, specify
ï Name
ï Type of array
ï Any data type
ï Number of elements
ï type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
ï Declaring multiple arrays of same type
ï Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
6
7. ï Initializing arrays
ï For loop
ï Set each element
ï Initializer list
ï Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
ï If not enough initializers, rightmost elements 0
ï If too many syntax error
ï To set every element to same value
int n[ 5 ] = { 0 };
ï If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
ï 5 initializers, therefore 5 element array
7