Introduction

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.

Prerequisites

Knowledge of C Program

Example with Source Code

Method One

We will perform addition of two matrices in the same function and we will not return the resultant matrix from the 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

We will perform addition of two matrices and we will return the resultant matrix from the function.

#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;
}

Testing the Program

When any of the the above program is run then you will see below 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

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

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