-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecondPart.cpp
97 lines (69 loc) · 2.21 KB
/
SecondPart.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
#include <iostream>
#include <vector>
using namespace std;
void createMatrixB(const vector<vector<int>>& matrixA, vector<vector<int>>& matrixB) {
int rows = matrixA.size();
matrixB = matrixA;
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < rows; j++) {
matrixB[i][j] = 0;
matrixB[rows - i - 1][j] = 0;
}
}
}
void compareAndModifyDiagonals(vector<vector<int>>& matrixA, vector<vector<int>>& matrixB) {
int rows = matrixA.size();
for (int i = 0; i < rows; i++) {
if (matrixA[i][i] == matrixB[i][i]) {
if (i + 1 < rows && i + 1 < matrixA[0].size()) {
matrixA[i][i] = matrixA[i + 1][i];
}
int rowSum = 0;
for (int j = 0; j < rows; j++) {
rowSum += matrixB[i][j];
}
matrixB[i][i] = rowSum;
}
}
}
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for Matrix A: ";
cin >> rows >> cols;
if (rows != cols) {
cout << "Matrix A must be square for well-defined diagonals." << endl;
return 1;
}
vector<vector<int>> matrixA(rows, vector<int>(cols));
cout << "Enter the elements for Matrix A:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrixA[i][j];
}
}
vector<vector<int>> matrixB;
createMatrixB(matrixA, matrixB);
cout << "Matrix B:" << endl;
for (const auto& row : matrixB) {
for (int element : row) {
cout << element << " ";
}
cout << endl;
}
compareAndModifyDiagonals(matrixA, matrixB);
cout << "Modified Matrix A:" << endl;
for (const auto& row : matrixA) {
for (int element : row) {
cout << element << " ";
}
cout << endl;
}
cout << "Modified Matrix B:" << endl;
for (const auto& row : matrixB) {
for (int element : row) {
cout << element << " ";
}
cout << endl;
}
return 0;
}