Initial commit

This commit is contained in:
Patrick 2023-01-17 09:05:46 +01:00
commit 84362719fc
Signed by: Paddi
GPG Key ID: CEF3546E095DF113
10 changed files with 3623 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.ipynb_checkpoints

1282
Blatt06.ipynb Normal file

File diff suppressed because it is too large Load Diff

502
Blatt07.ipynb Normal file

File diff suppressed because one or more lines are too long

492
Blatt08.ipynb Normal file

File diff suppressed because one or more lines are too long

404
Blatt09.ipynb Normal file

File diff suppressed because one or more lines are too long

383
Blatt10.ipynb Normal file

File diff suppressed because one or more lines are too long

520
Blatt11.ipynb Normal file
View File

@ -0,0 +1,520 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0e941e91-5c8f-4c2c-90e5-b434608a6742",
"metadata": {},
"source": [
"# C++ und Python\n",
"- Python is an interpreted language while C++ is a compiled language\n",
"- Python is dynamically typed, C++ is statically typed"
]
},
{
"cell_type": "markdown",
"id": "0b4e6bda-902e-42f1-8325-4af13c13aebc",
"metadata": {},
"source": [
"# Datentypen in C++"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "072d1611-f6d1-4d98-8afb-97b87a737c8b",
"metadata": {},
"outputs": [],
"source": [
"#include <boost/core/demangle.hpp>\n",
"#include <string>\n",
"#include <cmath>\n",
"#include <iostream>\n",
"#include <iomanip>\n",
"\n",
"template<typename T>\n",
"void print_answer(T expr) {\n",
" std::cout << std::fixed << std::setprecision(3) << \"Value: \" << expr << \"\\nType: \" << boost::core::demangle(typeid(expr).name()) << std::endl; \n",
"}"
]
},
{
"cell_type": "markdown",
"id": "9b3a4c32-7d4a-4634-b974-8e5191626e0b",
"metadata": {},
"source": [
"`3+5`\\\n",
"Value: `8`\\\n",
"Type: `int`\n",
"\n",
"Addition of two integer literals."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0ccabd5b-50c1-4552-b305-aaa5858f4d9b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 8\n",
"Type: int\n"
]
}
],
"source": [
"print_answer(3+5)"
]
},
{
"cell_type": "markdown",
"id": "eb9d0528-7eef-49db-9571-09558bc03603",
"metadata": {},
"source": [
"`3 + 5.0`\\\n",
"Value: `8.0`\\\n",
"Type: `double`\n",
"\n",
"The literal `5.0` is a `double`, addition casts the `int` as `double`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7d43a7c6-5ce5-4f3e-8f72-27ccf1f1b8bd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 8.000\n",
"Type: double\n"
]
}
],
"source": [
"print_answer(3 + 5.0);"
]
},
{
"cell_type": "markdown",
"id": "b6a51e15-2819-4d2d-801c-7281fa0b1e34",
"metadata": {},
"source": [
"`\"3\" + \"5\"`\n",
"Compiler error as `const char`s cannot be added together"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8d7dbd47-0b14-4182-bec3-ca7b4240f72f",
"metadata": {},
"outputs": [],
"source": [
"//\"3\" + \"5\" // Commented to not interrupt the jupyter notebook when running all cells"
]
},
{
"cell_type": "markdown",
"id": "016d6c54-585d-4dca-98e5-d4cece1f5136",
"metadata": {},
"source": [
"`std::string(\"3\") + \"5\"`\\\n",
"Value: `\"35\"`\\\n",
"Type: `std::string`\n",
"\n",
"The `std::string` type is mutable and by adding a `const char` to it, the values are concatenated"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d14e9813-37d0-4fee-971e-77554c281971",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 35\n",
"Type: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >\n"
]
}
],
"source": [
"print_answer(std::string(\"3\") + \"5\"); // std::string is just a typedef of the outputted type"
]
},
{
"cell_type": "markdown",
"id": "bda6f1ad-e10f-4da7-853d-b8dc4cd17f4b",
"metadata": {},
"source": [
"`3 / 2`\\\n",
"Value: `1`\\\n",
"Type: `int`\n",
"\n",
"Integer division"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "efba3964-fabe-43a0-ba54-1babd43f2ad5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 1\n",
"Type: int\n"
]
}
],
"source": [
"print_answer(3 / 2)"
]
},
{
"cell_type": "markdown",
"id": "81ca8e28-25d8-423a-8eb2-3a556cf7868a",
"metadata": {},
"source": [
"`3.0 / 2`\\\n",
"Value: `1.5`\\\n",
"Type: `double`\n",
"\n",
"Double divided by integer leads to real division"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "41398ba2-e643-4983-832b-cc6be0334555",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 1.500\n",
"Type: double\n"
]
}
],
"source": [
"print_answer(3.0 / 2)"
]
},
{
"cell_type": "markdown",
"id": "785e5fe0-f9ca-40f4-95ff-6eef03c1a328",
"metadata": {},
"source": [
"`int(2.71828)`\\\n",
"Value: `2`\\\n",
"Type: `int`\n",
"\n",
"Casting to `int` cuts off decimals"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0d1a2087-4d09-4f45-b8f7-8af5c21ab17f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 2\n",
"Type: int\n"
]
}
],
"source": [
"print_answer(int(2.71828))"
]
},
{
"cell_type": "markdown",
"id": "7d412ac1-f706-4ce4-afd4-99661f2a95ad",
"metadata": {},
"source": [
"`std::round(2.71828)`\\\n",
"Value: `3`\\\n",
"Type: `double`\n",
"\n",
"`std::round` rounds to the nearest integer but preserves the type"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c9d053a8-e679-476d-a866-5fdc6cc3a5e7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 3.000\n",
"Type: double\n"
]
}
],
"source": [
"print_answer(std::round(2.71828))"
]
},
{
"cell_type": "markdown",
"id": "9e158210-30ed-4fe7-9fe0-b3207f21b5b5",
"metadata": {},
"source": [
"# Zahlen Raten"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "155598ed-03b4-4c77-92ee-24b5d25f84b1",
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <time.h>\n",
"\n",
"void guessing_game() { \n",
" int maximum;\n",
" std::cout << \"Maximum: \";\n",
" std::cin >> maximum;\n",
" \n",
" int guess_count = 0;\n",
" int guess;\n",
" \n",
" srand (time (NULL));\n",
" int random_number;\n",
" random_number = rand() % maximum;\n",
"\n",
" do {\n",
" std::cout << \"Guess: \";\n",
" std::cin >> guess;\n",
" ++guess_count;\n",
" \n",
" if (guess > random_number) {\n",
" std::cout << \"Too big!\\n\";\n",
" } else if (guess < random_number) {\n",
" std::cout << \"Too small!\\n\";\n",
" } else {\n",
" std::cout << \"Spot on!\\n\";\n",
" }\n",
" } while ( guess != random_number);\n",
" std::cout << \"You tried \" << guess_count << \" times until you got the correct number\" << std::endl;\n",
" return;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "0ce0aa12-7351-4c61-ae3d-20d38dbf15c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximum: "
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
" 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Guess: "
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
" 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Too big!\n",
"Guess: "
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
" 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Spot on!\n",
"You tried 2 times until you got the correct number\n"
]
}
],
"source": [
"guessing_game()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "caa1571e-18f1-4bd5-8793-f1319f8e7466",
"metadata": {},
"outputs": [],
"source": [
"int fibonacci_recursive(int n) {\n",
" if (n == 0) return 0;\n",
" if (n == 1) return 1;\n",
" return fibonacci_recursive(n-1) + fibonacci_recursive(n-2);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "41fb1f9f-8c8f-4be1-a25d-402581e7a12b",
"metadata": {},
"outputs": [],
"source": [
"int fibonacci_iterative(int n) {\n",
" int a_2 = 0;\n",
" int a_1 = 1;\n",
" int a;\n",
" \n",
" for (int i = 1; i < n; i++) {\n",
" a = a_1 + a_2;\n",
" a_2 = a_1;\n",
" a_1 = a;\n",
" }\n",
" \n",
" return a;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "14a166b0-4314-43f2-bcf7-68fc0b6d7442",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fibonacci_iterative(40) == fibonacci_recursive(40)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "2b61c2c8-af26-4b7e-9b69-e46ca3575533",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# N time_recursive time_iterative\n",
"1\t270\t150\n",
"2\t161\t150\n",
"4\t191\t160\n",
"8\t351\t170\n",
"16\t6432\t181\n",
"32\t13361184\t211\n"
]
}
],
"source": [
"#include <chrono>\n",
"using namespace std::chrono;\n",
"\n",
"std::vector<int> nrs = {1, 2, 4, 8, 16, 32};\n",
"std::cout << \"# N time_recursive time_iterative\" << std::endl;\n",
"for (auto i : nrs) {\n",
" // Time the fibonacci_recursive\n",
" auto start1 = high_resolution_clock::now();\n",
" fibonacci_recursive(i);\n",
" auto end1 = high_resolution_clock::now();\n",
" auto duration_recursive = duration_cast<nanoseconds>(end1 - start1);\n",
" // Time the fibonacci_iterative\n",
" auto start2 = high_resolution_clock::now();\n",
" fibonacci_iterative(i);\n",
" auto end2 = high_resolution_clock::now();\n",
" auto duration_iterative = duration_cast<nanoseconds>(end2 - start2);\n",
" // Produce the output\n",
" std::cout << i << \"\\t\" << duration_recursive.count() << \"\\t\" << duration_iterative.count() << std::endl;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "f6d43ee5-28e8-4d85-afd8-04f078c0d557",
"metadata": {},
"outputs": [],
"source": [
"// TODO: Plotting"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ed32215-4c61-4f9d-9e9b-42c46bb1924f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

7
environment-build.yml Normal file
View File

@ -0,0 +1,7 @@
name: build-blatt11
channels:
- conda-forge
dependencies:
# Build dependencies
- cmake
- cxx-compiler

18
environment-host.yml Normal file
View File

@ -0,0 +1,18 @@
name: blatt11
channels:
- conda-forge
dependencies:
# Build dependencies
- cmake
# Host dependencies
- xeus-zmq=1.0.1
- cling=0.9
- nlohmann_json
- cppzmq
- xtl=0.7
- pugixml
- cpp-argparse
# Test dependencies
- pytest
- jupyter_kernel_test
- doctest >= 2.4.6

14
environment.yml Normal file
View File

@ -0,0 +1,14 @@
name: blatt11
channels:
- conda-forge
dependencies:
- python=3.10
- mamba==1.1.0
- conda==22.9.0
- pip
- xeus-cling
- ca-certificates
- certifi
- openssl
- jupyterlab
- boost