News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

A simple program to print the sum of MXN Matrix

Started by thiruvasagamani, Aug 11, 2008, 11:02 AM

Previous topic - Next topic

thiruvasagamani

To print the sum of MXN Matrix using C++

Here is the sample code

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

class matrix
{
private :
int a[10][10];
int row,col;
public  :
void readmatrix(int, int);
void sumofmatrix(matrix, matrix);
void printmatrix();
};

void matrix::readmatrix(int m, int n)
{
row=m;
col=n;
for(int i=0;i<row; i++)
for(int j=0; j<col; j++)
cin>>a[i][j];
}


void matrix::sumofmatrix(matrix tem1, matrix tem2)
{

row=tem1.row;
col=tem1.col;

for(int i=0;i<row; i++)
for(int j=0; j<col; j++)
a[i][j]= tem1.a[i][j] + tem2.a[i][j];


}
void matrix::printmatrix()
{

for(int i=0;i<row;i++)
{
for (int j=0; j<col;j++)
cout<<setw(3)<<a[i][j];
cout<<endl;
}
}

void main ()
{
matrix mat1, mat2, sum;

int m1,n1, m2,n2;

cout<<"Enter M & N : row and columns 1"<<endl;
cin>>m1>>n1;

cout<<"Enter M & N : row and columns 1"<<endl;
cin>>m2>>n2;

if(m1!=m2 || n1!=n2)
{
cout<<"Invalid row and column value"<<endl;
getch();
return;
}

cout<<"Enter Matrix 1 : "<<m1<<" x "<<n1<<endl;
mat1.readmatrix(m1,n1);

cout<<"Enter Matrix 2 : "<<m2<<" x "<<n2<<endl;
mat2.readmatrix(m2,n2);

sum.sumofmatrix(mat1, mat2);

cout<<"sum  matrix : "<<m1<<" x "<<n1<<endl;
sum.printmatrix();

getch();
}  
Pr
Thiruvasakamani Karnan