C Program for Matrix Addition

Check The Equality of Two Matrices

C programs to add two matrices have been shown here. Two matrices $[A]_{m \times n}$ and $[B]_{p \times q}$ are considered for addition if the number of rows and columns are same in both of the matrices i.e. $m = p$ and $n = q$.

Algorithm, pseudocode and time complexity of the program have also been shown.






1. Algorithm for matrix addition


1. Take two matrice $A_{m\times n}$ and $B_{p\times q}$

2. Check if $m=p$ and $n=q$

3. If step [2] is false then print "Addition not possible" and exit program

4. If step [2] is true, then

5. Add each corresponding elements of $A$ and $B$

6. Print the result and exit program




2. Pseudocode for matrix addition


Input: Two matrices $A_{m\times n}$ and $B_{p\times q}$

Output: $A + B$

1. Procedure matrixAddition($A_{m\times n}$, $B_{p\times q}$):

2. If $m==p$ and $n==q$:

3. Repeat for each $i \in m$ and $j \in n$

4. $C_{ij}\leftarrow A_{ij} + B_{ij}$

5. Return $C$

6. Else:

7. Return Addition Not Pssible

8. End Procedure





3. Time Complexity for matrix addition


Time Complexity: O($mn$)

Where $m$ is the number of rows and $n$ is the number of columns in the matrices.




4. C Program for matrix addition

Code has been copied
/**********************************
        alphabetacoder.com
    C Program for Matrix Addition
***********************************/
#include <stdio.h>

int main() {
    // declare variables
    int m, n, p, q, i, j;
    int A[10][10] = {0}, B[10][10] = {0};

    //take input of the order of first matrix
    printf("Enter the number of row and column of first matrix = ");
    scanf("%d%d", & m, & n);

    //take input of the first matrix
    printf("Enter the first matrix of order %d x %d = \n", m, n);
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            scanf("%d", & A[i][j]);

    //take input of the order of second matrix
    printf("Enter the number of row and column of second matrix = ");
    scanf("%d%d", & p, & q);

    //take input of the first matrix
    printf("Enter the second matrix of order %d x %d = \n", p, q);
    for (i = 0; i < p; i++)
        for (j = 0; j < q; j++)
            scanf("%d", & B[i][j]);

    // check if order of matrices are same
    // if not then addition of two matrices is not possible
    if (m != p || n != q)
        printf("\nMatrices are of different order,hence addition is not possible");
    else {
        //add each corresponding elements
        // and element wise print the result 
        printf("The resultant matrix after addition:\n");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++)
                printf("%d ", A[i][j] + B[i][j]);
            printf("\n");
        }
    }
    return 0;
}

Output


Case 1:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3 =

3 5 7

4 5 9

0 1 1

Enter the number of row and column of second matrix = 3 3

Enter the second matrix of order 3 x 3 =

9 4 5

3 7 8

1 2 3

The resultant matrix after addition:=

12 9 12

7 12 17

1 3 4



Case 2:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3 =

3 5 7

4 5 9

0 1 1

Enter the number of row and column of second matrix = 2 3

Enter the second matrix of order 2 x 3 =

9 4 5

3 7 8


Matrices are of different order,hence addition is not possible





5. C Program for matrix addition using recursion

Code has been copied
/**********************************************
            Alphabetacoder.com
C program for matrix addition using recursion
***********************************************/
#include <stdio.h>

#define MAXSIZE 10

// Declare a recursive function to add two matrices
// This function takes two matices (A and B), Sizes of both matrices (m, n)
// and index of current row & column as  parameters
// Elementwise addition is stored in B
int add_matrices(int A[][MAXSIZE], int B[][MAXSIZE], int m, int n, int row, int col) {

    // check if all columns are traversed
    // if yes the set col = 0 and increment row
    if (col >= n) {
        row++;
        col = 0;
    }

    // check if all rows are traversed
    if (row >= m)
        return 1;

    // Add corresponding elements
    // Store the result in matrix B
    B[row][col] = A[row][col] + B[row][col];
    // return the call of recursive function 
    // to consider the next element
    return add_matrices(A, B, m, n, row, col + 1);
}

int main() {
    // declare variables
    int m, n, p, q, flag = 0, i, j;
    int A[MAXSIZE][MAXSIZE] = {0}, B[MAXSIZE][MAXSIZE] = {0};

    //take input of the order of first matrix
    printf("Enter the number of row and column of first matrix = ");
    scanf("%d%d", & m, & n);

    //take input of the first matrix
    printf("Enter the first matrix of order %d x %d = \n", m, n);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", & A[i][j]);
        }
    }
    //take input of the order of second matrix
    printf("Enter the number of row and column of second matrix = ");
    scanf("%d%d", & p, & q);

    //take input of the first matrix
    printf("Enter the second matrix of order %d x %d = \n", p, q);
    for (i = 0; i < p; i++) {
        for (j = 0; j < q; j++) {
            scanf("%d", & B[i][j]);
        }
    }

    // check if order of matrices are same
    // if same then add two matrices
    if (m != p || n != q) {
        printf("\nMatrices are of different order, hence addition is not possible");
        flag = 1;
    } else {
        // Add two matrices by calling a recursive function
        add_matrices(A, B, m, n, 0, 0);
        // Display the B matrix as result
        printf("The resultant matrix after addition:\n");
        for (i = 0; i < p; i++) {
        	for (j = 0; j < q; j++) {
            	printf("%d ",  B[i][j]);
            }
            // new line
            printf("\n");
        }
    }

    return 0;
}

Output


Case 1:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3 =

1 2 3

4 5 6

3 4 2

Enter the number of row and column of second matrix = 3 3

Enter the second matrix of order 3 x 3 =

9 4 5

3 7 8

1 2 3

The resultant matrix after addition:

10 6 8

7 12 14

4 6 5



Case 2:

Enter the number of row and column of first matrix = 2 2

Enter the first matrix of order 2 x 2 =

5 1

4 3

Enter the number of row and column of second matrix = 2 3

Enter the second matrix of order 2 x 3 =

9 1 6

4 2 2


Matrices are of different order,hence addition is not possible