-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixTriangularSwap
More file actions
97 lines (65 loc) · 1.61 KB
/
MatrixTriangularSwap
File metadata and controls
97 lines (65 loc) · 1.61 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
#include <iostream>
using namespace std;
int** AllocateMemory(int& rows, int& cols) {
int** matrix;
matrix = new int* [rows];
for (int i = 0; i < rows; i++)
matrix[i] = new int[cols];
return matrix;
}
void InputMatrix(int** matrix, const int rows, const int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter elements for matrix m[" << i << "][" << j << "] :";
cin >> matrix[i][j];
}
}
}
void DisplayMatrix(int** matrix, const int& rows, const int& cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void swapTriangular(int** matrix, const int& rows, const int& cols) {
if (rows == cols) {
for (int i = 0; i < rows; i++) {
int temp;
for (int j = 0; j < cols / 2; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[i][cols - j - 1];
matrix[i][cols - j - 1] = temp;
}
}
for (int i = 0, j = rows - i - 1; i < rows / 2; i++, j--) {
int tem;
for (int k = 0; k < cols; k++) {
tem = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = tem;
}
}
}
else {
return;
}
}
int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
//Allocate memory for matrix
int** matrix = AllocateMemory(rows, cols);
// Input matrix elements from the user
InputMatrix(matrix, rows, cols);
// Display the input matrix
DisplayMatrix(matrix, rows, cols);
cout << "\n\n";
swapTriangular(matrix, rows, cols);
DisplayMatrix(matrix, rows, cols);
return 0;
}