Matrix addition in C involves adding two matrices of the same dimensions by summing the corresponding elements. Below is a detailed explanation of the concept along with a sample implementation.

Explanation of Matrix Addition

  1. Definition:

    • Matrix addition is the operation of adding two matrices by adding their corresponding elements. For two matrices AA and BB to be added, they must have the same number of rows and columns.
  2. Mathematical Representation:

    • If AA and BB are two matrices: C[i][j]=A[i][j]+B[i][j]C[i][j] = A[i][j] + B[i][j]
    • Here, CC is the resultant matrix, and ii and jj are the indices of the rows and columns, respectively.
  3. Conditions:

    • The matrices must have the same dimensions. For example, if AA is a m×nm \times n matrix, then BB must also be a m×nm \times n matrix.

Sample C Program for Matrix Addition

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

#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 addMatrices(int A[MAX][MAX], int B[MAX][MAX], int C[MAX][MAX], int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { C[i][j] = A[i][j] + B[i][j]; } } } int main() { int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX]; int rows, cols; // Input the dimensions of the matrices printf("Enter number of rows and columns: "); scanf("%d %d", &rows, &cols); // Input matrices A and B printf("Matrix A:\n"); inputMatrix(A, rows, cols); printf("Matrix B:\n"); inputMatrix(B, rows, cols); // Add the two matrices addMatrices(A, B, C, rows, cols); // Print the result printf("Resultant Matrix C (A + B):\n"); printMatrix(C, rows, cols); return 0; }

Explanation of the Code

  1. Header Files:

    • #include <stdio.h>: Includes the standard input-output library for functions like printf and scanf.
  2. Macro Definition:

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

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

    • Prompts the user for the number of rows and columns.
    • Calls inputMatrix to fill matrices A and B.
    • Calls addMatrices to perform the addition.
    • 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_addition.c -o matrix_addition
  2. Execute the Program:

    ./matrix_addition
  3. Input Data: Follow the prompts to input the number of rows, columns, and the elements of the matrices.

Example Input/Output

Input:

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

Output:

Resultant Matrix C (A + B): 6 8 10 12

This example demonstrates the basic procedure for matrix addition in C. You can extend this program to include error handling or support for larger matrices by dynamically allocating memory, but this simple implementation is a good starting point.