Addition of two matrices using c++ program

Posted By: Java Examples - 5:18 AM

Share

& Comment

This c program add two matrices i.e. compute the sum of two matrices and then print it. Firstly user will be asked to enter the order of matrix ( number of rows and columns ) and then two matrices. For example if the user entered order as 2, 2 i.e. two rows and two columns and matrices as

First Matrix :-
2 2
2 4
Second matrix :-
4 5
-1 5
then output of the program ( sum of First and Second matrix ) will be
6 7
1 9

Algorithm:

Rule: Addition of two matrixes is only possible if both matrixes are of same size.

Suppose two matrixes A and B is of same size m X n

Sum of two marixes is defined as

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

For example:
Suppose two matrixes A and B of size of 2 X 3 is as follow:




C ++programming code :



#include
int i,j,a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
using namespace std;


bool checkMatrix()
{
 /* Before accepting the Elements Check if no of
   rows and columns of both matrices is equal */
 return  (m1 != m2 || n1 != n2 );
}

void getMatrix()
{
 /* m - Number of rows
   n - Number of Columns */

 cout<<"\nEnter the number of Rows of Matrix 1 : ";
 cin>>m1;
 cout<<"\nEnter the number of Columns of Matrix 1 : ";
 cin>>n1;

 /* Accept the Elements in m x n Matrix */
 for( i = 0 ; i < m1 ; i++ )
  for( j = 0 ; j < n1 ; j++ )
  {
  cout<<"Enter the Element a["<>a[i][j];
  }

 // ------------------------------------------
 cout<<"\nEnter the number of Rows of Matrix 2 : ";
 cin>>m2;
 cout<<"\nEnter the number of Columns of Matrix 2 : ";
 cin>>n2;

 

 // ------ Terminate Program if Orders are unequal
 if ( checkMatrix() )
  {
   cout<<"\nOrder of two matrices is not same\n ";
   system("PAUSE");
   exit(0);
  }
 else
 {
 /* Accept the Elements in m x n Matrix */
 for( i = 0 ; i < m2 ; i++ )
  for( j = 0 ; j < n2 ; j++ )
  {
  cout<<"Enter the Element b["<>b[i][j];
  }

 // ------------------------------------------
 
 }
}

/* Addition of two matrices */
void sumMatrix()
{
 for( i = 0 ; i < m1 ; i++ )
  for( j = 0 ; j < n1 ; j++ )
  {
   c[i][j]= a[i][j] + b[i][j] ;
  }
}

/* Print out the Resultant Matrix */
void printMatrix()
{
cout<<"\nThe Subtraction of two Matrices is : \n";

for( i = 0 ; i < m1 ; i++ )
 {
    for( j = 0 ; j < n1 ; j++ )
    {
    cout<

Output of program :



Enter the number of Rows of Matrix 1 : 3

Enter the number of Columns of Matrix 1 : 2
Enter the Element a[0][0] : 2
Enter the Element a[0][1] : 3
Enter the Element a[1][0] : 4
Enter the Element a[1][1] : 5
Enter the Element a[2][0] : 4
Enter the Element a[2][1] : 8

Enter the number of Rows of Matrix 2 : 3

Enter the number of Columns of Matrix 2 : 2
Enter the Element b[0][0] : 8
Enter the Element b[0][1] : 5
Enter the Element b[1][0] : 6
Enter the Element b[1][1] : 1
Enter the Element b[2][0] : 2
Enter the Element b[2][1] : 3

The Subtraction of two Matrices is :
10      8
10      6
6       11

 Date - 1/25/2013-@author Y.ACHCHUTHAN -cpp90.blogspot.com
Press any key to continue . . .





About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

0 comments :

Post a Comment

Copyright © 2016 C++ Examples ACHCHUTHAN.ORG. Designed by Templateism .