Write a program to add two matrix...
#include<iostream.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],t[3][3],i,j,r,c;
clrscr();
cout<<"\n Enter rows: ";
cin>>r;
cout<<"\n Enter columns: ";
cin>>c;
cout<<"\n For Matrix A\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<" Enter Elements: "<<i+1<<"\t"<<j+1<<" "<<":: ";
cin>>a[i][j];
}
}
cout<<"\n For matrix B\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<" Enter elements: "<<i+1<<"\t"<<j+1<<" "<<":: ";
cin>>b[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
t[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\n Result Matrix is: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<t[i][j];
}
cout<<"\n";
}
getch();
}
Here is the output :)))
2} with class :))
#include<iostream.h>
#include<conio.h>
class matrix
{
public:
int a[3][3],b[3][3],t[3][3],i,j,r,c;
void rc()
{
cout<<"\n Enter The Number of Rows: ";
cin>>r;
cout<<"\n Enter The Number of Columns: ";
cin>>c;
}
void loop1()
{
cout<<"\n For Matrix A:";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\n Enter Elements: "<<i+1<<" "<<j+1<<" "<<": ";
cin>>a[i][j];
}
}
}
void loop2()
{
cout<<"\n For Matrix B: ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\n Enter Elements: "<<i+1<<" "<<j+1<<" "<<": ";
cin>>b[i][j];
}
}
}
void sum()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
t[i][j]=a[i][j]+b[i][j];
}
}
}
void total()
{
cout<<"\n For Result Matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<t[i][j];
}
cout<<"\n";
}
}
};
void main()
{
clrscr();
matrix m1;
m1.rc();
m1.loop1();
m1.loop2();
m1.sum();
m1.total();
getch();
}
Here is the output :)))
No comments:
Post a Comment