Multidimensional Numeric Arrays

A two dimensional array is a matrix; it can be visualized as a spreadsheet table, with the first index giving the row, and the second index giving the column.

#include <stdio.h>

int main(void){

    int array[2][3] = {
            /* row one */{42, 47, 73},
            /* row two */{7, 11, 3}
            };

    int row, column;

    for(row = 0; row < 2; row++){
        for(column = 0; column < 2; column++){
            printf("array[%d][%d] = %d \t", row, column, array[row][column]);
        }
        printf("\n");
    }
    

    return 0;
    
}

The indexes [2][3] define the array as a two-dimensional array with two rows consisting of three columns each. Note that for each dimension of the array we must include a set of brackets. We use nested for loops to iterate through multidimensional arrays. Remember here that each index starts at 0, not 1.

Our next program will sum each row in an array.

#include <stdio.h>

int main(void){


    int array[3][3] = {
        /* row 1 */ {128, 256, 512},
        /* row 2 */ {161, 389, 443},
        /* row 3 */ {80, 25, 118}
    }; //note the semicolon
    
    //traditional to use i, j
    //rather than row, column
    int i, j, total;
    for(i = 0; i < 3; i++){
        for(j = 0, total = 0; j < 3; j++){
            total += array[i][j];
            printf("%d ", array[i][j]);
        }
        printf(" \t total = %d\n", total);
    }

    return 0;

}

For our next program, we will pass the array to a function. Because an array of more than one dimension will be used, we must specify the dimensions of the array in the formal parameter list, in order for the compiler to properly set the array pointer for each of the rows.

#include <stdio.h>

int addColumn(int array[][3], int column, int numRows);

int main(void){


    int array[4][3] = {
        {25, 118, 123},
        {143, 161, 389},
        {80, 443, 22},
        {53, 88, 546}
    };

    int i, j;

    //go down each column
    //rather than across each row
    for(i = 0; i < 3; i++){
        printf("Column %d: \t", i+1);
        for(j = 0; j < 4; j++){
            printf("%d ", array[j][i]);
        }
        printf(" total = %d\n", addColumn(array, i, 4));
    }

    return 0;

}


int addColumn(int array[][3], int column, int numRows){
    int r = 0;
    for(int i = 0; i < numRows; i++){
        r += array[i][column];
    }
    return r;
}

Matrix multiplication is another useful thing we can do with two dimensional arrays. Matrix multiplication works by multiplying the columns of one matrix by the number of rows in the other. The general rule is that the number of columns in the first matrix must be equal to the number of rows in the second matrix; but, in order to simplify things, we will only be multiplying square matrices, where the number of columns and rows are equal.

#include <stdio.h>

void printMatrix(int array[3][3], int bounds);
void multiplyMatrix(int arrayX[3][3], int arrayY [3][3], int result[3][3], int bounds);

int main(void){
    
    int array1[3][3] = {
        {1, 2, 3},
        {2, 3, 4},
        {4, 5, 6}
    };

    int array2[3][3] = {
        {1, 3, 6},
        {3, 1, 6},
        {6, 3, 1},
    };

    int result[3][3];

    printf("Matrix one: \n");
    printMatrix(array1, 3);
    
    printf("\n\nMatrix two: \n");
    printMatrix(array2, 3);

    multiplyMatrix(array1, array2, result, 3);

    printf("\n results:\n");
    printMatrix(result, 3);

    return 0;


}


void printMatrix(int array[3][3], int bounds){
    int i, j;
    for(i = 0; i < bounds; i++){
        for(j = 0; j < bounds; j++){
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
}


void multiplyMatrix(int arrayX[3][3], int arrayY [3][3], int result[3][3], int bounds){
    int row, column, row2, column2;

    for(row = 0; row < bounds; row++){
        for(column = 0; column < bounds; column++){
            result[row][column] = 0;
            printf("Multiplying row %d of arrayX by column %d of arrayY\n", row, column);
            for(row2 = 0, column2 = 0; column2 < bounds; column2++, row2++){
                result[row][column] += arrayX[row][column2] * arrayY[row2][column];
            }
        }
    }

    
}

Take a look at my author’s page at: http://www.amazon.com/Big-Als-PHP-Al-Jensen-ebook/dp/B0084AKY18/

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s