Introduction to C / C++ Programming
Arrays

Overview

Declaring Arrays

Initializing Arrays

Using Arrays

Sample Programs Using 1-D Arrays

Multidimensional Arrays

Sample Program Using 2-D Arrays

        /* Sample program Using 2-D Arrays */
        
        #include <iostream> 
        using namespace std;
        
        int main( void ) {
        
            /* Program to add two multidimensional arrays */
            /* Written May 1995 by George P. Burdell */
            
            int a[ 2 ][ 3 ] = { { 5, 6, 7 }, { 10, 20, 30 } };
            int b[ 2 ][ 3 ] = { { 1, 2, 3 }, {  3,  2,  1 } };
            int sum[ 2 ][ 3 ], row, column;
            
            /* First the addition */
            
            for( row = 0; row < 2; row++ )
                for( column = 0; column < 3; column++ )
                    sum[ row ][ column ] =  
                        a[ row ][ column ] + b[ row ][ column ];
            
            /* Then print the results */
            
            cout <<  "The sum is: \n\n" ;
            
            for( row = 0; row < 2; row++ ) {
                for( column = 0; column < 3; column++ )
                    cout << "\t" <<  sum[ row ][ column ];
                cout << endl;   /* at end of each row */
            }
            
            return 0;
        }

Exercises

Passing Arrays to Functions

Exercises

Write the following functions:

Character Strings as Arrays of Characters