forked from pravalika2604/Basic-c-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix operator overloading.cpp
More file actions
135 lines (128 loc) · 2.71 KB
/
matrix operator overloading.cpp
File metadata and controls
135 lines (128 loc) · 2.71 KB
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
131
132
133
134
135
#include<iostream>
using namespace std;
class matrix
{
int r,c;
int a[10][10],b[10][10];
public:
matrix()
{
cout<<"matrix:constructed"<<endl;
}
~matrix()
{
cout<<" matrix:destructed"<<endl;
}
void read()
{
int i,j;
cout<<"enter the number of rows in matrix"<<endl;
cin>>r;
cout<<"enter the number of columns in matrix"<<endl;
cin>>c;
cout<<"enter the elements in matrix:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
}
matrix operator +(matrix &x)
{
matrix p;
p.r=r;
p.c=c;
cout<<p.r;
cout<<p.c;
for(int i=0;i<p.r;i++)
{
for(int j=0;j<p.c;j++)
{
p.a[i][j]=a[i][j]+x.a[i][j];
}
}
return p;
}
matrix operator -(matrix &y)
{
matrix p;
p.r=r;
p.c=c;
cout<<p.r;
cout<<p.c;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
p.a[i][j]=a[i][j]-y.a[i][j];
}
}
return p;
}
matrix operator *(matrix &z)
{
matrix p;
p.r=r;
p.c=c;
cout<<p.r;
cout<<p.c;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
p.a[i][j]=0;
for(int k=0;k<z.c;k++)
{
p.a[i][j]=p.a[i][j]+a[i][j]*z.a[j][k];
}
}
}
return p;
}
void display()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<a[i][j]<<" \t ";
}
cout<<"\n";
}
}
};
int main()
{
int ch;
matrix m1,m2,m3;
do
{
cout<<"\n1.addition \n2.subtraction \n3.multiplication \n4.exit"<<endl;
cout<<"enter the choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
m1.read();
m2.read();
m3=m1+m2;
m3.display();
break;
case 2:
m1.read();
m2.read();
m3=m1-m2;
m3.display();
break;
case 3:
m1.read();
m2.read();
m3=m1*m2;
m3.display();
break;
}
}
while(ch!=4);
}