Program 41 - CSU1128 - CSE 2026 - Shoolini University

Program to add two matrices entered by the user

Explanation

The program adds two matrices entered by the user and prints the result matrix.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and several variables are declared:

  • int d, m: Stores the number of rows and columns of the matrices.
  • int a[d][m]: Declares the first 2D array of integers with d rows and m columns.
  • int b[d][m]: Declares the second 2D array of integers with d rows and m columns.
  • int c[d][m]: Declares the result 2D array of integers with d rows and m columns.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the size and elements of the matrices:

  • printf("Enter the size of the matrices (rows columns): ");: Prompts the user to enter the number of rows and columns of the matrices.
  • scanf("%d%d", &d, &m);: Reads the number of rows and columns and stores them in d and m.
  • printf("Enter the elements of the first matrix:\n");: Prompts the user to enter the elements of the first matrix.
  • for (int i = 0; i < d; i++): Outer loop to iterate over each row of the first matrix.
  • for (int j = 0; j < m; j++): Inner loop to iterate over each column of the first matrix.
  • scanf("%d", &a[i][j]);: Reads each element of the first matrix and stores it in the array a.
  • printf("Enter the elements of the second matrix:\n");: Prompts the user to enter the elements of the second matrix.
  • for (int i = 0; i < d; i++): Outer loop to iterate over each row of the second matrix.
  • for (int j = 0; j < m; j++): Inner loop to iterate over each column of the second matrix.
  • scanf("%d", &b[i][j]);: Reads each element of the second matrix and stores it in the array b.

Matrix Addition

The program adds the corresponding elements of the two matrices:

  • for (int i = 0; i < d; i++): Outer loop to iterate over each row.
  • for (int j = 0; j < m; j++): Inner loop to iterate over each column.
  • c[i][j] = a[i][j] + b[i][j];: Adds the corresponding elements of the first and second matrices and stores the result in the array c.

Output Result

The program prints the result matrix:

  • printf("The sum of the entered matrix is: \n");: Prints a label for the result matrix.
  • for (int i = 0; i < d; i++): Outer loop to iterate over each row.
  • for (int j = 0; j < m; j++): Inner loop to iterate over each column.
  • printf("%d ", c[i][j]);: Prints each element of the result matrix followed by a space.
  • printf("\n");: Prints a newline after each row.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    printf("\n\n Program to add two matrices entered by the user. \n\n");

    // Read the size of the matrices from the user
    printf("Enter the size of the matrices (rows columns): ");
    int d, m;
    scanf("%d%d", &d, &m);

    // Declare two 2D arrays of integers with d rows and m columns
    int a[d][m], b[d][m];

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

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

    // Declare a third 2D array of integers with d rows and m columns to store the result of the matrix addition
    int c[d][m];

    // Add the corresponding elements of the first and second matrices
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < m; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }

    // Print the elements of the result matrix
    printf("The sum of the entered matrix is: \n");
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("%d ", c[i][j]);
        }
        printf("\n"); // Print a newline after each row
    }

    return 0;
}

                

Output

Program to add two matrices entered by the user.

Enter the size of the matrices (rows columns): 2 2
Enter the elements of the first matrix: 3 2 4 5
Enter the elements of the second matrix: 3 2 5 4
The sum of the entered matrix is:
6 4
9 9