Quantcast
Channel: Java Question Bank » Cpp Programs
Viewing all articles
Browse latest Browse all 22

Matrix operations using overloaded operators in C++

$
0
0

*******************************************************************************************************

Matrix operations using overloaded operators in  C++

*******************************************************************************************************

Program :To implement matrix operations using overloaded operators
     (<<,>>,+,-,*)

The operations supported by this ADT are

a.Reading

b.Printing

c.Addition

d.Subtraction

e.Multiplication    */

Source Code :

=========

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
&nbsp;

#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;stdlib.h&gt;
int m1,n1,m2,n2;
class Matrix
{
int a[10][10],m,n;
public:
Matrix(int a,int b)
{
m=a;
n=b;
}
friend istream &amp; operator &gt;&gt;(istream &amp;get,Matrix &amp;m);
friend ostream &amp; operator &lt;&lt;(ostream &amp;put,Matrix &amp;m);
Matrix operator +(Matrix);
Matrix operator -(Matrix);
Matrix operator *(Matrix);
};
istream &amp; operator &gt;&gt;(istream &amp;get,Matrix &amp;x)
{
for(int i=0;i&lt;x.m;i++)
for(int j=0;j&lt;x.n;j++)
get&gt;&gt;x.a[i][j];
return get;
}
ostream &amp; operator &lt;&lt;(ostream &amp;put,Matrix &amp;x)
{
for(int i=0;i&lt;x.m;i++)
{
for(int j=0;j&lt;x.n;j++)
put&lt;&lt;x.a[i][j]&lt;&lt;"\t";
put&lt;&lt;endl;
}
return put;
}
Matrix Matrix::operator +(Matrix x)
{
Matrix c(m1,n1);
for(int i=0;i&lt;m;i++)
for(int j=0;j&lt;n;j++)
c.a[i][j]=a[i][j]+x.a[i][j];
return c;
}
Matrix Matrix::operator -(Matrix x)
{
Matrix c(m1,n1);
c.m=m;
c.n=n;
for(int i=0;i&lt;m;i++)
for(int j=0;j&lt;n;j++)
c.a[i][j]=a[i][j]-x.a[i][j];
return c;
}
Matrix Matrix::operator *(Matrix x)
{
Matrix c(m1,n2);
for(int i=0;i&lt;m1;i++)
for(int j=0;j&lt;n2;j++)
{
c.a[i][j]=0;
for(int k=0;k&lt;n1;k++)
c.a[i][j]+=(a[i][k]*x.a[k][j]);
return c;
}
void main()
{
clrscr();
cout&lt;&lt;"\n Enter the order of first matrix:";
cin&gt;&gt;m1&gt;&gt;n1;
cout&lt;&lt;"\n Enter the order of second matrix:";
cin&gt;&gt;m2&gt;&gt;n2;
Matrix a(m1,n1),b(m2,n2),c(m1,n1),m(m1,n1),n(m1,n2);
if(m1==n2)
{
cout&lt;&lt;"\n Enter the elements of first matrix:";
cin&gt;&gt;a;
cout&lt;&lt;"\n Enter the elements of second matrix:";
cin&gt;&gt;b;
c=a+b;
cout&lt;&lt;"\n The first matrix A:\n";
cout&lt;&lt;a;
cout&lt;&lt;"\n The second matrix B:\n";
cout&lt;&lt;b;
cout&lt;&lt;"\n The resultant matrix after addition is:\n";
cout&lt;&lt;c;
m=a-b;
cout&lt;&lt;"\n The resultant matrix after subtraction is:\n";
cout&lt;&lt;m;
n=a*b;
cout&lt;&lt;"\n The resultant matrix after multiplication is:\n";
cout&lt;&lt;n;
}
else
cout&lt;&lt;"\n Matrix operation is not possible";
getch();
}

/*---------------INPUT/OUTPUT-----------
Enter the order of first matrix :2
2
Enter the order of second matrix :2
2
Enter the elements of first matrix :1
2
3
4
Enter the elements of second matrix :1
0
0
1
The first matrix A :
1   2
3   4
The second matrix B:
1   0
0   1
The resultant matrix after addition is:
2   2
3   5
The resultant matrix after subtraction is:
0   2
3   3
The resultant matrix after multiplication is:
1   2
3   4

*/

46 total views, no views today


Viewing all articles
Browse latest Browse all 22

Trending Articles