Learn C++ - C++ Array






An array is a data form that can hold several values, all of one type.

To create an array, you use a declaration statement.

For example, the following declaration creates an array named months that has 12 elements, each of which can hold a type short value:

short months[12];     // creates array of 12 short 

Each element, in essence, is a variable that you can treat as a simple variable.

This is the general form for declaring an array:

typeName arrayName [arraySize]; 

The expression arraySize, which is the number of elements, must be an integer constant.

Example

The following code demonstrates a few properties of arrays, including declaring an array, assigning values to array elements, and initializing an array.


#include <iostream> 
using namespace std; 
int main() /*  w w  w  . j a  v  a  2s.co m*/
{ 
     int my_array[3];    // creates array with three elements 
     my_array[0] = 7;    // assign value to first element 
     my_array[1] = 8; 
     my_array[2] = 6; 

     int my_value[3] = {20, 30, 5}; // create, initialize array 
     cout << my_array[0] + my_array[1] + my_array[2] << endl; 
     cout << my_array[1] << " my_array costs "; 
     cout << my_value[1] << ".\n"; 
     int total = my_array[0] * my_value[0] + my_array[1] * my_value[1]; 
     total = total + my_array[2] * my_value[2]; 
     cout << total << " cents.\n"; 

     cout << "\nSize of my_array array = " << sizeof my_array; 
     cout << " bytes.\n"; 
     cout << "Size of one element = " << sizeof my_array[0]; 
     cout << " bytes.\n"; 
     return 0; 
} 

The code above generates the following result.





Initialization Rules for Arrays

You can use the initialization form only when defining the array.

int cards[4] = {3, 6, 8, 10};       // okay 
int hand[4];                        // okay 

When initializing an array, you can provide fewer values than array elements.

float val[5] = {5.0, 2.5};

If you partially initialize an array, the compiler sets the remaining elements to zero.

It's easy to initialize all the elements of an array to zero-just explicitly initialize the first element to zero:

long totals[500] = {0};

If you leave the square brackets [] empty when you initialize an array, the C++ compiler counts the elements for you.

Suppose, for example, that you make this declaration:

short things[] = {1, 5, 3, 8};

The compiler makes things an array of four elements.

Second, you can use empty braces to set all the elements to 0:

unsigned int counts[10] = {};  // all elements set to 0 
float balances[100] {};        // all elements set to 0 

Example: Initializing an array's elements to zeros and printing the array.


#include <iostream> 
#include <iomanip> 
using namespace std; 
/* w  ww .ja  v  a  2  s. com*/
int main() { 
    int n[ 10 ]; // n is an array of 10 integers 

    // initialize elements of array n to 0 
    for ( int i = 0; i < 10; ++i ) 
       n[ i ] = 0; // set element at location i to 0 

    cout << "Element" << setw( 13 ) << "Value" << endl; 

    // output each array element's value 
    for ( int j = 0; j < 10; ++j ) 
       cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl; 
}

The code above generates the following result.





Initializing an array in a declaration


#include <iostream> 
#include <iomanip> 
using namespace std; 
//  ww w .j a  v  a 2 s .c  om
int main() { 
    // use initializer list to initialize array n 
    int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 

    cout << "Element" << setw( 13 ) << "Value" << endl; 

    // output each array element's value 
    for ( int i = 0; i < 10; ++i ) 
        cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 
}

The code above generates the following result.

Two-Dimensional Array

For a two-dimensional array, each element is itself an array.

Thus, the initialization consists of a comma-separated series of one-dimensional initializations, all enclosed in a set of braces:

int maxtemps[4][5] =   // 2-D array 
{ 
   {96, 100, 87, 101, 105},   // values for maxtemps[0] 
   {96, 98, 91, 107, 104},    // values for maxtemps[1] 
   {97, 101, 93, 108, 107},   // values for maxtemps[2] 
   {98, 103, 95, 109, 108}    // values for maxtemps[3] 
}; 

The following code shows how to use nested loops and 2-D array.

#include <iostream> 
const int Cities = 5; 
const int Years = 4; 
int main() 
{ 
     using namespace std; 
     const char * cities[Cities] =   // array of pointers 
     {                               // to 5 strings 
          "A", 
          "AA", 
          "AAA", 
          "AAAA", 
          "AAAAA" 
     }; 

     int maxtemps[Years][Cities] =   // 2-D array 
     { 
          {96, 100, 87, 101, 105},   // values for maxtemps[0] 
          {96, 98, 91, 107, 104},    // values for maxtemps[1] 
          {97, 101, 93, 108, 107},   // values for maxtemps[2] 
          {98, 103, 95, 109, 108}    // values for maxtemps[3] 
     }; 

     cout << "Maximum temperatures for 2008 - 2011\n\n"; 
     for (int city = 0; city < Cities; ++city) 
     { 
         cout << cities[city] << ":\t"; 
         for (int year = 0; year < Years; ++year) 
              cout << maxtemps[year][city] << "\t"; 
         cout << endl; 
     } 
     return 0; 
}