Skip to content

Commit

Permalink
Errata for page 136
Browse files Browse the repository at this point in the history
  • Loading branch information
markjprice committed Mar 22, 2022
1 parent e504bef commit 1df8162
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
32 changes: 32 additions & 0 deletions errata.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ If you find any mistakes in the sixth edition, *C# 10 and .NET 6 - Modern Cross-
- [Page 88 - Setting options with arguments](#page-88---setting-options-with-arguments)
- [Page 92 - Exercise 2.3 – Practice number sizes and ranges](#page-92---exercise-23--practice-number-sizes-and-ranges)
- [Page 110 - Understanding iteration statements](#page-110---understanding-iteration-statements)
- [Page 136 - Converting numbers from cardinal to ordinal](#page-136---converting-numbers-from-cardinal-to-ordinal)
- [Page 137 - Calculating factorials with recursion](#page-137---calculating-factorials-with-recursion)
- [Page 140 - Documenting functions with XML comments](#page-140---documenting-functions-with-xml-comments)
- [Page 152 - Customizing breakpoints](#page-152---customizing-breakpoints)
Expand Down Expand Up @@ -220,6 +221,37 @@ I wrote, "Iteration statements repeat a block of statements either while a condi

Some readers do not realize that the `for` statement does not iterate a specific number of times; it uses a while condition, or loops forever if the while condition is missing. To make that clearer, the sentence could be, "Iteration statements repeat a block of statements either while a condition is `true` (`while` and `for` statements) or for each item in a collection (`foreach` statement)."

## Page 136 - Converting numbers from cardinal to ordinal

> Thanks to [Felix Namutare](https://github.com/namutare) for raising this issue.
The `CardinalToOrdinal` function fails to properly convert numbers larger than 100 like 111, 112, 113. The fix is to calculate the last two digits and then switch on that value instead, as shown in the following code:
```cs
static string CardinalToOrdinal(int number)
{
int lastTwoDigits = number % 100;

switch (lastTwoDigits)
{
case 11: // special cases from 11th to 13th
case 12:
case 13:
return $"{number}th";
default:
int lastDigit = number % 10;

string suffix = lastDigit switch
{
1 => "st",
2 => "nd",
3 => "rd",
_ => "th"
};
return $"{number}{suffix}";
}
}
```

## Page 137 - Calculating factorials with recursion

The factorial function is defined for non-negative integers only i.e. for 0, 1, 2, 3, and so on, and it is defined as:
Expand Down
6 changes: 4 additions & 2 deletions vs4win/Chapter04/WritingFunctions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static decimal CalculateTax(

static void RunCardinalToOrdinal()
{
for (int number = 1; number <= 40; number++)
for (int number = 1; number <= 1040; number++)
{
Write($"{CardinalToOrdinal(number)} ");
}
Expand Down Expand Up @@ -165,7 +165,9 @@ partial class Program
/// <returns>Number as an ordinal value e.g. 1st, 2nd, 3rd, and so on.</returns>
static string CardinalToOrdinal(int number)
{
switch (number)
int lastTwoDigits = number % 100;

switch (lastTwoDigits)
{
case 11: // special cases for 11th to 13th
case 12:
Expand Down
4 changes: 3 additions & 1 deletion vscode/Chapter04/WritingFunctions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ partial class Program
/// <returns>Number as an ordinal value e.g. 1st, 2nd, 3rd, and so on.</returns>
static string CardinalToOrdinal(int number)
{
switch (number)
int lastTwoDigits = number % 100;

switch (lastTwoDigits)
{
case 11: // special cases for 11th to 13th
case 12:
Expand Down

0 comments on commit 1df8162

Please sign in to comment.