C program for matrix addition

devquora
devquora

Posted On: Dec 23, 2020

C program for matrix addition

 

Overview:

Matrix is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns. In this article I will introduce you with the mathematical concept Matrix and matrix Addition. After that, I will explain What is an array? and What is Multidimensional Array? Moreover, I will show Demonstration for addition of matrices. At last, I will show a C program to add two matrices with output in front of you.

Table of contents:

  1. What is Matrix?
  2. What is matrix Addition?
  3. What is an Array?
  4. What is multi dimensional array?
  5. Demonstration for addition of matrices.
  6. Logic for matrix addition
  7. c program for matrix addition
  8. Conclusion

What is Matrix?

A matrix is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns. The matrix of r rows and c column is known as r X c matrix. Matrices are often denoted using capital roman letters such as A, B and C. For example,

 

Demo of matrix

What is matrix Addition?

Matrix addition is the operation of adding two matrices by adding the corresponding entries together. Matrix addition is done element wise (entry wise) i.e. Sum of two matrices. A and B of size mXn is defined by:

(A + B) = Aij + Bij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)

What is an Array?

An array is a container for the collection of items that stores these items at contiguous memory locations. An array is a variable that can store multiple values. For example, if you want to store 100 integers in a single variable then you can create an array for it.

What is multi dimensional array?

Multidimensional array is an array of arrays. You can declare a two-dimensional (2d) array. For example, float x[3][4]; Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3];

Demonstration for addtion of matrices.

Demo of matrix addition

Logic for matrix addtion

To add two matrices, i.e., compute their sum and print it. A user inputs their orders (number of rows and columns) and the elements of these two matrices. For example, if the order is 2, 2, i.e., two rows and two columns and the matrices are as follows:

First matrix A =

1 1 2

3 1 4

Second matrix B=

4  1 5

-1 5 3

The Sum of these two matrices is:

1+4 1+1 2+5

3-1 1+5 4+3

=

5 2 7

2 6 7

The main Logic to write C program for the Matrix Addition is as follows:

  • Step 1: Take 3 arrays such as first[][], second[][], and sum[][].
  • Step 2: Accept the number of rows and columns for the two arrays.
  • Step 3: Then, accept elements of these two arrays.
  • Step 4: After all, add the corresponding elements of the arrays and store its elements to the new array and display its elements.

C program for matrix addition

#include <stdio.h>
 
int main()
{
   int r, c, i, j, first[10][10], second[10][10], sum[10][10];
 
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &r, &c);
   printf("Enter the elements of first matrix\n");
 
   for (i = 0; i < r; i++)
      for (j = 0; j < c; j++)
         scanf("%d", &first[i][j]);
 
   printf("Enter the elements of second matrix\n");
 
   for (i = 0; i < r; i++)
      for (j = 0 ; j < c; j++)
         scanf("%d", &second[i][j]);
   
   printf("Sum of entered matrices:-\n");
   
   for (i = 0; i < r; i++) {
      for (j = 0 ; j < c; j++) {
         sum[i][j] = first[i][j] + second[i][j];
         printf("%d\t", sum[i][j]);
      }
      printf("\n");
   }
 
   return 0;
}

MarixAdditionOutput

Conclusion:

This Program shows matrix addition through the addition of corresponding elements of the matrices.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..