Course List http://www.c-jump.com/bcc/
Topics include:
Array Navigation
Array Initialization
Arrays and STL Algorithms
Multidimensional Arrays
typename a[ size ]; // array declaration typename a[ size ] = { V1, V2, ..., Vn }; // initializer a[ sub ] = x; // element access x = a[ sub ]; sizeof( a ); // array size in bytes sizeof( a )/sizeof( typename ); // array size
To create an array of ten integers, declare it like this:
int a[ 10 ];
C++ array has the following properties:
number of elements is constant
sizeof( a ) returns size of array a in bytes.
sizeof( a ) / sizeof( int ) returns the number of elements in the array.
What do we know about elements of the array ? For example,
int a[ 10 ];
C++ array is zero-based: its elements are indexed from zero to size - 1
a[ 0 ] could be pronounced as
element sub zero
first element of a
zeroeth element of a
initial element of a
#include <iostream> using namespace std; int main() { const int SIZE = 5; char carr[ SIZE ]; // array of five characters for ( int idx = 0; idx < SIZE; ++idx ) { carr[ idx ] = 'A' + idx; // change element value cout << carr[ idx ]; // display element value } return 0; }
Array initializer provides a list of array values:
char carr[ ] = { 'a', 'b', 'c', 0 }; // ok: size is calculated int iarr[ 2 ] = { 5 }; // ok: same as { 5, 0 } float fa[ 3 ] = { x, y, z }; // variable initializers ok int ibad[ 2 ] = { 1, 2, 3 }; // error: too many initializers ibad[ 2 ] = { 10, 20 }; // error: no initializer-assignments
Arrays can be manipulated by STL algoritms:
#include <algorithm>
STL internet resources:
#include <iostream> #include <algorithm> using namespace std; int main() { const int SIZE = 5; int dummy[ SIZE ] = { 5, 10, 5, 5, 10 }; int cnt = std::count( dummy, dummy + SIZE, 5 ); std::cout << "Array has " << cnt << " values that are equal to 5." << '\n' ; return 0; } /*Program output: Array has 3 values that are equal to 5. */
array name is the address of the zeroth element
array name is a constant address
a[ 1 ] is equivalent to ( a + 1 )[ 0 ]
a[ n ] is equivalent to *( a + n )
Half-open address range concept:
int a[ 10 ] = { 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 };
a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | a[6] | a[7] | a[8] | a[9] | |
15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | ??? |
(a + 10) is the address one-beyond the end of an array.
#include <iostream> #include <algorithm> // STL algoritms #include <iterator> // for std::ostream_iterator int main() { const int SIZE = 5; int iarr[ SIZE ] = { 1, 2, 3 }; int dummy[ SIZE ] = { 0 }; std::copy( // Copy elements from iarr to dummy iarr, iarr + SIZE, dummy ); std::copy( // Display elements of dummy array dummy, dummy + SIZE, std::ostream_iterator< int >( std::cout, "\t" ) ); return 0; } /* Program output: 1 2 3 0 0 */
|
|
The program continuously prints random numbers between 0 and 9:
6 8 5 5 0 1 5 1 4 3 5 9 6 2 7 6 3 1 9 1 4 8 6 9 6 1 9 4 3 3 1 0 4 7 3 1 9 3 7 8 7 4 6 2 7 6 9 0 7 5 8 1 3 1 4 3 7 4 4 1 6 2 3 4 5 3 5 8 7 1 6 8 2 6 4 4 2
#include <iostream> #include <cstdlib> // for rand() and srand() #include <ctime> #include <algorithm> #include <iterator> // for std::ostream_iterator // frand returns pseudo-random numbers 0 to 9: int frand( int ) { return rand() % 10; } int main() { srand( (unsigned)time( 0 ) ); const int SIZE = 5; int dummy[ SIZE ]; // Populate array dummy with random numbers: std::transform( dummy, dummy + SIZE, dummy, frand ); // Display elements of dummy: std::copy( dummy, dummy + SIZE, std::ostream_iterator< int >( std::cout, "\t" ) ); return 0; } /* Program output: 5 8 7 9 7 */
#include <iostream> #include <algorithm> int main() { int dummy[] = { 1, 2, 3, 4, 5 }; // Swap elements: std::swap( dummy[ 0 ], dummy[ 4 ] ); // Display elements of dummy array: std::cout << dummy[ 0 ] << '\t' << dummy[ 4 ] << '\n' ; return 0; } /* Program output: 5 1 */
|
|
Two-dimensional array with three rows and four columns,
int a[3][4];
can be illustrated as follows:
 
|
|
Multidimensional arrays are arrays of arrays:
int good[3][4]; // three arrays of arrays of four integers int bad[3,4]; // bad syntax, compile-time error
Two-dimensional array is really a one-dimensional array, each of whose elements is an array.
The subscripts are written as
x = a[ i ][ j ]; // correct: a[row][column]
rather than
x = a[ i, j ]; // compile-time error
 
If the array is to be passed to a function f, the declaration of f would look like:
void f( int daytab[2][13] );
or it could also be written as
void f( int daytab[ ][13] );