C Program to Add Two Matrix Using Multi-dimensional Arrays

This example will show you how to add two matrices using two dimensional array in C program. In this example a user will be asked to enter the number of rows and columns for two matrices. Then user will be asked to enter the elements at each index of the matrices.

Here we will see also how to use pointers to allocate memory dynamically for array using malloc function.

Method One – in the same function

#include <stdio.h> 
#include <stdlib.h>

int main() {

    int i, j, rows, cols, **a, **b, **result;

    printf("Addition of two Matrix");

    printf("\nEnter number of rows :");
    scanf("%d", &rows);
    printf("\nEnter number of columns :");
    scanf("%d", &cols);

    // allocate rows, each row is a pointer to int
    a = malloc(rows * sizeof (int *));
    b = malloc(rows * sizeof (int *));
    result = malloc(rows * sizeof (int *));

    // for each row allocate cols int
    for (i = 0; i < rows; i++) {
        a[i] = malloc(cols * sizeof (int));
        b[i] = malloc(cols * sizeof (int));
        result[row] = malloc(cols * sizeof (int));
    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("Enter Element %d %d : ", i, j);
            scanf("%d", &a[i][j]);
        }
    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("Enter Element %d %d : ", i, j);
            scanf("%d", &b[i][j]);
        }
    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < rows; j++) {
            result[i][j] = a[i][j] + b[i][j];
        }
    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("\nElement at %d %d : %d", i, j, result[i][j]);
        }
    }

    return 0;
}

Method Two – function returning an array

#include <stdio.h>
#include <stdlib.h>

int main() {

    int i, j, rows, cols, **a, **b;

    printf("Addition of two Matrix");

    printf("\nEnter number of rows :");
    scanf("%d", &rows);
    printf("\nEnter number of columns :");
    scanf("%d", &cols);

    // allocate rows, each row is a pointer to int
    a = malloc(rows * sizeof (int *));
    b = malloc(rows * sizeof (int *));

    // for each row allocate cols int
    for (i = 0; i < rows; i++) {
        a[i] = malloc(cols * sizeof (int));
        b[i] = malloc(cols * sizeof (int));
    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("Enter Element %d %d : ", i, j);
            scanf("%d", &a[i][j]);
        }
    }

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("Enter Element %d %d : ", i, j);
            scanf("%d", &b[i][j]);
        }
    }

    result = matrixAdd(a, b, rows, cols);

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("\nElement at %d %d : %d", i, j, result[i][j]);
        }
    }

    return 0;
}

int ** matrixAdd(int **a, int **b, int r, int c) {
    int i, j;
    int **result = malloc(r * sizeof (int *));

    for (i = 0; i < r; i++) {
        result[i] = malloc(c * sizeof (int));
    }

    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            result[i][j] = a[i][j] + b[i][j];
        }
    }

    return result;
}

Output

Addition of two Matrix
Enter number of rows :2

Enter number of columns :2
Enter Element 0 0 : 1
Enter Element 0 1 : 2
Enter Element 1 0 : 3
Enter Element 1 1 : 4
Enter Element 0 0 : 1
Enter Element 0 1 : 2
Enter Element 1 0 : 3
Enter Element 1 1 : 4

Element at 0 0 : 2
Element at 0 1 : 4
Element at 1 0 : 6
Element at 1 1 : 8

Thanks for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials Twitter Facebook  Google Plus Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *