Lecture Notes for Introduction to Computer Science II
14 June 2001 - Arrays
- what are arrays good for
- representing a sequence of similar entities
- simple access requirements - sequential, not value-based
- may not be efficient for large collections
- what are arrays
- array, index, subscript, element, size
- the indexing operator
[]
- c++ arrays
- arrays represent a fixed sequence of values - a problem
- the size of the array
- indexes start a zero and go up by one
- the size-index difference
- array declarations
- an array declaration determines
- the array name
- the element type
- the array size
- arrays don't have special names
- the element type may be anything, even other arrays
- the array size
- array sizes must be fixed
-
int size = 10; int ia[size];
fails
- many compilers accept it anyway
- use the
const
keyword to make a value constant
-
const int size = 10; int ia[size];
works
- using
const
improves documentation by highlighting unarying
values
- using
const
may improve execution time performance
- why not just numbers - they're magic numbers; no documentation;
hard to change; without meaning
- initialization
- new arrays contain garbage - except global arrays
- initializers can give new arrays meaningful values
- too few initializers are ok - zeros fill in the rest
- too many initializers are an error
- computing the size from the initializer list
This page last modified on 14 June 2001.