First Exercise

This commit is contained in:
JirR02 2025-02-20 23:51:20 +01:00
parent 2b700071aa
commit d580b385e5
27 changed files with 207 additions and 36 deletions

View File

@ -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.

View File

@ -0,0 +1,22 @@
#include <iostream>
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;
}

View File

@ -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.

View File

@ -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.

View File

@ -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 = ?
```

View File

@ -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

View File

@ -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.

View File

@ -0,0 +1,25 @@
#include <iostream>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

View File

@ -1,3 +0,0 @@
# Informatik I
In diesem Verzeichnis findest du die Projekte für Informatik I.

50
Projekt_2/README.md Normal file
View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -1,3 +0,0 @@
# Vorkurs Informatik
In diesem Verzeichnis findest du die Projekte für den Vorkurs Informatik.