Matrix multiplication in C explain with example

Matrix Multiplication in C

Introduction of Matrix in C

In this article we are going to discuss how we can do Matrix Multiplication in C and Matrix addition in C. All Matrix example we mentioned here using two dimensions array. Let go through this article and know more about Matrix Multiplication in C and Matrix Additions in C.

Explain Matrix Addition in C

Matrix addition involves adding corresponding elements of two matrices to create a new matrix. The matrices must be of the same size. Here we are using two dimensional array for all example.

Example:

Matrix A:

1 2
3 4

Matrix B:

5 6
7 8

Resultant Matrix C (A + B):

6 8
10 12

Code for Matrix Addition in C

#include <stdio.h>

#define ROWS 2
#define COLS 2

void addMatrices(int a[ROWS][COLS], int b[ROWS][COLS], int result[ROWS][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            result[i][j] = a[i][j] + b[i][j];
        }
    }
}

void printMatrix(int matrix[ROWS][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 A[ROWS][COLS] = {{1, 2}, {3, 4}};
    int B[ROWS][COLS] = {{5, 6}, {7, 8}};
    int C[ROWS][COLS];

    addMatrices(A, B, C);

    printf("Resultant Matrix:\n");
    printMatrix(C);

    return 0;
}

Matrix Multiplication in C

Matrix multiplication involves multiplying rows of the first matrix by columns of the second matrix and summing the results. The number of columns in the first matrix must equal the number of rows in the second matrix.

Example:

Matrix A (2×3):

1 2 3
4 5 6

Matrix B (3×2):

7  8
9 10
11 12

Resultant Matrix C (A * B)

 58  64
139 154

Code for Matrix Multiplication in C

#include <stdio.h>

#define ROWS_A 2
#define COLS_A 3
#define ROWS_B 3
#define COLS_B 2

void multiplyMatrices(int a[ROWS_A][COLS_A], int b[ROWS_B][COLS_B], int result[ROWS_A][COLS_B]) {
    for (int i = 0; i < ROWS_A; i++) {
        for (int j = 0; j < COLS_B; j++) {
            result[i][j] = 0;
            for (int k = 0; k < COLS_A; k++) {
                result[i][j] += a[i][k] * b[k][j];
            }
        }
    }
}

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

int main() {
    int A[ROWS_A][COLS_A] = {{1, 2, 3}, {4, 5, 6}};
    int B[ROWS_B][COLS_B] = {{7, 8}, {9, 10}, {11, 12}};
    int C[ROWS_A][COLS_B];

    multiplyMatrices(A, B, C);

    printf("Resultant Matrix:\n");
    printMatrix(C);

    return 0;
}

These examples demonstrate basic matrix operations in C. The addition example sums corresponding elements, while the multiplication example multiplies and sums appropriate elements from the matrices.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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