Introduction to C Programming
Arrays

Overview

Declaring Arrays

Initializing Arrays

Designated Initializers:

int numbers[ 100 ] = { 1, 2, 3, [10] = 10, 11, 12, [60] = 50, [42] = 420 };

Using Arrays

Sample Programs Using 1-D Arrays

Multidimensional Arrays

Sample Program Using 2-D Arrays

        /* Sample program Using 2-D Arrays */
        
        #include <stdlib.h>
        #include <stdio.h> 
        
        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 */
            
            printf( "The sum is: \n\n" );
            
            for( row = 0; row < 2; row++ ) {
                for( column = 0; column < 3; column++ )
                    printf( "\t%d",  sum[ row ][ column ] );
                printf( '\n' );   /* at end of each row */
            }
            
            return 0;
        }

Exercises

Material Below This Line Not Covered in CS 107 Until Later


Passing Arrays to Functions ( in C++ )

Exercises

Write the following functions:

Character Strings as Arrays of Characters