This commit is contained in:
2025-04-09 10:22:44 +02:00
commit 96ff5392c8
330 changed files with 28300 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
---
BasedOnStyle: LLVM
---
Language: Cpp
AllowShortFunctionsOnASingleLine: Empty
DerivePointerAlignment: false
PointerAlignment: Left
ColumnLimit: 120
TabWidth: 4
IndentWidth: 2
...
+25
View File
@@ -0,0 +1,25 @@
# debug: clangd --check=modules/iue-io/ccsv.h
# debug: clangd --check=task1.hpp
# debug: clangd --check=task1.test.cpp
InlayHints:
Enabled: No
ParameterNames: Yes
DeducedTypes: No
---
CompileFlags:
Add:
# - --target=x86_64-w64-windows-gnu
# - --target=x86_64-pc-linux-gnu
- -Wall
- -Wno-unused-function
- -Wno-unused-variable
---
If:
PathMatch: [.*\.c, .*\.h]
CompileFlags:
Add: [-std=c11]
---
If:
PathMatch: [.*\.cpp, .*\.hpp]
CompileFlags:
Add: [-std=c++20]
+3
View File
@@ -0,0 +1,3 @@
task1
task2
task3
+3
View File
@@ -0,0 +1,3 @@
task1.main.cpp
task2.cpp
task3.cpp
+16
View File
@@ -0,0 +1,16 @@
## source: https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.h text
*.hpp text
*.c text
*.cpp text
*.py text
*.ipynb text
*.md text
*.txt text
*.csv text
+358
View File
@@ -0,0 +1,358 @@
# custom
build
doc
.cache
.vscode
.idea
# https://github.com/github/gitignore/blob/main/CMake.gitignore
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
# ttps://github.com/github/gitignore/blob/main/C.gitignore
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# https://github.com/github/gitignore/blob/main/C%2B%2B.gitignore
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# source: https://github.com/github/gitignore/blob/main/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# jetbrain IDEs: https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# VSCODE source: https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix
+42
View File
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.20)
# define project metadata
project(exercise2 LANGUAGES CXX
DESCRIPTION "exercise2"
HOMEPAGE_URL "https://sgit.iue.tuwien.ac.at/360050/exercise2")
# setting required language standards
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
# misc settings
# avoid ctest dashboard targets
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
# generate a compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# make all symbols visible on windows (which is default on unix)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
# options
option(BUILD_TESTING "enable testing with ctest" ON)
# testing
include(CTest)
# include own targets
add_executable(task1 task1.main.cpp)
add_test(NAME task1 COMMAND task1 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
set_property(TEST task1 PROPERTY PROPERTY_REGULAR_EXPRESSION "-1024")
add_executable(task2 task2.cpp task2.test.cpp)
add_test(NAME task2 COMMAND task2 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
add_executable(task3 task3.cpp task3.test.cpp)
add_test(NAME task3 COMMAND task3 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
+20
View File
@@ -0,0 +1,20 @@
# Hausübung 2 (3 Punkte)
**Ausgabe**: Donnerstag 14. März 2024, vormittags.
**Abgabe bis**: Montag 08. April 2024, Ende des Tages.
**Abgabe via**: git-Repository mit dem Namen **`exercise2`** auf unserem git-Server https://sgit.iue.tuwien.ac.at
Details zum Abgabeprozess via `git` finden Sie hier: https://sgit.iue.tuwien.ac.at/360050/git
# Aufgabenstellung
In dieser Hausübung werden folgende Themen erstmalig einfließen:
- Bedingungen/Verzweigungen
- Schleifen
- Übergabe/Rückgabe/Manipulation von Sequenzen in Form eines `std::vector<int>` und `std::vector<double>`
- Übergabe und Rückgabewerte mehrerer Werte mittels `std::tuple<double,double,double>` und `std::tuple<double,double>`
**Die genaue Beschreibung und Anforderungen finden Sie in [`main.ipynb`](main.ipynb) und im Quellcode.**
+1
View File
@@ -0,0 +1 @@
-Imodules
+2
View File
@@ -0,0 +1,2 @@
2024-04-08T09:19:08+02:00 | b8f93888363b8ac94476d28a5f7e0b85485a23ec | who has two thumbs and is a genius? not this guy!
2024-04-08T09:17:30+02:00 | 0d8d68a4679f8e4b48027a25da4280ec914cb6ab | Fingers crossed!
+192
View File
@@ -0,0 +1,192 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cheatsheets\n",
"\n",
"- [exercise1](https://sgit.iue.tuwien.ac.at/360050/cheatsheet/raw/branch/master/exercise1.pdf)\n",
"- [exercise2](https://sgit.iue.tuwien.ac.at/360050/cheatsheet/raw/branch/master/exercise2.pdf)\n",
"\n",
"## Aufgabe 1: Ein eigenes kleines C++-Programm (*vector in/ scalar out*) (1 Punkt)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Erstellen Sie in [`task1.main.cpp`](task1.main.cpp) ein lauffähiges Ein-Dateien-Programm das folgende Struktur aufweist:\n",
"\n",
"- Einbinden benötigter Header-Dateien aus der Standardbibliothek, z.B.:\n",
"\t```cpp\n",
"\t#include <iostream> // std::cout, std::endl\n",
"\t#include <...>\n",
"\t```\n",
"- Definition/Implementierung einer eigenen Funktion, z.B.:\n",
"\t```cpp\n",
"\tint func(...){\n",
"\t ...\n",
"\t}\n",
"\t``` \n",
"- Definition/Implementierung einer `main`-Funktion (Einstiegspunkt für jedes lauffähige Programm), die Ihre selbst geschriebene Funktion verwendet und die berechneten Ergebnisse in der Konsole ausgibt, z.B.:\n",
"\t```cpp\n",
"\tint main(){\n",
"\t ...\n",
"\t auto res = func(...)\t\n",
"\t std::cout << res << std::endl;\n",
"\t return 0;\n",
"\t}\n",
"\t``` \n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"- Eine genaue Beschreibung und Anforderungen finden Sie in [`task1.main.cpp`](task1.main.cpp)\n",
"- Ihre Implementierung erfolgt ebenfalls in [`task1.main.cpp`](task1.main.cpp)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aufgabe 2: Funktion mit Sequenzen von Werten als Parameter (`std::vector`), internen Verzweigungen (`if`/`else`), und mehreren Rückgabewerten (`std::tuple`) (1 Punkt)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Implementieren Sie die folgenden Funktionen:\n",
"\n",
"```cpp\n",
"// type aliases\n",
"using Vector = std::vector<double>;\n",
"using Tuple2 = std::tuple<double,double>;\n",
"\n",
"int count_gt(Vector data, double ref);\n",
"int count_lt(Vector data, double ref);\n",
"\n",
"Vector select_gt(Vector data, double ref);\n",
"Vector select_lt(Vector data, double ref);\n",
"Vector select_gt_and_lt(Vector data, double lower, double upper);\n",
"\n",
"double mean(Vector data);\n",
"double median(Vector data);\n",
"\n",
"Tuple2 minmax(Vector data);\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"- Die vorgegebenen Deklarationen und eine genaue Beschreibung und Anforderungen finden Sie in [`task2.hpp`](task2.hpp)\n",
"- Ihre Implementierung erfolgt in [`task2.cpp`](task2.cpp)\n",
"- Die zugeordneten Tests finden Sie in [`task2.test.cpp`](task2.test.cpp)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aufgabe 3: Kapselung einer Berechnung mittels einer Funktion mit mehreren Rückgabewerten (1 Punkt)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Sie kapseln die Berechnung der Lösungen zu einer quadratischen Gleichung $ax^2 + bx + c = 0$ in einer Funktion.\n",
"\n",
"\n",
"Implementieren Sie folgenden beiden Funktionen (überladener Funktionsname):\n",
"\n",
"```cpp\n",
"std::tuple<double, double> solve_quadratic_equation(double a, double b, double c);\n",
"std::tuple<double, double> solve_quadratic_equation(std::tuple<double, double, double> abc);\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"- Die vorgegebenen Deklaration und eine genaue Beschreibung und Anforderungen finden Sie in [`task3.hpp`](task3.hpp)\n",
"- Ihre Implementierung erfolgt in [`task3.cpp`](task3.cpp)\n",
"- Die zugeordneten Tests finden Sie in [`task3.test.cpp`](task3.test.cpp)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kompilieren/Testen\n",
"\n",
"So testen Sie Ihre Implementierung (direkter Aufruf von `g++`):\n",
"\n",
"```shell\n",
"# prepare\n",
"mkdir build\n",
"# compile\n",
"g++ -g -std=c++20 task1.main.cpp -o build/task1.exe\n",
"g++ -g -std=c++20 task2.cpp task2.test.cpp -o build/task2.exe\n",
"g++ -g -std=c++20 task3.cpp task3.test.cpp -o build/task3.exe\n",
"# run tests \n",
"./build/task1.exe\n",
"./build/task2.exe\n",
"./build/task3.exe\n",
"```\n",
"\n",
"Alternativ (mittels CMake-Configuration):\n",
"\n",
"```shell\n",
"# prepare\n",
"cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug\n",
"# compile \n",
"cmake --build build --config Debug --target task1\n",
"cmake --build build --config Debug --target task2\n",
"cmake --build build --config Debug --target task3\n",
"# run tests \n",
"ctest --test-dir build -C Debug -R task1\n",
"ctest --test-dir build -C Debug -R task2\n",
"ctest --test-dir build -C Debug -R task3\n",
"```\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+33
View File
@@ -0,0 +1,33 @@
/// @file
/// @brief Task1: "single-file" excutable C++ program
/// @todo Include standard library headers as needed
#include <iostream>
#include <vector>
/// @brief Find the minimum value in a sequence of integer values
/// @param data Sequence of values stored in a std::vector<int> (assertion: sequence is not empty )
/// @return Minimum value in the sequence
int min(const std::vector<int>& data) {
int min = data[0];
for (int i = 1; i < data.size(); i++) {
if (data[i] < min) {
min = data[i];
}
}
return min;
}
/// @brief main function (entry point) conducting the following tasks in this order:
/// - create and prepare a local variable of type std::vector<int>
/// holding a sequence of 5 values in this order: -10, 20, 100, -1024, 2048
/// - call your function 'min' and provide the prepared variable as argument to the call
/// - capture the result of your function call in a local variable and print it to the console
int main() {
std::vector<int> data = {-10, 20, 100, -1024, 2048};
int min_value = min(data);
std::cout << "The minimum value is: " << min_value << std::endl;
return 0;
}
+117
View File
@@ -0,0 +1,117 @@
#include "task2.hpp" // count_gt|count_lt|select_gt|select_lt|select_gt_and_lt|mean|median|minmax
#include <algorithm>
/// @todo Include standard library headers as needed
/// @brief Counts how many values in a sequence are greater than (gt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Number of values in the sequence, which are greater than than ref
int count_gt(std::vector<double> data, double ref) {
int count = 0;
for (int i = 0; i < data.size(); i++) {
if (data[i] > ref) {
count++;
}
}
return count;
}
/// @brief Counts how many values in a sequence are less than (lt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Number of values in the sequence, which are less than than ref
int count_lt(std::vector<double> data, double ref) {
int count = 0;
for (int i = 0; i < data.size(); i++) {
if (data[i] < ref) {
count++;
}
}
return count;
}
/// @brief Selects values from a sequence which are greater than (gt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_gt(std::vector<double> data, double ref) {
std::vector<double> selected;
for (int i = 0; i < data.size(); i++) {
if (data[i] > ref) {
selected.push_back(data[i]);
}
}
return selected;
}
/// @brief Selects values from a sequence which are less than (lt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_lt(std::vector<double> data, double ref) {
std::vector<double> selected;
for (int i = 0; i < data.size(); i++) {
if (data[i] < ref) {
selected.push_back(data[i]);
}
}
return selected;
}
/// @brief Selects values from a sequence which are bounded by two reference values
/// @param data Sequence of values
/// @param lower Lower bound
/// @param upper Upper bound
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_gt_and_lt(std::vector<double> data, double lower, double upper) {
std::vector<double> selected;
for (int i = 0; i < data.size(); i++) {
if (data[i] > lower && data[i] < upper) {
selected.push_back(data[i]);
}
}
return selected;
}
/// @brief Calculates the mean for a sequence of values
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Mean value (arithmetic mean)
double mean(std::vector<double> data) {
double sum = 0;
for (int i = 0; i < data.size(); i++) {
sum += data[i];
}
return sum / data.size();
}
/// @brief Calculate the median for a sequence of numbers
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Median value:
/// - if the length of the sequence is odd, the median value is "the middle value", else
/// - if the length of the sequence is even, the median value is the avarage of the "two middle values"
double median(std::vector<double> data) {
std::sort(data.begin(), data.end());
if (data.size() % 2 == 0) {
return (data[data.size() / 2 - 1] + data[data.size() / 2]) / 2;
} else {
return data[data.size() / 2];
}
}
/// @brief Finds the minimum and maximum value
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Tuple with the minimum and maximum value (in this order)
std::tuple<double, double> minmax(std::vector<double> data) {
double min = data[0];
double max = data[0];
for (int i = 1; i < data.size(); i++) {
if (data[i] < min) {
min = data[i];
}
if (data[i] > max) {
max = data[i];
}
}
return std::make_tuple(min, max);
}
+55
View File
@@ -0,0 +1,55 @@
/// @file
/// @brief Task2: function declarations
#pragma once
#include <tuple> // std::tuple
#include <vector> // std::vector
/// @brief Counts how many values in a sequence are greater than (gt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Number of values in the sequence, which are greater than than ref
int count_gt(std::vector<double> data, double ref);
/// @brief Counts how many values in a sequence are less than (lt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Number of values in the sequence, which are less than than ref
int count_lt(std::vector<double> data, double ref);
/// @brief Selects values from a sequence which are greater than (gt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_gt(std::vector<double> data, double ref);
/// @brief Selects values from a sequence which are less than (lt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_lt(std::vector<double> data, double ref);
/// @brief Selects values from a sequence which are bounded by two reference values
/// @param data Sequence of values
/// @param lower Lower bound
/// @param upper Upper bound
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_gt_and_lt(std::vector<double> data, double lower, double upper);
/// @brief Calculates the mean for a sequence of values
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Mean value (arithmetic mean)
double mean(std::vector<double> data);
/// @brief Calculate the median for a sequence of numbers
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Median value:
/// - if the length of the sequence is odd, the median value is "the middle value", else
/// - if the length of the sequence is even, the median value is the avarage of the "two middle values"
double median(std::vector<double> data);
/// @brief Finds the minimum and maximum value
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Tuple with the minimum and maximum value (in this order)
std::tuple<double, double> minmax(std::vector<double> data);
+112
View File
@@ -0,0 +1,112 @@
/// @file
/// @brief Test for Task2
#include "task2.hpp" // count_gt|count_lt|select_gt|select_lt|select_gt_and_lt|mean|median|minmax
#include <cassert> // assert
#include <iostream> // std::cout|endl
int main() {
{ // testing function 'count_gt'
int res = count_gt({1.0, 2.0, 3.0, 4.0}, 2.0);
assert(res == 2);
}
{ // testing function 'count_gt'
int res = count_gt({-4.0, -2.0, -2.0, -1.0}, -2.0);
assert(res == 1);
}
{ // testing function 'count_lt'
int res = count_lt({-4.0, -2.0, -2.0, -1.0}, -1.0);
assert(res == 3);
}
{ // testing function 'count_lt'
int res = count_lt({2.0, 2.0, 2.0, 2.0}, 3.0);
assert(res == 4);
}
{ // testing function 'select_lt'
std::vector<double> res = select_lt({2.0, 2.0, 2.0, 2.0}, 3.0);
std::vector<double> expected = {2.0, 2.0, 2.0, 2.0};
assert(res == expected);
}
{ // testing function 'select_lt'
std::vector<double> res = select_lt({1.0, 2.0, 3.0, 4.0}, 4.0);
std::vector<double> expected = {1.0, 2.0, 3.0};
assert(res == expected);
}
{ // testing function 'select_gt'
std::vector<double> res = select_gt({2.0, 2.0, 2.0, 2.0}, 2.0);
std::vector<double> expected = {};
assert(res == expected);
}
{ // testing function 'select_gt'
std::vector<double> res = select_gt({1.0, 2.0, 3.0, 4.0}, 2.0);
std::vector<double> expected = {3.0, 4.0};
assert(res == expected);
}
{ // testing function 'select_gt_and_lt'
std::vector<double> res = select_gt_and_lt({-3.0, 2.0, -3.0, -2.0, 4.0, 1.0, -1.0, -4.0}, -3.0, 3.0);
std::vector<double> expected = {2.0, -2.0, 1.0, -1.0};
assert(res == expected);
}
{ // testing function 'mean'
double res = mean({1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0});
double expected = 3.0;
assert(std::abs(res - expected) < 1e-7);
}
{ // testing function 'mean'
double res = mean({-3.0, -2.0, -1.0, 1.0, 2.0, 3.0});
double expected = 0.0;
assert(std::abs(res - expected) < 1e-7);
}
{ // testing function 'median'
double res = median({3.0});
double expected = 3.0;
assert(std::abs(res - expected) < 1e-7);
}
{ // testing function 'median'
double res = median({2.0, 3.0});
double expected = 2.5;
assert(std::abs(res - expected) < 1e-7);
}
{ // testing function 'median'
double res = median({3.0, 2.0, 3.0, 2.0});
double expected = 2.5;
assert(std::abs(res - expected) < 1e-7);
}
{ // testing function 'median'
double res = median({3.0, 3.0, 3.0, -1.0, 4.0});
double expected = 3.0;
assert(std::abs(res - expected) < 1e-7);
}
{ // testing function 'minmax'
std::tuple<double, double> res = minmax({300.0, 100.0});
std::tuple<double, double> expected = {100.0, 300.0};
assert(res == expected);
}
{ // testing function 'minmax'
std::tuple<double, double> res = minmax({-1.0, -2.0, -3.0, 4.0, 20.0, 12.0});
std::tuple<double, double> expected = {-3.0, 20.0};
assert(res == expected);
}
std::cout << "task2.test.cpp: all asserts passed" << std::endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include "task3.hpp" // solve_quadratic_equation
#include <valarray>
/// @brief Calculates the real solutions of the quadratic equation a*x^2 + b*x + c = 0.
/// @param a coefficient; assertion: 'a' is a non-zero value
/// @param b coefficient
/// @param c coefficient
/// @return The two real solutions (order: ascending).
/// If no real solutions exists, the tuple contains two quiet NaNs.
std::tuple<double, double> solve_quadratic_equation(double a, double b, double c){
double x1, x2;
double d = b*b - 4*a*c;
if(d < 0){
x1 = NAN;
x2 = NAN;
}else{
x1 = (-b - sqrt(d))/(2*a);
x2 = (-b + sqrt(d))/(2*a);
}
return std::make_tuple(x1, x2);
}
/// @brief Calculates the real solutions of the quadratic equation a*x^2 + b*x + c = 0.
/// @param abc coefficients; assertion: first coefficient 'a' is a non-zero value
/// @return The two real solutions (order: ascending).
/// If no real solutions exists, the tuple contains two quiet NaNs.
std::tuple<double, double> solve_quadratic_equation(std::tuple<double, double, double> abc){
double a = std::get<0>(abc);
double b = std::get<1>(abc);
double c = std::get<2>(abc);
return solve_quadratic_equation(a, b, c);
}
+20
View File
@@ -0,0 +1,20 @@
/// @file
/// @brief Task3: function declarations
#pragma once
#include <tuple> // std::tuple
/// @brief Calculates the real solutions of the quadratic equation a*x^2 + b*x + c = 0.
/// @param a coefficient; assertion: 'a' is a non-zero value
/// @param b coefficient
/// @param c coefficient
/// @return The two real solutions (order: ascending).
/// If no real solutions exists, the tuple contains two quiet NaNs.
std::tuple<double, double> solve_quadratic_equation(double a, double b, double c);
/// @brief Calculates the real solutions of the quadratic equation a*x^2 + b*x + c = 0.
/// @param abc coefficients; assertion: first coefficient 'a' is a non-zero value
/// @return The two real solutions (order: ascending).
/// If no real solutions exists, the tuple contains two quiet NaNs.
std::tuple<double, double> solve_quadratic_equation(std::tuple<double, double, double> abc);
+54
View File
@@ -0,0 +1,54 @@
/// @file
/// @brief Test for Task3
#include "task3.hpp" // solve_quadratic_equation
#include <cassert> // assert
#include <cmath> // NAN
#include <iostream> // std::cout|endl
int main() {
{ // testing function overloads 'solve_quadratic_equation'
auto [s1, s2] = solve_quadratic_equation(1.0, 3.0, 2.0);
assert(std::abs(s1 - (-2.0)) < 1e-7);
assert(std::abs(s2 - (-1.0)) < 1e-7);
}
{ // testing function overloads 'solve_quadratic_equation'
std::tuple<double, double, double> coefficients = {1.0, 3.0, 2.0};
auto [s1, s2] = solve_quadratic_equation(coefficients);
assert(std::abs(s1 - (-2.0)) < 1e-7);
assert(std::abs(s2 - (-1.0)) < 1e-7);
}
{ // testing function overloads 'solve_quadratic_equation'
auto [s1, s2] = solve_quadratic_equation(1.0, 2.0, 5.0);
assert(std::isnan(s1));
assert(std::isnan(s2));
}
{ // testing function overloads 'solve_quadratic_equation'
std::tuple<double, double, double> coefficients = {1.0, 2.0, 5.0};
auto [s1, s2] = solve_quadratic_equation(coefficients);
assert(std::isnan(s1));
assert(std::isnan(s2));
}
{ // testing function overloads 'solve_quadratic_equation'
auto [s1, s2] = solve_quadratic_equation(2.0, 1.0, -3.0);
assert(std::abs(s1 - (-1.5)) < 1e-7);
assert(std::abs(s2 - (1.0)) < 1e-7);
}
{ // testing function overloads 'solve_quadratic_equation'
std::tuple<double, double, double> coefficients = {2.0, 1.0, -3.0};
auto [s1, s2] = solve_quadratic_equation(coefficients);
assert(std::abs(s1 - (-1.5)) < 1e-7);
assert(std::abs(s2 - (1.0)) < 1e-7);
}
std::cout << "task3.test.cpp: all asserts passed" << std::endl;
return 0;
}