Skip to content

Commit 60b5d71

Browse files
committed
Address code review comments:
- add curly bracket - remove static_cast - remove IIFE - fix format for the matrix - format output with tabs - properly sanitize user input - remove extraneous assignment - fix off-by-1
1 parent 8394ead commit 60b5d71

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

contents/gaussian_elimination/code/c++/gaussian_elimination.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cmath>
44
#include <iostream>
55
#include <vector>
6+
#include <iomanip>
67

78
void gaussian_elimination(std::vector<double>& a, int cols) {
89
assert(a.size() % cols == 0);
@@ -13,16 +14,12 @@ void gaussian_elimination(std::vector<double>& a, int cols) {
1314
// Main loop going through all columns
1415
for (int col = 0; col < cols - 1; ++col) {
1516
// Step 1: finding the maximum element for each column
16-
int max_index = [&]() {
17-
int res = row;
18-
int max_element = a[col + row * cols];
19-
for (int r = row + 1; r < rows; ++r)
20-
if (max_element < std::abs(a[col + r * cols])) {
21-
max_element = std::abs(a[col + r * cols]);
22-
res = r;
23-
}
24-
return res;
25-
}();
17+
int max_index = row;
18+
for (int r = row + 1; r < rows; ++r) {
19+
if (a[col + max_index * cols] < std::abs(a[col + r * cols])) {
20+
max_index = r;
21+
}
22+
}
2623

2724
// Check to make sure matrix is good!
2825
if (a[col + max_index * cols] == 0) {
@@ -61,10 +58,6 @@ std::vector<double> back_substitution(const std::vector<double>& a, int cols) {
6158
// Creating the solution Vector
6259
std::vector<double> soln(rows);
6360

64-
// initialize the final element
65-
soln[rows - 1] =
66-
a[cols - 1 + (rows - 1) * cols] / a[cols - 1 - 1 + (rows - 1) * cols];
67-
6861
for (int i = rows - 1; i >= 0; --i) {
6962
auto sum = 0.0;
7063
for (int j = cols - 2; j > i; --j) {
@@ -86,12 +79,12 @@ void gauss_jordan_elimination(std::vector<double>& a, int cols) {
8679
if (a[col + row * cols] != 0) {
8780

8881
// divide row by pivot and leaving pivot as 1
89-
for (int i = cols - 1; i >= static_cast<int>(col); --i)
82+
for (int i = cols - 1; i >= col; --i)
9083
a[i + row * cols] /= a[col + row * cols];
9184

9285
// subtract value from above row and set values above pivot to 0
93-
for (int i = 0; i < static_cast<int>(row - 1); ++i)
94-
for (int j = cols - 1; j >= static_cast<int>(col); --j)
86+
for (int i = 0; i < row; ++i)
87+
for (int j = cols - 1; j >= col; --j)
9588
a[j + i * cols] -= a[col + i * cols] * a[j + row * cols];
9689
++row;
9790
}
@@ -104,16 +97,22 @@ void print_matrix(const std::vector<double>& a, int cols) {
10497
for (int i = 0; i < rows; ++i) {
10598
std::cout << "[";
10699
for (int j = 0; j < cols; ++j) {
107-
std::cout << a[j + i * cols] << " ";
100+
std::cout << std::fixed << a[j + i * cols] << "\t";
108101
}
109102
std::cout << "]\n";
110103
}
111104
}
112105

113106
int main() {
114-
std::vector<double> a = {2, 3, 4, 6, 1, 2, 3, 4, 3, -4, 0, 10};
107+
std::vector<double> a = { 2, 3, 4, 6,
108+
1, 2, 3, 4,
109+
3, -4, 0, 10 };
115110
const int cols = 4;
116-
assert(a.size() % cols == 0);
111+
if (a.size() % cols != 0)
112+
{
113+
std::cout << "The input dimentions are incorrect\n";
114+
return 1;
115+
}
117116

118117
gaussian_elimination(a, cols);
119118
print_matrix(a, cols);

0 commit comments

Comments
 (0)