Introduction

Matrix subtraction is an operation that involves subtracting the corresponding elements of two matrices of the same dimensions. This operation is widely used in fields such as computer graphics, physics, and statistics. Just like matrix addition, matrix subtraction is straightforward and forms the basis for more complex matrix operations.

#include <stdio.h>

#define MAX_SIZE 10 // Define maximum size for the matrices

void subtractMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = a[i][j] - b[i][j]; // Subtract corresponding elements
        }
    }
}

int main() {
    int a[MAX_SIZE][MAX_SIZE], b[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
    int rows, cols;

    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    printf("Enter elements of the first matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("a[%d][%d]: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter elements of the second matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("b[%d][%d]: ", i, j);
            scanf("%d", &b[i][j]);
        }
    }

    subtractMatrices(a, b, result, rows, cols);

    printf("Resultant matrix after subtraction:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Explanation

  1. Function Definition:
    • subtractMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows, int cols): This function takes two input matrices (a and b), an empty matrix (result) for storing the difference, and the number of rows and columns.
  2. Loop:
    • Nested for loops iterate over each element of the matrices. For each element at position (i, j), the corresponding elements of matrices a and b are subtracted and stored in result.
  3. Main Function:
    • Prompts the user for the dimensions of the matrices.
    • Accepts the elements for both matrices from user input.
    • Calls subtractMatrices to perform the subtraction.
    • Prints the resultant matrix.

Input

When you run the program, it will ask for the number of rows and columns, followed by the elements of each matrix. For example:

Enter the number of rows and columns: 2 2
Enter elements of the first matrix:
a[0][0]: 8
a[0][1]: 6
a[1][0]: 4
a[1][1]: 2
Enter elements of the second matrix:
b[0][0]: 5
b[0][1]: 3
b[1][0]: 2
b[1][1]: 1

Output

The output will display the resultant matrix after subtraction:

Resultant matrix after subtraction:
3 3 
2 1

Conclusion

Matrix subtraction is a fundamental operation in mathematics and various applications. The provided C implementation demonstrates how to perform subtraction of two matrices of the same dimensions. The simplicity of the algorithm makes it easy to understand and apply in different contexts, while also providing a foundation for more complex matrix operations.

Leave a Reply

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

Verified by MonsterInsights