Add Two Matrices In C

C Code

Example

#include <stdio.h>
int main() {
    // Declare a 2D array with 3 rows and 4 columns
    int matrix[3][4];
    
    // Assign values to the matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            matrix[i][j] = i * 4 + j;
        }
    }
    
    // Print the matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

In this instance, we initially define a 2D array named "matrix" comprising of 3 rows and 4 columns. Subsequently, we employ nested iterations to populate the matrix. The outer loop traverses the rows while the inner loop moves through the columns. Ultimately, we utilize an additional series of nested iterations to display the matrix, with each item delimited by a space and each row appearing on a fresh line. Another approach involves crafting a matrix through dynamic memory allocation by following these guidelines:

Output

Output

0 1 2 3 
4 5 6 7 
8 9 10 11

Explanation:

This example illustrates the fundamental concept of generating and managing a matrix in C through a 2D array. It showcases the process of populating the matrix with values via nested iterations, and displaying the matrix utilizing the same nested loop pattern. Nested loops play a crucial role in matrix operations and manipulations within the C programming language, serving as the foundational components for various matrix computations.

How to Add Two Matrices in C

Here is an illustration of how to perform addition of two matrices in the C programming language:

C Code

Example

#include <stdio.h>
int main() {
    // Declare two matrices with 3 rows and 4 columns
    int matrix1[3][4], matrix2[3][4];
    
    // Assign values to the first matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            matrix1[i][j] = i * 4 + j;
        }
    }
    
    // Assign values to the second matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            matrix2[i][j] = i * 4 + j + 12;
        }
    }
    
    // Declare a new matrix to store the result of the addition
    int result[3][4];
    
    // Add the two matrices and store the result in the new matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    
    // Print the result
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

Output

Output

12 14 16 18 
20 22 24 26 
28 30 32 34

The result produced by the aforementioned code will be a 3x4 matrix comprising integer values. Each element's value is calculated using the formula "i 4 + j + (i 4 + j + 12)", derived from the summation of the two matrices.

Input Required

This code uses input(). Please provide values below: