Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program for Matrix Addition

Last Updated on April 18, 2023 by Prepbytes

The addition of matrices is a crucial operation in linear algebra and finds widespread use in fields like physics, engineering, and computer science. This article explores the topic of matrix addition and demonstrates how to implement it in the C programming language. To provide a robust conceptual understanding of the topic, this article also covers the dry run of matrix addition in C, the algorithm for matrix addition in C, and the complexity analysis of C program for matrix addition.

About Matrices

A matrix is a rectangular array consisting of numbers, symbols, variables, or expressions arranged in rows and columns. The size of a matrix is defined by the number of rows and columns it contains. For example, a matrix of size 2×3 has two rows and three columns. The elements of a matrix are usually denoted by their row and column positions, such as A[i,j], where i denotes the row number and j denotes the column number.

Matrices can be added, subtracted, multiplied, and transposed. The addition and subtraction of matrices involve adding or subtracting the corresponding elements of two matrices to obtain a new matrix. The multiplication of matrices is a more complex operation that involves the multiplication of rows and columns of two matrices to obtain a new matrix.

Dry Run of Matrix Addition in C

Suppose there are two matrices, namely A and B where Matrix A is [[2,4],[3,9]]. On the other hand, matrix B consists of the values [[3,5],[3,9]]. In order to add the matrix, there must be a point to keep in mind if the number of rows and columns or dimension of matrix share the same value in both the matrices or not as inequality of matrix dimension cannot make matrix addition possible.

To add the matrices, we start from coordinates (0,0) and traverse until (n-1,m-1) of both the matrices where n is the total number of rows and m is the total number of columns. We maintain a storage matrix to store the result from addition operations.

We add the elements at row 0 and column 0 i.e. 2+3 equals to 5.

We add the elements at row 0 and column 1 i.e. 4+5 equals to 9.

We add the elements at row, 1 and column,0 i.e. 3+3 equals to 6.

We add the elements at row, 1 and column,1 i.e. 9+9 equals to 18.

In this manner, we get matrix C as the result with added values from both matrix A and matrix B.

Algorithm for Matrix Addition in C

The algorithm for matrix addition in C is as follows:

  • Step 1 – Take two matrices of the same dimensions as input from the user.
  • Step 2 – Initialize a matrix to store the results, having the same dimensions as the input matrices.
  • Step 3 – Use nested loops to traverse the matrices, and add the elements from the input matrices at corresponding positions.
  • Step 4 – Store the sum of the elements in the storage array at the similar position.
  • Step 5 – Repeat step 3-4 for all the elements in both matrices.
  • Step 6 – Print the storage array, which contains the sum of both input matrices.

A dry run of the algorithm for matrix addition in C involves performing each sub-operation chronologically for the addition of matrices until an output matrix with added values from both matrices is obtained.

C Program For Matrix Addition

Now that we have understood how the program works using the dry run approach and logic behind the program with the help of the algorithm discussed in the above section, in this section, implementation of the c program for matrix addition is performed.

#include <stdio.h>

#define M 10
#define N 10

int main()
{
    int m, n, c, d, one[M][N], two[M][N], three[M][N];
    printf("Enter the number of rows and columns of matrix: ");
    scanf("%d%d", &m, &n);
    printf("Enter the elements of first matrix: ");
    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++) scanf("%d", &one[c][d]);
    printf("Enter the elements of second matrix: ");
    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++) scanf("%d", &two[c][d]);
    printf("Sum of entered matrices:\n");
    for (c = 0; c < m; c++)
    {
        for (d = 0; d < n; d++)
        {
            three[c][d] = one[c][d] + two[c][d];
            printf("%d\t", three[c][d]);
        }
        printf("\n");
    }
    return 0;
}

Explanation:
Upon receiving input from the user, a storage array is created to store all the obtained results. The program then initializes a loop with an integer i set to 0 that runs until n-1 (the number of rows). Inside the loop, another nested loop is initialized with an integer j set to 0 that runs until m-1 (the number of columns). During each iteration of the loop, the program performs an addition operation, where the elements located at the ith row and jth column from both matrices are summed and stored in the resultant matrix.

Complexity Analysis of C Program for Matrix Addition

The time complexity of the program will jump up to O(N^2) for a matrix with equal rows and columns and O(M*N) for a matrix with differing rows and columns and the reason being that each row is going to be traversed column times and vice versa.

The space complexity is going to be O(N^2) for matrices with equal rows and columns as auxiliary space will be required for all the rows as well as columns. In clear words, an entire dummy matrix will be created for the purpose of storing results.

Conclusion
In conclusion, the C program for matrix addition is a simple and straightforward implementation of a fundamental operation in linear algebra. In this article, we mainly focused on the C program for matrix addition, we understood what a matrix is and how addition can be performed on a matrix. Later, we moved to the code implementation along with the dry run algorithm and analyzed the complexity of the code for the space and time it takes in the worst case.

FAQs on Matrix Addition in C

Here are some frequently asked questions on matrix addition in C.

Q1: What is the difference between matrix multiplication and matrix addition in C programming?
Ans: Matrix addition in C programming involves adding the corresponding elements of two matrices to obtain a new matrix, while matrix multiplication involves the multiplication of rows and columns of two matrices to obtain a new matrix.

Q2: Can matrices of different sizes be added in C programming?
Ans: No, matrices of different sizes cannot be added in C programming.

Q3: How do you transpose a matrix in C programming?
Ans: To transpose a matrix in C programming, you need to interchange its rows and columns.

Q4: Can a matrix be multiplied by a scalar in C programming?
Ans: Yes, a matrix can be multiplied by a scalar in C programming.

Q5: What is the difference between a row vector and a column vector in C programming?
Ans: A row vector is a matrix with a single row, while a column vector is a matrix with a single column.

Q6: Can you add a row vector to a column vector in C programming?
Ans: No, you cannot add a row vector to a column vector in C programming.

Q7: What is a sparse matrix in C programming?
Ans: A sparse matrix in C programming is a matrix that contains mostly zero elements.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers
Calculator Program in C Language
Simple Interest Program in C
Compound Interest Program in C
Priority Scheduling Program in C
Doubly Linked List Program in C
FCFS Scheduling Program in C
Insertion Sort Program in C
Singly Linked List Program in C
Star Program in C
String Program in C
Radix Sort Program in C

Leave a Reply

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