-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathass3.cpp
More file actions
153 lines (119 loc) · 2.53 KB
/
ass3.cpp
File metadata and controls
153 lines (119 loc) · 2.53 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
using namespace std;
class Matrix{
int m,n;
int **p;
public:
Matrix(int m,int n){
this.m = m;
this.n = n;
p=createMatrix(m,n);
}
int ** createMatrix(int m,int n){
int **temp = new int *[m];
for(int i=0;i<m;i++){
int *x = new int[n];
temp[i]=x;
}
return temp;
}
void inputMatrix(){
for(int i=0;i<this.m;i++){
for(int j=0;j<this.n;j++)
cin>>this.p[i][j];
}
}
void add(Matrix M3,Matrix M1,Matrix M2){
for(int i =0;i<M1.m;i++){
for(int j=0;i<M1.n;j++){
M3.p[i][j] = M1.p[i][j] + M2.p[i][j];
}
}
}
void display(){
for(int i=0;i<this.m;i++){
for(int j=0;j<this.n;j++){
cout << this.p[i][j] << '\t';
}
cout << endl;
}
}
};
/*
int** multiply(int **p,int**q,int m1,int n1,int m2,int n2){
int** answer = createMatrix(m1,n2);
for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++){
int sum=0;
for(int k=0;k<m2;k++){
sum += p[i][k]*q[k][j];
}
answer[i][j]=sum;
}
}
return answer;
}
void displayMatrix(int m,int n,int** temp){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout << temp[i][j] << "\t";
}
cout<<endl;
}
}
*/
int main(){
cout << "Matrix Operations choose any options from the following\n"<<endl;
cout << "1.)Add\n2.)Multiply\n3.)Transpose"<<endl;
int opt;
cin >> opt;
switch(opt){
case 1:
int m1,n1,m2,n2;
cout << "Enter the size of Matrix-1" << endl;
cin >> m1 >> n1;
Matrix M1(m1,n1);
M1.inputMatrix(m1,n1);
cout << "Enter the size of Matrix-2" << endl;
cin >> m2 >> n2;
Matrix M2(m2,n2);
M2.inputMatrix(m2,n2);
Matrix M3(m1,n1);
M3.add(M3,M1,M2);
M3.display();
break;
case 2:
cout << "Multiply";
break;
case 3:
cout << "Transpose";
break;
default:
cout << "Wrong Option";
break;
}
return 0;
}
/*
int m1,m2,n1,n2;
int **p,**q,**answer;
cout << "Enter the size of First Matrix:-" <<endl;
cin >> m1 >> n1;
Matrix M1(m1,n1);
cout << "Enter the size of the Second Matrix:-" <<endl;
cin >> m2 >> n2;
Matrix M2(m2,n2);
if(n1!=m2){
cout << "Multiplication not possible";
return 0;
}
else{
cout<< "\n Enter the elements of the 1st Matrix:-"<<endl;
p=inputMatrix(m1,n1);
cout<< "\n Enter the elements of the 2nd Matrix:-"<<endl;
q=inputMatrix(m2,n2);
answer=multiply(p,q,m1,n1,m2,n2);
cout << "The MUltiplication of two matrix is:-"<<endl;
displayMatrix(m1,n2,answer);
}
*/