C Program to Add Two Matrices

Introduction

Adding two matrices involves summing the corresponding elements of the two matrices to produce a new matrix. This guide will show you how to write a C program that adds two matrices provided by the user.

Problem Statement

Create a C program that:

  • Takes two matrices as input from the user.
  • Adds the corresponding elements of the two matrices.
  • Displays the resulting matrix.

Example:

  • Input:
    • Matrix 1:
      1 2 3
      4 5 6
      7 8 9
      
    • Matrix 2:
      9 8 7
      6 5 4
      3 2 1
      
  • Output: Resultant Matrix:
    10 10 10
    10 10 10
    10 10 10
    

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two input matrices, the resultant matrix, and loop counters.
  4. Input the Dimensions and Elements of the Matrices: Use loops to take input from the user for both matrices.
  5. Add the Corresponding Elements of the Matrices: Use nested loops to iterate through each element of the matrices and add them.
  6. Display the Resulting Matrix: Use loops to display the resulting matrix.

C Program to Add Two Matrices

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrices and dimensions
    int rows, cols;
    int matrix1[100][100], matrix2[100][100], result[100][100];
    int i, j;

    // Step 2: Prompt the user to enter the dimensions of the matrices
    printf("Enter the number of rows and columns of the matrices: ");
    scanf("%d %d", &rows, &cols);

    // Step 3: Input the elements of the first matrix
    printf("Enter elements of the first matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Step 4: Input the elements of the second matrix
    printf("Enter elements of the second matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Step 5: Add the corresponding elements of the matrices
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    // Step 6: Display the resulting matrix
    printf("Resultant matrix after addition:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;  // Step 7: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variables rows and cols store the dimensions of the matrices. matrix1, matrix2, and result are 2D arrays to store the input matrices and the resulting matrix, respectively. The variables i and j are used as loop counters.

Step 2: Input the Dimensions of the Matrices

  • The program prompts the user to enter the number of rows and columns for the matrices using scanf.

Step 3: Input the Elements of the First Matrix

  • The program uses nested for loops to take input for each element of the first matrix from the user.

Step 4: Input the Elements of the Second Matrix

  • Similarly, the program uses nested for loops to take input for each element of the second matrix.

Step 5: Add the Corresponding Elements of the Matrices

  • The program uses nested for loops to iterate through each element of the matrices:
    • The corresponding elements from matrix1 and matrix2 are added and stored in the result matrix.

Step 6: Display the Resulting Matrix

  • The program uses nested for loops to display the elements of the resulting matrix after addition.

Step 7: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of rows and columns of the matrices: 3 3
Enter elements of the first matrix:
1 2 3
4 5 6
7 8 9
Enter elements of the second matrix:
9 8 7
6 5 4
3 2 1
Resultant matrix after addition:
10 10 10 
10 10 10 
10 10 10

Another Example:

Enter the number of rows and columns of the matrices: 2 2
Enter elements of the first matrix:
1 1
2 2
Enter elements of the second matrix:
3 3
4 4
Resultant matrix after addition:
4 4
6 6

Conclusion

This C program demonstrates how to add two matrices by iterating through each element and summing the corresponding elements from the two matrices. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top