Ex W6 |home |print view |recent changes |changed October 25, 2017 | exact |You are 3.237.186.116 <- set your identity!

## 0 Catch Up^

Chances are high that you haven't been able to solve all exercises from last week. So first complete the remaining non-extra exercises from ExW4

## 1 Date with enum for Month and Weekday^

The class Date from the lecture examples (chose one you like to start with) is confusing to use, because we specify day, month and year as numbers. Change/implement a Date class that uses an enum type for month consistently, so that one can not specify a wrong date as easily.

• Can you provide overloads of Date's constructors to allow writing `Date{1, Nov, 2012}` and `Date{Nov, 1, 2012}` ?

Implement a member function `day_of_week()` for your date class that calculates if a given date is Monday, Tuesday etc. Define a corresponding enum type within the Date class.

• Should this be an `enum class` or is an `enum` sufficient?

Consult wikipedia to figure out how to calculate the day of the week with your calendar. Note, that we only support Gregorian dates.

Maybe another internal representation than 3 numbers for day-month-year, like the number of days since the start of your calendar makes it easier to implement this feature. Even though this might require change to other member functions, but your test cases should help you keep those working. Try also to implement this more compact date representation.

## 2 Simulated Switch with toggle button^

To exercise an enum type with hidden values we implement a class Switch. The Switch simulates three states: `off`, `on`, `blinking`. It comes with a single button simulated with the member function `pressButton()` that switches from off to on to blinking and then to off again. Do not expose the enum values in your `Switch.h` file.

## 3 Finite Field Modulo 5^

Take the `Ring5` class from the github and modify it to a class `Field5`. We will use it to implement a finite field (galois field/endlicher Körper).

• provide subtraction as inverse of addition.
• can it be mapped directly to integer subtraction?
• `a - a = 0`
• `3 - 4 = 4` <=> `4 + 4 = 3`
• provide unary minus operator for negation
• `a + (-a) = 0`
• provide division as inverse of multiplication.
• first figure out for each `x` (excluding `0`), what `y` provides `1 / x = y` -> `1 = x * y`
• Does it make sense to provide relational operators for `Field5`?

### Simplification with Boost^

Remove all operator implementations you can replace with Boost operators.

#### Installing Boost (if you are not using the VM)^

• On Linux (Ubuntu) fire up a terminal and type the following command:
``` \$ sudo apt-get install libboost-all-dev
```
• If you are using another Linux distribution you are expected to be familiar its package manager and able to install boost yourself.
• On a Mac, when you've got homebrew installed, use
``` \$ brew install boost
```
• Copy the `boost` directory to `<mingw-installation>/mingw32/i686-w64-mingw32/include`

## Optional Exercises^

### Code Reading Example from {cvu} (Code Critique 106)^

This is a code reading exercise that contains various C++ features you should be familiar with:

• Classes (Week 5)
• Enumerations (Week 6)
• I/O with Streams (Week 2)
• Exceptions (Week 4)

You can ignore that some features of C++ are used that your are not familiar with yet:

• `constexpr` Keyword
• `static_assert`

The magazine {cvu} has a column about C++ code that does not what it is supposed to do. You should have a look at the submission on page 19 of the September 2017 issue: https://accu.org/var/uploads/journals/CVu29-4_with_cover.pdf

Quick summary: The code is supposed to count different kinds of meals attendees of an event have ordered. Possibilities are any combination of `breakfast`, `lunch` and `dinner`. The programm should print the total number of attendess and the total number of each meal ordered. But somehow the result is not as expected.

To make finding the error easier for you we have created a CUTE executable project with some test cases for the code. It is an archive of a project that can be imported via the menu File->Import...->General->Existing Projects into Workspace. The archive can be found on the Skripte server (only available at HSR internally or VPN): https://skripte.hsr.ch/Informatik/Fachbereich/C++/CPl/Exercises/W06%20CVU%20Code%20Critique/meals.zip

• Can you find and fix the error just by reading the code?
• Does it help to have the unit tests we provide in the project?

### Ring Modulo 6^

Implement a class Ring6 that implements modulo arithmetic for unsigned integers modulo 6.

• Start out with corresponding test cases, before you implement an operation.
• Provide addition and multiplication operators.
• Provide output operator to a stream.
• Provide inward and outward conversion from unsigned integers.
• When is it useful to make these conversions explicit?
• Is it useful to implement subtraction for Ring6?
• Is it useful/possible to implement division for Ring6?

 |home |print view |recent changes |changed October 25, 2017 | exact |You are 3.237.186.116 <- set your identity! Ex W6