Arrays

Introduction

  • An array holds a collection of objects of some type.
  • Once an array is created, new elements cannot be added.
  • The size of the array must be a constant
  • Arrays are data structures that are similar to library vectors but are built into the language.
  • Like a vector, an array is a container of objects of a single data type. The individual objects are not named; rather, each one is accessed by its position in the array.
  • An array is a compound type that consists of a type specifier, an identifier, and a dimension.

Examples

** Example**: Constant size of array

int anArray1[15]; // ok - 15 is a literal constant
 
#define ARRAY_SIZE 5
int anArray2[ARRAY_SIZE]; // ok - ARRAY_SIZE is a symbolic constant
 
enum ArrayElements
{
  MAX_ARRAY_SIZE = 5;
}
int anArray3[MAX_ARRAY_SIZE]; // ok - MAX_ARRAY_SIZE is an enum constant
 
int nSize = 5;
int anArray4[nSize]; //WRONG - nSize is not a constant

Example: Initializing an array

// to initialize all the elements of an array to 0
int anArray[5] = {0};
 
// both buf_size and max_files are const
          const unsigned buf_size = 512, max_files = 20;
          int staff_size = 27;            // nonconst
          const unsigned sz = get_size();  // const value not known until run time
          char input_buffer[buf_size];     // ok: const variable
          string fileTable[max_files + 1]; // ok: constant expression
          double salaries[staff_size];     // error: non const variable
          int test_scores[get_size()];     // error: non const expression
          int vals[sz];                    // error: size not known until run time

Example: Array elements may be accessed using the subscript operator.

int main()
          {
              const size_t array_size = 10;
              int ia[array_size]; // 10 ints, elements are uninitialized
 
              // loop through array, assigning value of its index to each element
              for (size_t ix = 0; ix != array_size; ++ix)
                    ia[ix] = ix;
              return 0;
          }

Example: Using a similar loop, we can copy one array into another.

int main()
          {
              const size_t array_size = 10;
              int ia[array_size]; // 10 ints, elements are uninitialized
 
              // loop through array, assigning value of its index to each element
              for (size_t ix = 0; ix != array_size; ++ix)
                    ia[ix] = ix;
              return 0;
          }

Example: How many elements an array contains

int nElements = sizeof(anArray) / sizeof(anArray[0]);

Example: One problem with arrays is that the integer indices do not provide any information to the programmer about the meaning of the variable.

const int nNumberOfStudents = 5;
int aTestScores[nNumberOfStudents];
aTestScores[2] = 76; // not clear who is represented by array element 2
 
// It is common to use enumerated values to index the array
enum StudentNames
{
    KENNY, // 0
    KYLE, // 1
    STAN, // 2
    BUTTERS, // 3
    CARTMAN, // 4
    MAX_STUDENTS // 5
};
 
int atESTsCORES[MAX_STUDENTS]; // allocate 5 integers
aTestScores[STAN] = 76;

Multidimensional Arrays

To initialize a two-dimensional array, it is easiest to use nested braces.

int anArray[3][5] =
{
{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2
};

Two-dimensional arrays with initializer lists can omit only the first size specification:

int anArray[][5] =
{
{ 1, 2, 3, 4, 5, },
{ 6, 7, 8, 9, 10, },
{ 11, 12, 13, 14, 15 }
};

Example: Multiplication table with two-dimensional array

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.