Matrix multiplication in C involves multiplying two matrices to produce a third matrix. This operation requires the number of columns in the first matrix to equal the number of rows in the second matrix. Here’s a detailed explanation along with a sample implementation.

Explanation of Matrix Multiplication

  1. Definition:

    • Matrix multiplication is an operation where two matrices are multiplied to yield a new matrix. The element at the position C[i][j]C[i][j] in the resulting matrix CC is calculated by taking the dot product of the ii-th row of the first matrix AA and the jj-th column of the second matrix BB.
  2. Mathematical Representation:

    • For two matrices AA (of size m×nm \times n) and BB (of size n×pn \times p), the resulting matrix CC will have the size m×pm \times p:
    C[i][j]=k=0n1A[i][k]B[k][j]C[i][j] = \sum_{k=0}^{n-1} A[i][k] \cdot B[k][j]
    • Here, ii is the row index for AA, jj is the column index for BB, and kk is used to iterate over the columns of AA and the rows of BB.
  3. Conditions:

    • The number of columns in matrix AA must be equal to the number of rows in matrix BB. If AA has dimensions m×nm \times n, BB must have dimensions n×pn \times p.

Sample C Program for Matrix Multiplication

Here is a simple C program that demonstrates how to perform matrix multiplication:

#include <stdio.h> #define MAX 10 // Maximum size of the matrix void inputMatrix(int matrix[MAX][MAX], int rows, int cols) { printf("Enter elements of the matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("Element [%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } } } void printMatrix(int matrix[MAX][MAX], int rows, int cols) { printf("The matrix is:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } void multiplyMatrices(int A[MAX][MAX], int B[MAX][MAX], int C[MAX][MAX], int rowA, int colA, int colB) { for (int i = 0; i < rowA; i++) { for (int j = 0; j < colB; j++) { C[i][j] = 0; // Initialize the element for (int k = 0; k < colA; k++) { C[i][j] += A[i][k] * B[k][j]; // Multiply and accumulate } } } } int main() { int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX]; int rowA, colA, rowB, colB; // Input dimensions for the first matrix printf("Enter number of rows and columns for Matrix A: "); scanf("%d %d", &rowA, &colA); // Input dimensions for the second matrix printf("Enter number of rows and columns for Matrix B: "); scanf("%d %d", &rowB, &colB); // Check if multiplication is possible if (colA != rowB) { printf("Error: Number of columns in Matrix A must equal number of rows in Matrix B.\n"); return 1; // Exit with error } // Input matrices A and B printf("Matrix A:\n"); inputMatrix(A, rowA, colA); printf("Matrix B:\n"); inputMatrix(B, rowB, colB); // Multiply the two matrices multiplyMatrices(A, B, C, rowA, colA, colB); // Print the result printf("Resultant Matrix C (A * B):\n"); printMatrix(C, rowA, colB); return 0; }

Explanation of the Code

  1. Header Files:

    • #include <stdio.h>: Includes the standard input-output library.
  2. Macro Definition:

    • #define MAX 10: Defines a constant for the maximum size of the matrices.
  3. Functions:

    • inputMatrix: This function prompts the user to enter the elements of the matrix.
    • printMatrix: This function prints the matrix to the console.
    • multiplyMatrices: This function takes two matrices (A and B) and computes their product, storing the result in matrix C.
  4. main Function:

    • Prompts the user for the number of rows and columns for both matrices.
    • Checks if the multiplication is valid (i.e., the number of columns in A must equal the number of rows in B).
    • Calls inputMatrix to fill matrices A and B.
    • Calls multiplyMatrices to perform the multiplication.
    • Calls printMatrix to display the resultant matrix C.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc matrix_multiplication.c -o matrix_multiplication
  2. Execute the Program:

    ./matrix_multiplication
  3. Input Data: Follow the prompts to input the dimensions and elements of the matrices.

Example Input/Output

Input:

Enter number of rows and columns for Matrix A: 2 3 Enter number of rows and columns for Matrix B: 3 2 Matrix A: Element [0][0]: 1 Element [0][1]: 2 Element [0][2]: 3 Element [1][0]: 4 Element [1][1]: 5 Element [1][2]: 6 Matrix B: Element [0][0]: 7 Element [0][1]: 8 Element [1][0]: 9 Element [1][1]: 10 Element [2][0]: 11 Element [2][1]: 12

Output:

Resultant Matrix C (A * B): 58 64 139 154

Conclusion

Matrix multiplication is a fundamental operation in linear algebra and is widely used in various fields, including computer graphics, machine learning, and scientific computing. The above example provides a clear understanding of how to implement matrix multiplication in C. You can extend this program further by adding error handling, supporting larger matrices, or using dynamic memory allocation.