By | 27.12.2016

# Subtract Two Matrices using array

Write a C++ Program to Subtract Two Matrices using array. Here’s simple Program to Subtract Two Matrices using array in C++ Programming Language.

## What is Matrix ?

Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

• type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.

Below is the source code for C++ Program to Subtract Two Matrices using array which is successfully compiled and run on Windows System to produce desired output as shown below :

### SOURCE CODE : :

```/*  C++ Program to Subtract Two Matrices using array  */

#include<iostream>
using namespace std;

int main()
{

int arr1, arr2, arr3, sub, i, j,m,n;

cout<<"Enter size of matrix ( Max:5 ) :: ";
cin>>m;
cout<<"\nEnter Elements to Matrix A Below :: \n";

for(i=0;i<m;i++)
{
for(j=0;j<m;++j)
{
cout<<"\nEnter arr1["<<i<<"]["<<j<<"] Element :: ";
cin>>arr1[i][j];
}

}

cout<<"\nEnter Elements to Matrix B Below :: \n";

for(i=0;i<m;i++)
{
for(j=0;j<m;++j)
{
cout<<"\nEnter arr2["<<i<<"]["<<j<<"] Element :: ";
cin>>arr2[i][j];
}

}

cout<<"\nSubtracting Matrix ( A - B ) ..... \n";
for(i=0; i<m; i++)
{
for(j=0; j<m; j++)
{
arr3[i][j]=arr1[i][j]-arr2[i][j];
}
}

cout<<"\nAfter Subtraction, Matrix C is :: \n";

for (i = 0; i < m; ++i)
{
for (j = 0; j < m; ++j)
{
cout<<"\t"<<arr3[i][j];
}
printf("\n\n");
}

return 0;
}```

### OUTPUT : :

```/*  C++ Program to Subtract Two Matrices using array  */

Enter size of matrix ( Max:5 ) :: 3

Enter Elements to Matrix A Below ::

Enter arr1 Element :: 1

Enter arr1 Element :: 2

Enter arr1 Element :: 3

Enter arr1 Element :: 4

Enter arr1 Element :: 5

Enter arr1 Element :: 6

Enter arr1 Element :: 7

Enter arr1 Element :: 8

Enter arr1 Element :: 9

Enter Elements to Matrix B Below ::

Enter arr2 Element :: 9

Enter arr2 Element :: 8

Enter arr2 Element :: 7

Enter arr2 Element :: 6

Enter arr2 Element :: 5

Enter arr2 Element :: 4

Enter arr2 Element :: 3

Enter arr2 Element :: 2

Enter arr2 Element :: 1

Subtracting Matrix ( A - B ) .....

After Subtraction, Matrix C is ::
-8      -6      -4

-2      0       2

4       6       8

Process returned 0```

Above is the source code for C++ Program to Subtract Two Matrices using array which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval. 