diff options
| Commit | f51185d90bfe76d1fef272fe449c1d3d3bb89528 (patch) | |
|---|---|---|
| Author | 2021-11-03 21:15:51 +0200 | |
| Committer | 2021-11-03 21:15:51 +0200 | |
| Files | 4c291c81d43cf6305cd22777bc33efc6b580c4ad /Sem. 03 | |
| Introduction_to_programming_FMI-sem03-solutions.tar Introduction_to_programming_FMI-sem03-solutions.tar.gz Introduction_to_programming_FMI-sem03-solutions.zip | ||
| Parent | 72eb06dcc769114c7dbdcf7092e218b58d2196bf (diff) | |
Made some minor formatting and style changes to improve readability sem03-solutions
11 files changed, 27 insertions, 19 deletions
| -rw-r--r-- | Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp | 3 | ||||
| -rw-r--r-- | Sem. 03/Solutions/03. Remove_Middle_Digits.cpp | 7 | ||||
| -rw-r--r-- | Sem. 03/Solutions/04. Primes_In_Interval.cpp | 1 | ||||
| -rw-r--r-- | Sem. 03/Solutions/08. Triangle.cpp | 2 | ||||
| -rw-r--r-- | Sem. 03/Solutions/09. Lefthand_Triangle.cpp | 6 | ||||
| -rw-r--r-- | Sem. 03/Solutions/10. Spiral_Numbers.cpp | 4 | ||||
| -rw-r--r-- | Sem. 03/Solutions/11. Spiral_Numbers_2.cpp | 2 | ||||
| -rw-r--r-- | Sem. 03/Solutions/12. Frame.cpp | 2 | ||||
| -rw-r--r-- | Sem. 03/Solutions/13. Dumbbell.cpp | 8 | ||||
| -rw-r--r-- | Sem. 03/Solutions/14. Tree.cpp | 2 | ||||
| -rw-r--r-- | Sem. 03/Solutions/15. Bowtie.cpp | 9 |
diff --git a/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp b/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp index 75af7ea..62d01ce 100644 --- a/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp +++ b/Sem. 03/Solutions/02. Sorted_Common_Divisors.cpp @@ -7,8 +7,7 @@ int main() int n, k; cin >> n >> k; - // i = smaller number - for (int i = (n < k) ? n : k; i > 0; i--) + for (int i = ((n < k) ? n : k); i > 0; i--) { if (n % i == 0 && k % i == 0) cout << i << " "; diff --git a/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp b/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp index c22a028..7e7be68 100644 --- a/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp +++ b/Sem. 03/Solutions/03. Remove_Middle_Digits.cpp @@ -11,13 +11,12 @@ int main() for (int i = n; i != 0; i /= 10) digits++; - int digitsToTakeFromEachEnd = digits / 2 + (digits % 2 - 1); - int tenToTheHalf = 1; // ten to the power of digits to take from each end + int tenToTheHalf = 1; // 10 to the power of digits to take from each end (calculated in the loop below) - for (int i = 0; i < digitsToTakeFromEachEnd; i++) + for (int endDigits = digits / 2 + (digits % 2 - 1); endDigits > 0; endDigits--) tenToTheHalf *= 10; - int tenToTheMiddle = (digits % 2 == 0) ? 100 : 10; // ten to the power of digits to remove from the middle + int tenToTheMiddle = (digits % 2 == 0) ? 100 : 10; // 10 to the power of digits to remove from the middle int resultNumber = (n % tenToTheHalf) + (n / tenToTheHalf / tenToTheMiddle) * tenToTheHalf; diff --git a/Sem. 03/Solutions/04. Primes_In_Interval.cpp b/Sem. 03/Solutions/04. Primes_In_Interval.cpp index a1510da..6787e1f 100644 --- a/Sem. 03/Solutions/04. Primes_In_Interval.cpp +++ b/Sem. 03/Solutions/04. Primes_In_Interval.cpp @@ -14,6 +14,7 @@ int main() { bool isPrime = true; int rootOfI = sqrt(i); + for (int j = 2; j <= rootOfI; j++) { if (i % j == 0) diff --git a/Sem. 03/Solutions/08. Triangle.cpp b/Sem. 03/Solutions/08. Triangle.cpp index fa2b501..ad6764b 100644 --- a/Sem. 03/Solutions/08. Triangle.cpp +++ b/Sem. 03/Solutions/08. Triangle.cpp @@ -9,7 +9,7 @@ int main() for (int row = 1; row <= n; row++) { - for (int col = 0; col < row; col++) + for (int col = 1; 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 1aba6c7..f59ed96 100644 --- a/Sem. 03/Solutions/09. Lefthand_Triangle.cpp +++ b/Sem. 03/Solutions/09. Lefthand_Triangle.cpp @@ -14,11 +14,11 @@ int main() for (int col = absRow; col > 0; col--) cout << " "; - int numberOnLine = 0; + int lineEndNum = 0; for (int col = n - absRow; col > 0; col--) - (numberOnLine *= 10) += col; + (lineEndNum *= 10) += col; - cout << numberOnLine << endl; + cout << lineEndNum << endl; } } diff --git a/Sem. 03/Solutions/10. Spiral_Numbers.cpp b/Sem. 03/Solutions/10. Spiral_Numbers.cpp index 66e5e70..15ab136 100644 --- a/Sem. 03/Solutions/10. Spiral_Numbers.cpp +++ b/Sem. 03/Solutions/10. Spiral_Numbers.cpp @@ -10,6 +10,7 @@ int main() for (int row = 0; row < n; row++) { int previous = row + 1; + for (int col = 0; col < n; col++) { if (col == 0) @@ -17,8 +18,9 @@ int main() else if (col % 2 == 0) 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 b7337a4..5dc5d5b 100644 --- a/Sem. 03/Solutions/11. Spiral_Numbers_2.cpp +++ b/Sem. 03/Solutions/11. Spiral_Numbers_2.cpp @@ -11,7 +11,7 @@ int main() { int modifier = 1; - // When going from right to left, make previous be the number at the end of the line + 1, and then decrement + // When going from right to left, previous = number at the end of the line + 1, and make further number operations decrement if (row % 2 == 1) { previous += n + 1; diff --git a/Sem. 03/Solutions/12. Frame.cpp b/Sem. 03/Solutions/12. Frame.cpp index 6eca5a2..9a5d43c 100644 --- a/Sem. 03/Solutions/12. Frame.cpp +++ b/Sem. 03/Solutions/12. Frame.cpp @@ -9,7 +9,7 @@ int main() for (int row = 0; row < n; row++) { - bool onFirstOrLastLine = (row == 0 || row == n-1); + bool onFirstOrLastLine = (row == 0 || row == n - 1); cout << ((onFirstOrLastLine) ? "+ " : "| "); diff --git a/Sem. 03/Solutions/13. Dumbbell.cpp b/Sem. 03/Solutions/13. Dumbbell.cpp index 7db650a..e0afd63 100644 --- a/Sem. 03/Solutions/13. Dumbbell.cpp +++ b/Sem. 03/Solutions/13. Dumbbell.cpp @@ -14,12 +14,16 @@ int main() if (row == 0 || row == n - 1) { for (int col = 0; col < rowWidth; col++) - cout << ((col >= n && col <= rowWidth - n - 1) ? " " : "."); + cout << ((col >= n && col <= rowWidth - n - 1) ? " " : "."); // Print spaces in the middle } else { for (int col = 0; col < rowWidth; col++) - cout << (((col >= 1 && col <= n - 2) || (col >= rowWidth - n + 1 && col <= rowWidth - 2)) ? " " : "."); + { + bool inLeftHole = (col >= 1 && col <= n - 2); + bool inRightHole = (col >= rowWidth - n + 1 && col <= rowWidth - 2); + cout << ((inLeftHole || inRightHole) ? " " : "."); + } } cout << endl; } diff --git a/Sem. 03/Solutions/14. Tree.cpp b/Sem. 03/Solutions/14. Tree.cpp index 1955730..f55d47d 100644 --- a/Sem. 03/Solutions/14. Tree.cpp +++ b/Sem. 03/Solutions/14. Tree.cpp @@ -14,7 +14,7 @@ int main() cout << " "; for (int col = -row + 1; col < row; col++) - cout << row - ((col < 0) ? -col : col); + cout << row - ((col < 0) ? -col : col); // row - abs(col) cout << endl; } diff --git a/Sem. 03/Solutions/15. Bowtie.cpp b/Sem. 03/Solutions/15. Bowtie.cpp index 31ca6e7..61c5df3 100644 --- a/Sem. 03/Solutions/15. Bowtie.cpp +++ b/Sem. 03/Solutions/15. Bowtie.cpp @@ -11,11 +11,13 @@ int main() { { int absI = (i < 0) ? -i : i, diff = n - absI; - /* Numbers on the left */ + // Numbers on the left + for (int j = 1; j <= diff; j++) cout << j << " "; - /* Spaces/Line in the middle */ + // Spaces/Line in the middle + if (i == 0) middleSpacer = '-'; @@ -25,7 +27,8 @@ int main() { if (i == 0) middleSpacer = ' '; - /* Number on the right */ + // Number on the right + for (int j = diff; j > 0; j--) cout << " " << j; |
