From 76e44a531f104795e207978c4a150720b617bae8 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 29 Oct 2021 10:03:25 +0300 Subject: Renamed i and j loop variables to row and col where applicable. Added some comments. --- Sem. 03/Solutions/08. Triangle.cpp | 4 ++-- Sem. 03/Solutions/09. Lefthand_Triangle.cpp | 16 +++++++++------- Sem. 03/Solutions/10. Spiral_Numbers.cpp | 4 ++-- Sem. 03/Solutions/11. Spiral_Numbers_2.cpp | 14 +++++++++----- Sem. 03/Solutions/12. Frame.cpp | 6 +++--- Sem. 03/Solutions/14. Tree.cpp | 17 ++++++++++------- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Sem. 03/Solutions/08. Triangle.cpp b/Sem. 03/Solutions/08. Triangle.cpp index ca25b0e..fa2b501 100644 --- a/Sem. 03/Solutions/08. Triangle.cpp +++ b/Sem. 03/Solutions/08. Triangle.cpp @@ -7,9 +7,9 @@ int main() int n; cin >> n; - for (int i = 1; i <= n; i++) + for (int row = 1; row <= n; row++) { - for (int j = 0; j < i; j++) + for (int col = 0; col < row; col++) cout << "$ "; cout << endl; diff --git a/Sem. 03/Solutions/09. Lefthand_Triangle.cpp b/Sem. 03/Solutions/09. Lefthand_Triangle.cpp index 183aea8..1aba6c7 100644 --- a/Sem. 03/Solutions/09. Lefthand_Triangle.cpp +++ b/Sem. 03/Solutions/09. Lefthand_Triangle.cpp @@ -7,16 +7,18 @@ int main() int n; cin >> n; - for (int i = -n + 1; i < n; i++) + for (int row = -n + 1; row < n; row++) { - int absI = ((i > 0) ? i : -i), number = 0; + int absRow = ((row > 0) ? row : -row); - for (int j = n - absI; j > 0; j--) - (number *= 10) += j; - - for (int j = absI; j > 0; j--) + for (int col = absRow; col > 0; col--) cout << " "; - cout << number << endl; + int numberOnLine = 0; + + for (int col = n - absRow; col > 0; col--) + (numberOnLine *= 10) += col; + + cout << numberOnLine << endl; } } diff --git a/Sem. 03/Solutions/10. Spiral_Numbers.cpp b/Sem. 03/Solutions/10. Spiral_Numbers.cpp index 5a3dced..66e5e70 100644 --- a/Sem. 03/Solutions/10. Spiral_Numbers.cpp +++ b/Sem. 03/Solutions/10. Spiral_Numbers.cpp @@ -15,9 +15,9 @@ int main() if (col == 0) cout << previous << " "; else if (col % 2 == 0) - cout << (previous += row*2 + 1) << " "; + cout << (previous += row * 2 + 1) << " "; else - cout << (previous += (n-row-1) * 2 + 1) << " "; + cout << (previous += (n -row -1) * 2 + 1) << " "; } cout << endl; } diff --git a/Sem. 03/Solutions/11. Spiral_Numbers_2.cpp b/Sem. 03/Solutions/11. Spiral_Numbers_2.cpp index f6bc2d4..b7337a4 100644 --- a/Sem. 03/Solutions/11. Spiral_Numbers_2.cpp +++ b/Sem. 03/Solutions/11. Spiral_Numbers_2.cpp @@ -7,20 +7,24 @@ int main() int n; cin >> n; - for (int i = 0, previous = 0; i < n; i++) + for (int row = 0, previous = 0; row < n; row++) { int modifier = 1; - if (i % 2 == 1) + + // When going from right to left, make previous be the number at the end of the line + 1, and then decrement + if (row % 2 == 1) { previous += n + 1; modifier = -1; } - - for (int j = 0; j < n; j++) + + for (int col = 0; col < n; col++) cout << (previous += modifier) << " "; cout << endl; - if (i % 2 == 1) + + // Account for decrementing when gong from right to left + if (row % 2 == 1) previous += n - 1; } } diff --git a/Sem. 03/Solutions/12. Frame.cpp b/Sem. 03/Solutions/12. Frame.cpp index 6347ead..a91c907 100644 --- a/Sem. 03/Solutions/12. Frame.cpp +++ b/Sem. 03/Solutions/12. Frame.cpp @@ -7,13 +7,13 @@ int main() int n; cin >> n; - for (int i = 0; i < n; i++) + for (int row = 0; row < n; row++) { - bool onFirstOrLastLine = (i == 0 || i == n-1); + bool onFirstOrLastLine = (row == 0 || row == n-1); cout << ((onFirstOrLastLine) ? "+" : "|"); - for (int j = 0; j < n - 2; j++) + for (int col = 0; col < n - 2; col++) cout << "-"; cout << ((onFirstOrLastLine) ? "+" : "|") << endl; diff --git a/Sem. 03/Solutions/14. Tree.cpp b/Sem. 03/Solutions/14. Tree.cpp index c6ee5cd..1955730 100644 --- a/Sem. 03/Solutions/14. Tree.cpp +++ b/Sem. 03/Solutions/14. Tree.cpp @@ -7,21 +7,24 @@ int main() int n; cin >> n; - for (int i = 1; i <= n; i++) + // Tree crown + for (int row = 1; row <= n; row++) { - for (int j = n - i; j > 0; j--) + for (int col = n - row; col > 0; col--) cout << " "; - for (int j = -i + 1; j < i; j++) - cout << i - ((j < 0) ? -j : j); + for (int col = -row + 1; col < row; col++) + cout << row - ((col < 0) ? -col : col); cout << endl; } - for (int i = n - 1; i > 0; i--) + + // Tree trunk + for (int row = n - 1; row > 0; row--) { - for (int j = n - 1; j > 0; j--) + for (int col = n - 1; col > 0; col--) cout << " "; - cout << i << endl; + cout << row << endl; } } -- cgit v1.3.1-24-gd84d