diff --git a/Informatik_I/Examples/Week_1/README.md b/Informatik_I/Examples/Week_1/README.md new file mode 100644 index 0000000..a8a2828 --- /dev/null +++ b/Informatik_I/Examples/Week_1/README.md @@ -0,0 +1,13 @@ +# Task + +Write a program which reads in an integer a larger than `1000` and outputs its last three digits. For example, if `a=14325`, the output should be `3 2 5`. + +_Hint_: You need to use integer division and modulo operators. + +# Input + +An integer larger than `1000`. + +# Output + +Last three digits separated by spaces. diff --git a/Informatik_I/Examples/Week_1/main.cpp b/Informatik_I/Examples/Week_1/main.cpp new file mode 100644 index 0000000..c5fa680 --- /dev/null +++ b/Informatik_I/Examples/Week_1/main.cpp @@ -0,0 +1,22 @@ +#include + +int main() { + + int a; + std::cin >> a; // get input of for a + + if (a >= 1000) { // check if input is greater than 1000 + int b = a % 10; // get last digit + a /= 10; + int c = a % 10; // get second digit + a /= 10; + int d = a % 10; // get first digit + + std::cout << d << " " << c << " " + << b; // output first, second, and last digit + } else { // if input not greater than 1000 return 0 + return 0; + } + + return 0; +} diff --git a/Informatik_I/Exercise_1/Task_1/README.md b/Informatik_I/Exercise_1/Task_1/README.md new file mode 100644 index 0000000..12710bb --- /dev/null +++ b/Informatik_I/Exercise_1/Task_1/README.md @@ -0,0 +1,17 @@ +# Task + +Let `a`, `b`, `c`, and `d` be variables of type `int`. + +- Which of the following character sequences are valid expressions in the sense that they are accepted by a C++ Compiler? Explain your answer. + +1. a = b = 5 +1. 1 = a +1. ++a + b++ +1. a + b = c + d +1. a = 2 b + +Assume that all the variables have been defined and correctly initialized and that all expressions are completely independent from each other. + +- For each of the expressions that you have identified as valid, decide whether the entire expression is an `lvalue` or an `rvalue`, and explain your decision. + +- Determine the values of the expressions that you have identified as valid and explain how these values are obtained. diff --git a/Informatik_I/Exercise_1/Task_1/main.md b/Informatik_I/Exercise_1/Task_1/main.md new file mode 100644 index 0000000..8ccd845 --- /dev/null +++ b/Informatik_I/Exercise_1/Task_1/main.md @@ -0,0 +1,23 @@ +# Task 1 + +--- + +### Expression 1: `a = b = 5` + +The expression is valid and will be accepted by the CPP compiler. It will result with the l-values `a = 5` and `b = 5`. This is due to the fact the the `=` opperation is read from right to left so `b = 5` will run first and `a = b` afterwards. + +### Expression 2: `1 = a` + +The expression is invalid. + +### Expression 3: `++a + b++` + +The expression is valid and will be accepted by the CPP compiler. It will result in a r-value since the result is not assigned to a variable. + +### Expression 4: `a + b = c + d` + +The expression is invalid. + +### Expression 5: `a = 2 b` + +The expression is valid and will be accepted by the CPP compiler. It will result with the l-value `a = 2 b`. This is valid since a l-value can be a r-value. diff --git a/Informatik_I/Exercise_1/Task_2/README.md b/Informatik_I/Exercise_1/Task_2/README.md new file mode 100644 index 0000000..50ee818 --- /dev/null +++ b/Informatik_I/Exercise_1/Task_2/README.md @@ -0,0 +1,24 @@ +# Task + +_This task is a text-based task but mostly automatically checked. You are required to write your answers into submission.txt by replacing the question marks with the correct solution. Please, do not change the line formating._ + +_You can check whether your solution is correct by clicking on the test button._ + +Numbers can be provided in various formats in C++. Literals prefixed with `0b` indicate binary encoding. Assume unsigned arithmetics with sufficient numbers of bits, i.e. no overflows. Convert the following binary numbers into decimal numbers (1-4) and decimal numbers to binary (5-8): + +```txt +# Lines starting with # are comments. Spaces is ignored. +# Lines starting with a whitespace before # will not be a comment. + +# Convert to decimal: +0b1 = ? +0b10 = ? +0b000001 = ? +0b101010 = ? + +# Convert to binary: +7 = ? +11 = ? +28 = ? +1024 = ? +``` diff --git a/Informatik_I/Exercise_1/Task_2/submission.txt b/Informatik_I/Exercise_1/Task_2/submission.txt new file mode 100644 index 0000000..3769208 --- /dev/null +++ b/Informatik_I/Exercise_1/Task_2/submission.txt @@ -0,0 +1,14 @@ +# Lines starting with # are comments. Spaces are ignored. +# Lines starting with a whitespace before # will not be a comment. + +# Convert to decimal: +0b1 = 1 +0b10 = 2 +0b000001 = 1 +0b101010 = 42 + +# Convert to binary: +7 = 0b111 +11 = 0b1011 +28 = 0b11100 +1024 = 0b10000000000 \ No newline at end of file diff --git a/Informatik_I/Exercise_1/Task_3/README.md b/Informatik_I/Exercise_1/Task_3/README.md new file mode 100644 index 0000000..ccc598c --- /dev/null +++ b/Informatik_I/Exercise_1/Task_3/README.md @@ -0,0 +1,18 @@ +# Task + +Write a program resistance.cpp that computes the equivalent resistance of the following wiring: + +![circuit](./resistance.png) + +We assume that $R_1$, $R_2$, $R_3$, and $R_4$ have an integer valued resistance. After input of the four values, the program should output the result arithmetically rounded to the nearest integer. + +**Remark:** In order to facilitate the task, you may want to: + +- Conceptually divide the task into sub tasks. For example, start with computation of serial resistors $R_{12}$ and $R_{34}$. +- Solve the task first naively using default rounding and then think about how to accomplish arithmetic rounding. Recall that $\text{round}(x) = [x + 0.5] \text{ }\forall \text{ } x \in \mathbb{R}$, i.e., a real number can be rounded arithmetically by adding $0.5$ to it and then rounding down. For example, $\text{round}(8,6) = [8.6 + 0.5] = [9.1] = 9$. + +You can find formulas for computing the total resistance in this [Wikipedia article](https://en.wikipedia.org/wiki/Resistor#Series_and_parallel_resistors). + +**Important:** using anything other than int (e.g., floating point numbers, long, or double long) is forbidden. + +**Important:** using if-else and any other branches is forbidden. diff --git a/Informatik_I/Exercise_1/Task_3/resistance.cpp b/Informatik_I/Exercise_1/Task_3/resistance.cpp new file mode 100644 index 0000000..ad71bbd --- /dev/null +++ b/Informatik_I/Exercise_1/Task_3/resistance.cpp @@ -0,0 +1,25 @@ +#include + +int main() { + int r1; + int r2; + int r3; + int r4; + int res; + + std::cin >> r1; + std::cin >> r2; + std::cin >> r3; + std::cin >> r4; + + int r12 = r1 + r2; + int r34 = r3 + r4; + + res = (r12 * r34 + ((r12 + r34) / 2)) / + (r12 + r34); // By adding the average of r12 and r34, the result is + // increased enough to get the correct rounded number + + std::cout << res; + + return 0; +} diff --git a/Informatik_I/Exercise_1/Task_3/resistance.png b/Informatik_I/Exercise_1/Task_3/resistance.png new file mode 100644 index 0000000..65819fd Binary files /dev/null and b/Informatik_I/Exercise_1/Task_3/resistance.png differ diff --git a/Informatik_I/README.md b/Informatik_I/README.md deleted file mode 100644 index 3210648..0000000 --- a/Informatik_I/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Informatik I - -In diesem Verzeichnis findest du die Projekte für Informatik I. diff --git a/Vorkurs/Projekt_1/Task_1/guess_a_number.cpp b/Projekt_1/Task_1/guess_a_number.cpp similarity index 100% rename from Vorkurs/Projekt_1/Task_1/guess_a_number.cpp rename to Projekt_1/Task_1/guess_a_number.cpp diff --git a/Vorkurs/Projekt_1/Task_1/guess_a_number.h b/Projekt_1/Task_1/guess_a_number.h similarity index 100% rename from Vorkurs/Projekt_1/Task_1/guess_a_number.h rename to Projekt_1/Task_1/guess_a_number.h diff --git a/Vorkurs/Projekt_1/Task_1/main.cpp b/Projekt_1/Task_1/main.cpp similarity index 100% rename from Vorkurs/Projekt_1/Task_1/main.cpp rename to Projekt_1/Task_1/main.cpp diff --git a/Vorkurs/Projekt_1/Task_2/guess_a_number.cpp b/Projekt_1/Task_2/guess_a_number.cpp similarity index 100% rename from Vorkurs/Projekt_1/Task_2/guess_a_number.cpp rename to Projekt_1/Task_2/guess_a_number.cpp diff --git a/Vorkurs/Projekt_1/Task_2/guess_a_number.h b/Projekt_1/Task_2/guess_a_number.h similarity index 100% rename from Vorkurs/Projekt_1/Task_2/guess_a_number.h rename to Projekt_1/Task_2/guess_a_number.h diff --git a/Vorkurs/Projekt_1/Task_2/main.cpp b/Projekt_1/Task_2/main.cpp similarity index 100% rename from Vorkurs/Projekt_1/Task_2/main.cpp rename to Projekt_1/Task_2/main.cpp diff --git a/Projekt_2/README.md b/Projekt_2/README.md new file mode 100644 index 0000000..6b2170c --- /dev/null +++ b/Projekt_2/README.md @@ -0,0 +1,50 @@ +# Projekt 2: Hangman + +## Layers + +### Start (Optional) + +Beim Starten des Spiels soll ein Welcome screen erscheinen mit den Optionen, das Spiel zu starten und das Spiel zu beenden. Damit der Welcome screen gut aussieht soll es Terminal Art beinhalten. Nach dem Start Vorgang wird der Anzahl der Spieler gefragt. +Daraus entstehen 2 Szenarien: + +1. Falls es im Einzelspieler Modus ist, wird ein Wort aus der Liste ausgesucht. +1. Falls im Mehrspieler Modus, darf der andere Spieler ein Wort zum Raten auswählen. + +- [ ] Start input +- [ ] End input +- [ ] Invalid Input +- [ ] Single or Multiplayer +- [ ] Terminal Art + +### Game + +Das zu ratende Wort wird verdeckt im Terminal gezeigt. Es wird dann ein Input als Buchstabe verlangt. Wenn der Input zu lang oder ein invalid character ist, wird der Spieler nochmals dazu aufgefordert, ein Buchstabe einzugeben. +Daraus entstehen 2 Szenarien: + +1. Ist der Buchstabe in der Zahl enthalten, wird der Buchstabe aufgedeckt und ein positiver Satz erscheint im Terminal. +1. Ist der Buchstabe falsch, so wird ein Leben abgezogen und ein negativer Satz wird ausgespuckt. + +Während des ganzen Spiels wird der Terminal Art aktualisiert. + +- [ ] Wort verdeckt im Terminal anzeigen +- [ ] Input von einem Buchstaben verlangen +- [ ] Input kontrollieren +- [ ] Buchstabe kontrollieren +- [ ] Positiver Satz +- [ ] Buchstabe aufdecken +- [ ] Negativer Satz +- [ ] (Optional) Terminal Art + +### End + +Es entstehen daraus zwei Endszenarien: + +1. Wurden alle Buchstaben eraten, so wird ein Gewinner Satz ausgesprochen und gefragt ob das Spiel neugestartet werden soll. +1. Wurden alle Versuche verbraucht, so wird ein verlierer Satz ausgesprochen, das Wort aufgelöst und gefragt, ob das Spiel neugestartet werden soll. + +Falls das Programm geschlossen wird, wird ein Abschiedssatz gezeigt. + +- [ ] Gewinner Satz +- [ ] Verlierer Satz +- [ ] (Optional) Fragen für eine neue Runde +- [ ] Abschiedssatz diff --git a/Vorkurs/Projekt_2/hangman.cpp b/Projekt_2/hangman.cpp similarity index 100% rename from Vorkurs/Projekt_2/hangman.cpp rename to Projekt_2/hangman.cpp diff --git a/Vorkurs/Projekt_2/hangman.h b/Projekt_2/hangman.h similarity index 100% rename from Vorkurs/Projekt_2/hangman.h rename to Projekt_2/hangman.h diff --git a/Vorkurs/Projekt_2/main.cpp b/Projekt_2/main.cpp similarity index 100% rename from Vorkurs/Projekt_2/main.cpp rename to Projekt_2/main.cpp diff --git a/Vorkurs/Projekt_2/termcolor.h b/Projekt_2/termcolor.h similarity index 100% rename from Vorkurs/Projekt_2/termcolor.h rename to Projekt_2/termcolor.h diff --git a/Vorkurs/Projekt_2/words.csv b/Projekt_2/words.csv similarity index 100% rename from Vorkurs/Projekt_2/words.csv rename to Projekt_2/words.csv diff --git a/README.md b/README.md index 3ad5d09..2e19f40 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ Hier werden die Informatik Projekte der ETH vom D-ITET Jahrgang 2024 gemanged. Sie können für Inspirationen genutzt werden. -Die Projekte werden auf [Code Expert](https://expert.ethz.ch) hochgeladen. +Die Projekte werden auf [Code Expert](https://expert.ethz.ch/enrolled/AS24/itet0/exercises) hochgeladen. diff --git a/Vorkurs/Projekt_1/Task_1/README.md b/Vorkurs/Projekt_1/Task_1/README.md deleted file mode 100644 index 898b40d..0000000 --- a/Vorkurs/Projekt_1/Task_1/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Guess A Number (Task 1) - -## Project Overview - -The goal of the first project is to program a simple number guessing game: the player needs to correctly guess a number, chosen from an interval, with at most guesses. - -In the lecture, a first version of the game was presented, in which the player had only one chance of guessing the correct number. In order to implement the full game, you will have to extend this version by allowing the player to guess up to times. - -The first project consists of two tasks: task 1 (this task) is to reimplement the first version of the game that was presented in the lecture, task 2 is to implement the full game. diff --git a/Vorkurs/Projekt_1/Task_2/README.md b/Vorkurs/Projekt_1/Task_2/README.md deleted file mode 100644 index e173e41..0000000 --- a/Vorkurs/Projekt_1/Task_2/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Guess A Number (Task 2) - -## Project Overview - -The goal of the first project is to program a simple number guessing game: the player needs to correctly guess a number, chosen from an interval, with at most guesses. - -In the lecture, a first version of the game was presented, in which the player had only one chance of guessing the correct number. In order to implement the full game, you will have to extend this version by allowing the player to guess up to times. - -The first project consists of two tasks: task 1 was to reimplement the first version of the game that was presented in the lecture, task 2 (this task) is to implement the full game. diff --git a/Vorkurs/Projekt_2/README.md b/Vorkurs/Projekt_2/README.md deleted file mode 100644 index b51aab2..0000000 --- a/Vorkurs/Projekt_2/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Project 2 - -## Project Overview - -The goal of this project is to implement a version of the popular guessing game known as Hangman (German: Galgenmännchen). The rules of the game are simple: the first player picks a word which the second player has to guess character by character — with a limited amount of guesses. - -For your version, we focus on the essence of the game: - -1. Choosing a word and initialising the game -1. Repeatedly guessing characters, and uncovering them if they occur in the chosen word -1. Eventually announcing that the player won ... or lost diff --git a/Vorkurs/README.md b/Vorkurs/README.md deleted file mode 100644 index 56878b6..0000000 --- a/Vorkurs/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Vorkurs Informatik - -In diesem Verzeichnis findest du die Projekte für den Vorkurs Informatik.