Lecture Notes for Introduction to Computer Science II

14 June 2001 - Arrays


  1. what are arrays good for

    1. representing a sequence of similar entities

    2. simple access requirements - sequential, not value-based

    3. may not be efficient for large collections

  2. what are arrays

    1. array, index, subscript, element, size

    2. the indexing operator []

  3. c++ arrays

    1. arrays represent a fixed sequence of values - a problem

    2. the size of the array

    3. indexes start a zero and go up by one

    4. the size-index difference

  4. array declarations

    1. an array declaration determines

      1. the array name

      2. the element type

      3. the array size

    2. arrays don't have special names

    3. the element type may be anything, even other arrays

    4. the array size

      1. array sizes must be fixed

        1. int size = 10; int ia[size]; fails

        2. many compilers accept it anyway

      2. use the const keyword to make a value constant

        1. const int size = 10; int ia[size]; works

        2. using const improves documentation by highlighting unarying values

        3. using const may improve execution time performance

        4. why not just numbers - they're magic numbers; no documentation; hard to change; without meaning

    5. initialization

      1. new arrays contain garbage - except global arrays

      2. initializers can give new arrays meaningful values

      3. too few initializers are ok - zeros fill in the rest

      4. too many initializers are an error

      5. computing the size from the initializer list


This page last modified on 14 June 2001.