Exercise 5 & updated README
Added Exercise 5 and translated README and added signature as well as disclaimer
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
# Mistakes
|
||||
|
||||
- The variable `j` goes into overflow which is not allowed!
|
||||
|
||||
# Fibonacci overflow check
|
||||
|
||||
## Background
|
||||
|
50
Informatik_I/Exercise_5/Task_1/README.md
Normal file
50
Informatik_I/Exercise_5/Task_1/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# Task
|
||||
|
||||
A perpetual calendar can be used to determine the weekday (Monday, ..., Sunday) of any given date. You may for example know that the Berlin wall came down on November 9, 1989, but what was the weekday? It was a Thursday. Or what is the weekday of the 1000th anniversary of the Swiss confederation, to be celebrated on August 1, 2291? It will be a Saturday. The task of this exercise is to write a program that outputs the weekday of a given input date.
|
||||
|
||||
Your program will read the date from the input. The input is given as three integer values in the following order: Day, month, year. First the program must validate the input. Pay attention to special cases of February (leap years!) also, the date must be greater or equal to the reference date. If a date is invalid output invalid date. If the date is valid, calculate the weekday of this day. This can be done by calculating how many days lie between the date in question and a reference date whose weekday is known. As reference date use Monday, 1st January 1900. Finally, output the weekday as one word in English.
|
||||
|
||||
**Approach**: The goal of this exercise is to learn the usage of functions. For that, we split the program into the following sub tasks given as function declarations. Your task is to **implement the provided functions** in the calendar.cpp file, so that they perform the action that is specified in their post condition.
|
||||
|
||||
**Important: There is a well-known mathematical function to calculate the weekday of a date. Using this function is an incorrect solution as it defeats the purpose of this exercise.**
|
||||
|
||||
```cpp
|
||||
// PRE: a year greater or equal than 1900
|
||||
// POST: returns whether that year was a leap year
|
||||
bool is_leap_year(int year);
|
||||
|
||||
// PRE: a year greater or equal than 1900
|
||||
// POST: returns the number of days in that year
|
||||
int count_days_in_year(int year);
|
||||
|
||||
// PRE: a month between 1 and 12 and a year greater or equal than 1900
|
||||
// POST: returns the number of days in the month of that year
|
||||
int count_days_in_month(int month, int year);
|
||||
|
||||
// PRE: n/a
|
||||
// POST: returns whether the given values represent a valid date
|
||||
bool is_valid_date(int day, int month, int year);
|
||||
|
||||
// PRE: the given values represent a valid date
|
||||
// POST: returns the number of days between January 1, 1900 and this date (excluding this date)
|
||||
int count_days(int day, int month, int year);
|
||||
|
||||
// PRE: the given values represent a (potentially invalid) date
|
||||
// POST: prints the weekday if the date is valid or "invalid date" otherwise.
|
||||
// Everything must be printed in lowercase.
|
||||
void print_weekday(int day, int month, int year);
|
||||
```
|
||||
|
||||
To complete the task, you have to provide the definition of the aforementioned functions.
|
||||
|
||||
The `calendar.cpp` file contains skeletons of the functions to be implemented. The `main.cpp` file contains testing functions and the main function. In the main function, a menu is printed to select the function that has to be tested.
|
||||
|
||||
**Important** the main is not editable. Required functions must be implemented in `calendar.cpp`.
|
||||
|
||||
**Additional notes:**
|
||||
|
||||
1. For function arguments that do **not** fulfill the precondition, the behavior is undefined.
|
||||
|
||||
1. There are a few opportunities here to use switch statements. Use them if they result in better readable code.
|
||||
|
||||
1. A leap year is defined as follows: It is an integer multiple of 4, except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400. (Source: [Wikipedia](https://en.wikipedia.org/wiki/Leap_year))
|
@@ -28,24 +28,24 @@ int count_days_in_year(int year) {
|
||||
// POST: returns the number of days in the month of that year
|
||||
int count_days_in_month(int month, int year) {
|
||||
if (year >= 1900 && month > 0 && month <= 12) {
|
||||
if (is_leap_year(year) == false) {
|
||||
if (is_leap_year(year) == true) {
|
||||
if (month % 2 != 0 && month != 2 && month <= 7)
|
||||
return 31;
|
||||
else if (month % 2 == 0 && month != 2 && month > 7)
|
||||
return 31;
|
||||
else if (month == 2)
|
||||
return 28;
|
||||
else
|
||||
return 30;
|
||||
} else {
|
||||
if (month % 2 == 0 && month != 2 && month <= 7)
|
||||
return 31;
|
||||
else if (month % 2 == 0 && month != 2 && month > 7)
|
||||
else if (month % 2 == 0 && month > 7)
|
||||
return 31;
|
||||
else if (month == 2)
|
||||
return 29;
|
||||
else
|
||||
return 30;
|
||||
} else {
|
||||
if (month % 2 != 0 && month != 2 && month <= 7)
|
||||
return 31;
|
||||
else if (month % 2 == 0 && month > 7)
|
||||
return 31;
|
||||
else if (month == 2)
|
||||
return 28;
|
||||
else
|
||||
return 30;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -66,7 +66,7 @@ bool is_valid_date(int day, int month, int year) {
|
||||
int count_days(int day, int month, int year) {
|
||||
int res_day = 0;
|
||||
if (is_valid_date(day, month, year) == true) {
|
||||
for (int j = 1901; j <= year; ++j) {
|
||||
for (int j = 1900; j < year; ++j) {
|
||||
res_day += count_days_in_year(j);
|
||||
}
|
||||
for (int j = 1; j < month; ++j) {
|
||||
|
8
Informatik_I/Exercise_5/Task_2/README.md
Normal file
8
Informatik_I/Exercise_5/Task_2/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
_This task is a text-based task. You do not need to write any program/C++ file: the answer should be written in functions.cpp._
|
||||
|
||||
Consider the functions implemented in `functions.cpp`. For each function, add proper pre- and post-conditions.
|
||||
|
||||
- If no pre-condition is needed, you can simply write "n/a".
|
||||
- The post-condition does not have to be a mathematical formula, e.g. it can be an informal description, but it must completely characterize the results and effects of the functions depending on the provided parameters.
|
||||
|
||||
**Note**: For the purposes of this task, you can ignore overflows.
|
28
Informatik_I/Exercise_5/Task_3/README.md
Normal file
28
Informatik_I/Exercise_5/Task_3/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
_This task is a text based task. You do not need to write any program/C++ file: the answer should be written in main.md (and might include code fragments if questions ask for them)._
|
||||
|
||||
# Task:
|
||||
|
||||
What are the problems (if any) with the following functions? Fix them and find appropriate [pre-](https://en.wikipedia.org/wiki/Precondition) and [postconditions](https://en.wikipedia.org/wiki/Postcondition).
|
||||
|
||||
1. function is_even:
|
||||
|
||||
```cpp
|
||||
|
||||
bool is_even(int i) {
|
||||
if (i % 2 == 0) return true;
|
||||
}
|
||||
```
|
||||
|
||||
1. function invert:
|
||||
|
||||
```cpp
|
||||
double invert(int x) {
|
||||
double result;
|
||||
if (x != 0) {
|
||||
result = 1.0 / x;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
**Hint**: The C++ compiler does not protect you from certain types of errors. Therefore, even if you run a program in Code Expert, it is not guaranteed that the behaviour you observe is the “real” one. We have prepared a [program tracing handout](https://lec.inf.ethz.ch/ifmp/2023/guides/tracing/intro.html) that shows how to execute a program with a pen and paper and which conditions indicate bugs in the executed program not caught by the C++ compiler.
|
64
Informatik_I/Exercise_5/Task_4/README.md
Normal file
64
Informatik_I/Exercise_5/Task_4/README.md
Normal file
@@ -0,0 +1,64 @@
|
||||
Task
|
||||
|
||||
Run-length encoding is a simple data compression technique that represents $N$ consecutive identical values $W$ (a run) by a tuple ($N,W$). This method is applied for image compression, for instance. Example:
|
||||
|
||||

|
||||
|
||||
Write a program that implements run-length encoding and decoding of a byte sequence as described above. By a byte, we mean an integer value in the range $\[ 0; 255 \]$. Use the stepwise refinement method to implement the program. **Your solution must consist of at least two functions, encode and decode. Please implement them in `run_length.cpp`.**
|
||||
|
||||
The _input_ is structured as follows:
|
||||
|
||||
1. One integer that determines whether to encode: $0$ or decode: $1$.
|
||||
1. Byte sequence to be encoded or decoded (of arbitrary length). If a value outside the range $\[ 0; 255 \]$(except $-1$) is entered, output `error` and stop the en- or decoding.
|
||||
1. Integer -1 signaling the end of the byte sequence. Any extra input should be ignored.
|
||||
|
||||
For the example above, the inputs are:
|
||||
|
||||
**Encode:**
|
||||
|
||||
```sh
|
||||
0 42 42 85 85 85 85 172 172 172 13 13 42 -1
|
||||
```
|
||||
|
||||
**Decode:**
|
||||
|
||||
```sh
|
||||
1 2 42 4 85 3 172 2 13 1 42 -1
|
||||
```
|
||||
|
||||
_The output_ is expected on a single line in the following format:
|
||||
|
||||
1. A start value to indicate the begin of the sequence: either $0$ for decoded values or $1$ for encoded values.
|
||||
1. The values that make up the encoded or decoded byte sequence.
|
||||
1. The value $-1$ to indicate the end of the sequence.
|
||||
|
||||
I.e., you can 'reuse' the output as the input.
|
||||
|
||||
**Note 1):** As the encoded sequence must be a _byte_ sequence, runs of length 256 or longer need to be split into multiple runs of length 255 at most.
|
||||
|
||||
**Note 2):** The first input element (the integer that determines wether to encode or decode), is already consumed by the `main`, that calls either the encode or decode function.
|
||||
|
||||
**Note 3):** Your output should not be followed by new line (i.e., do not use `std::endl` or `\n` at the end of your printout)
|
||||
|
||||
**Note 4):** The program will print your output (the result of the decoding or encoding), surrounded by asterisks. You don't have to worry about them, the autograder can safely recognize your solution
|
||||
|
||||
**Note 5):** Output only what is strictly required (the encoded or decoded sequence). The autograder will only accept output that exactly matches the expected result. For all other messages, use `std::cerr` as in:
|
||||
|
||||
```cpp
|
||||
std::cerr << "This is a test message\n"
|
||||
```
|
||||
|
||||
Those will be ignored by the autograder.
|
||||
|
||||
**Special cases**: While decoding a byte sequence two special cases can occur. These must be handled as follows:
|
||||
|
||||
1. If a byte sequence ends in the middle of a tuple, stop printing the output of en- or decoding and output `error`.
|
||||
1. Tuples of run-length 0 are possible. For such tuples, output only the leading indicator ($0$/$1$) and the trailing $-1$.
|
||||
|
||||
**Hint**: You can enter multiple numbers at once separated with a space on the console, e.g, you can copy and paste the above examples, and sequentially read them using multiple `std::cin >> _var_` calls.
|
||||
|
||||
Also note that, even though the program's output and the user input are both shown in the same console, they are processed separately internally, so you do not have to worry that the two will mix: if your program outputs something to the console before reading another value, it will only read the user input and not the previous output value. See the Calculator code in the Lecture 4 handout for an example of reading input and producing output in a loop.
|
||||
|
||||
Finally, part of the correctness for this task is the termination of your code, so pay attention to this. The autograder may not catch these kinds of mistakes.
|
||||
|
||||
**Restrictions**: using a vector, an array or a similar data structure in any part of your code is not allowed and would result in 0 points.
|
BIN
Informatik_I/Exercise_5/Task_4/pictures/encode_decode.png
Normal file
BIN
Informatik_I/Exercise_5/Task_4/pictures/encode_decode.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
14
README.md
14
README.md
@@ -1,5 +1,15 @@
|
||||
# ETH Informatik Projekte
|
||||
|
||||
Hier werden die Informatik Projekte der ETH vom D-ITET Jahrgang 2024 gemanged. Sie können für Inspirationen genutzt werden.
|
||||
In this respository the computer science project of the ETH of D-ITET 2024 are managed. They can be used for inspiration.
|
||||
|
||||
Die Projekte werden auf [Code Expert](https://expert.ethz.ch/enrolled/AS24/itet0/exercises) hochgeladen.
|
||||
The projects are uploaded on [Code Expert](https://expert.ethz.ch/enrolled/AS24/itet0/exercises).
|
||||
|
||||
## DISCLAIMER!!!
|
||||
|
||||
I assume no liability for possible errors in the code (it certainly has a few in it, since I write it myself).
|
||||
|
||||
Bugs can be reported via Discord, WhatsApp, Mail and Moodle.
|
||||
|
||||
---
|
||||
|
||||
Made by JirR02 in Switzerland 🇨🇭
|
||||
|
Reference in New Issue
Block a user