-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path08_matrix_negation.cpp
113 lines (92 loc) · 2.37 KB
/
08_matrix_negation.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
/*
Consider a class Matrix
Class Matrix
{
int matrix[3][3], rows = 3, cols = 3;
};
Overload the - (Unary) should negate the numbers stored in the object.
*/
// // Header files
#include <iostream>
#include <iomanip>
// // use namespace
using namespace std;
// // define class Matrix
class Matrix
{
private:
// // instance member variables
int matrix[3][3], rows = 3, cols = 3;
public:
// // constructors
Matrix()
{
}
// // instance member function to input matrix
void inputMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "\nEnter element[" << i << "][" << j << "] => ";
cin >> matrix[i][j];
}
}
}
// // instance member function to show matrix
void showMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << setw(4) << matrix[i][j];
cout << endl; // // Add New line
}
}
// // instance member function to set element
void setElement(int row, int col, int value)
{
if (row >= 0 && row < rows && col >= 0 && col < cols)
matrix[row][col] = value;
else
cout << "\nInvalid Index...";
}
// // instance member function to get element
int getElement(int row, int col)
{
if (row >= 0 && row < rows && col >= 0 && col < cols)
return matrix[row][col];
cout << "\nInvalid Index...";
}
// // overload unary minus (-a) operator
Matrix operator-()
{
Matrix temp;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
temp.matrix[i][j] = -matrix[i][j];
}
return temp;
}
};
// // Main Function Start
int main()
{
Matrix matrixA, matrixB; // // create objects of Matrix class
// // input elements of matrix
cout << "\n>>>>>>>> Enter Elements of Matrix-A of Order 3 x 3 <<<<<<<<<\n";
matrixA.inputMatrix();
// // show matrix
cout << "\n>>>>>>>> Matrix-A <<<<<<<<<\n";
matrixA.showMatrix();
matrixB = -matrixA;
// // show negation of m1
cout << "\n>>>>>>>> Negation of Matrix-A <<<<<<<<<\n";
matrixB.showMatrix();
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End