-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical1.cpp
116 lines (115 loc) · 2.87 KB
/
practical1.cpp
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
/*C++ classes with static members,methods with default arguments, and friend
functions. (For example, design matrix and MATRIX classes with static allocation,
and a friend function to do matrix-MATRIX multiplication)*/
#include <iostream>
using namespace std;
class MATRIX; // Forward declaration
class Matrix {
private:
static const int rows = 3; // Static allocation: fixed rows
static const int cols = 3; // Static allocation: fixed cols
int data[rows][cols]; // Array to store matrix elements
public:
// Constructor with default values
Matrix(int defaultVal = 0) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
data[i][j] = defaultVal;
}
}
}
// Method to input matrix elements
void input() {
cout << "Enter elements for Matrix (" << rows << "x" << cols
<< "):" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> data[i][j];
}
}
}
// Method to display matrix
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
// Friend function for matrix multiplication
friend void multiply(const Matrix &mat1, const MATRIX &mat2);
};
class MATRIX {
private:
static const int rows = 3; // Static allocation: fixed rows
static const int cols = 3; // Static allocation: fixed cols
int data[rows][cols]; // Array to store matrix elements
public:
// Constructor with default values
MATRIX(int defaultVal = 1) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
data[i][j] = defaultVal;
}
}
}
// Method to input matrix elements
void input() {
cout << "Enter elements for MATRIX (" << rows << "x" << cols
<< "):" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> data[i][j];
}
}
}
// Method to display matrix
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
// Friend function for matrix multiplication
friend void multiply(const Matrix &mat1, const MATRIX &mat2);
};
// Friend function for matrix multiplication
void multiply(const Matrix &mat1, const MATRIX &mat2) {
const int rows = 3;
const int cols = 3;
int result[rows][cols] = {0};
// Multiply mat1 and mat2
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
for (int k = 0; k < cols; ++k) {
result[i][j] += mat1.data[i][k] * mat2.data[k][j];
}
}
}
// Display the result
cout << "Result of Matrix-MATRIX multiplication:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}
}
int main() {
Matrix mat1;
MATRIX mat2;
// Input matrices
mat1.input();
mat2.input();
// Display matrices
cout << "Matrix 1:" << endl;
mat1.display();
cout << "MATRIX 2:" << endl;
mat2.display();
// Perform multiplication
multiply(mat1, mat2);
return 0;
}