C Program to Add Two Matrices Using Multi-dimensional Arrays

Matrix addition is a fundamental operation in linear algebra. This article will guide you through the process of creating a C Program to Add Two Matrices Using Multi-Dimensional Arrays. We will cover three different solutions, each explained with examples and outputs. We will also discuss the prerequisites and conclude with a summary.

Prerequisites

Before starting with the code, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with arrays and loops in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Simple Iterative Approach

1.1 Explanation

In this solution, we will use a nested loop to iterate through the elements of the matrices and add corresponding elements. The result will be stored in a third matrix.

1.2 Program

C
#include <stdio.h>

int main() {
    int rows, cols;
    
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    
    int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
    
    // Input first matrix
    printf("Enter elements of the first matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }
    
    // Input second matrix
    printf("Enter elements of the second matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }
    
    // Add the matrices
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    
    // Print the result
    printf("Sum of the matrices:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

1.3 Output

C
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
1 2
3 4
Enter elements of the second matrix:
5 6
7 8
Sum of the matrices:
6 8
10 12

Solution 2: Using Functions for Modularity

2.1 Explanation

In this solution, we will use functions to modularize the code. We will create separate functions for input, addition, and output of matrices.

2.2 Program

C
#include <stdio.h>

void inputMatrix(int matrix[][10], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void addMatrices(int matrix1[][10], int matrix2[][10], int sum[][10], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
}

void printMatrix(int matrix[][10], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int rows, cols;
    
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    
    int matrix1[10][10], matrix2[10][10], sum[10][10];
    
    // Input first matrix
    printf("Enter elements of the first matrix:\n");
    inputMatrix(matrix1, rows, cols);
    
    // Input second matrix
    printf("Enter elements of the second matrix:\n");
    inputMatrix(matrix2, rows, cols);
    
    // Add the matrices
    addMatrices(matrix1, matrix2, sum, rows, cols);
    
    // Print the result
    printf("Sum of the matrices:\n");
    printMatrix(sum, rows, cols);
    
    return 0;
}

Output

C
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
1 2
3 4
Enter elements of the second matrix:
5 6
7 8
Sum of the matrices:
6 8
10 12

Solution 3: Using Pointers

3.1 Explanation

In this solution, we will use pointers to access and manipulate the matrix elements. This approach provides a more flexible way to handle matrix operations.

3.2 Program

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

void inputMatrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void addMatrices(int **matrix1, int **matrix2, int **sum, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
}

void printMatrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int rows, cols;
    
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    
    int **matrix1 = (int **)malloc(rows * sizeof(int *));
    int **matrix2 = (int **)malloc(rows * sizeof(int *));
    int **sum = (int **)malloc(rows * sizeof(int *));
    
    for (int i = 0; i < rows; i++) {
        matrix1[i] = (int *)malloc(cols * sizeof(int));
        matrix2[i] = (int *)malloc(cols * sizeof(int));
        sum[i] = (int *)malloc(cols * sizeof(int));
    }
    
    // Input first matrix
    printf("Enter elements of the first matrix:\n");
    inputMatrix(matrix1, rows, cols);
    
    // Input second matrix
    printf("Enter elements of the second matrix:\n");
    inputMatrix(matrix2, rows, cols);
    
    // Add the matrices
    addMatrices(matrix1, matrix2, sum, rows, cols);
    
    // Print the result
    printf("Sum of the matrices:\n");
    printMatrix(sum, rows, cols);
    
    // Free allocated memory
    for (int i = 0; i < rows; i++) {
        free(matrix1[i]);
        free(matrix2[i]);
        free(sum[i]);
    }
    free(matrix1);
    free(matrix2);
    free(sum);
    
    return 0;
}

Output

C
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
1 2
3 4
Enter elements of the second matrix:
5 6
7 8
Sum of the matrices:
6 8
10 12

Conclusion

Adding two matrices using multi-dimensional arrays in C can be done in several ways. The simple iterative approach is straightforward and easy to understand. Using functions for modularity enhances code readability and reusability. Utilizing pointers provides flexibility in handling dynamic memory allocation. Each method has its own advantages and understanding these methods will help you handle matrix operations efficiently in C programming.