diff --git a/dot vscode/launch.json b/dot vscode/launch.json index 081340e..58291ac 100644 --- a/dot vscode/launch.json +++ b/dot vscode/launch.json @@ -4,7 +4,7 @@ "name": "Debug: C/C++", "type": "cppdbg", "request": "launch", - "program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", + "program": "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", @@ -30,7 +30,7 @@ "name": "Release: C/C++", "type": "cppdbg", "request": "launch", - "program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", + "program": "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", diff --git a/dot vscode/tasks.json b/dot vscode/tasks.json index 6037983..8299a09 100644 --- a/dot vscode/tasks.json +++ b/dot vscode/tasks.json @@ -6,12 +6,13 @@ "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", + "-I${workspaceFolder}\\include", "-ggdb", "-std=c++20", "-pedantic-errors", - "${fileDirname}/**.cpp", + "${workspaceFolder}/src/**.cpp", "-o", - "${fileDirname}\\build\\${fileBasenameNoExtension}.exe" + "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" @@ -26,13 +27,14 @@ "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", + "-I${workspaceFolder}\\include", "-O2", "-DNDEBUG", "-std=c++20", "-pedantic-errors", - "${fileDirname}/**.cpp", + "${workspaceFolder}/src/**.cpp", "-o", - "${fileDirname}\\build\\${fileBasenameNoExtension}.exe" + "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" diff --git a/include/.gitkeep b/include/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/io.cpp b/io.cpp deleted file mode 100644 index 6c03ca8..0000000 --- a/io.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "io.h" -#include - -int readNumber() -{ - std::cout << "Enter a number to add: "; - int x{}; - std::cin >> x; - return x; -} -void writeAnswer(int x) -{ - std::cout << "The answer is: " << x << "\n"; -} \ No newline at end of file diff --git a/io.h b/io.h deleted file mode 100644 index 7c489bc..0000000 --- a/io.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -int readNumber(); -void writeAnswer(int x); \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 9cf4fd0..0000000 --- a/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include // for std::cout -#include "io.h" - -// Definition of function main() -int main() -{ - - int a{readNumber()}; - int b{readNumber()}; - writeAnswer(a + b); - return 0; -} \ No newline at end of file diff --git a/src/algo.cpp b/src/algo.cpp new file mode 100644 index 0000000..f13f6b4 --- /dev/null +++ b/src/algo.cpp @@ -0,0 +1,11 @@ +#include "algo.h" + +void bubble_sort_print_demo() +{ + int demo[]{11, 14, 3, 18, 8, 17, 43, 6, 5, 4}; + double demo2[]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3, 12.3}; + std::string demo3[]{"hallo", "velo", "kekw", "imagin"}; + bubble_sort_print(sizeof(demo) / sizeof(demo[0]), demo); + bubble_sort_print(sizeof(demo2) / sizeof(demo2[0]), demo2); + bubble_sort_print(sizeof(demo3) / sizeof(demo3[0]), demo3); +} \ No newline at end of file diff --git a/src/algo.h b/src/algo.h new file mode 100644 index 0000000..52bc41f --- /dev/null +++ b/src/algo.h @@ -0,0 +1,55 @@ +#pragma once +#include +#include +#include + +void bubble_sort_print_demo(); + +template +struct TypeName +{ + static const char *Get() + { + return typeid(T).name(); + } +}; +template <> +struct TypeName +{ + static const char *Get() + { + return "s"; + } +}; + +template +void bubble_sort(int size, T *a) +{ + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size - 1 - i; j++) + { + if (a[j] > a[j + 1]) + { + T temp{a[j]}; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } +}; + +template +void bubble_sort_print(int size, T *a) +{ + std::cout << std::left; // left justify output + std::cout << "unsorted " << TypeName::Get() << std::setw(10) << " Array: "; + for (int i = 0; i < size; i++) + std::cout << a[i] << ", "; + std::cout << "\n"; + bubble_sort(size, a); + std::cout << "sorted " << TypeName::Get() << std::setw(12) << " Array: "; + for (int i = 0; i < size; i++) + std::cout << a[i] << ", "; + std::cout << "\n\n"; +}; \ No newline at end of file diff --git a/src/io.cpp b/src/io.cpp new file mode 100644 index 0000000..c8a8734 --- /dev/null +++ b/src/io.cpp @@ -0,0 +1,54 @@ +#include "io.h" +#include +#include + +void add_two_numbers() +{ + int a{read_number()}; + int b{read_number()}; + write_answer(a + b); +} +int read_number() +{ + std::cout << "Enter a number to add: "; + int x{}; + std::cin >> x; + return x; +} +void write_answer(int x) +{ + std::cout << "The answer is: " << x << "\n"; +} +void convert_char_to_ascii() +{ + std::cout << "Enter a single character: "; + char c{}; + std::cin >> c; + std::cout << "You entered '" << c << "', which hass ASCII code " << static_cast(c) << "."; +} + +void print_type_size_bytes() +{ + std::cout << std::left; // left justify output + std::cout << std::setw(16) << "bool:" << sizeof(bool) << " bytes\n"; + std::cout << std::setw(16) << "char:" << sizeof(char) << " bytes\n"; + std::cout << std::setw(16) << "short:" << sizeof(short) << " bytes\n"; + std::cout << std::setw(16) << "int:" << sizeof(int) << " bytes\n"; + std::cout << std::setw(16) << "long:" << sizeof(long) << " bytes\n"; + std::cout << std::setw(16) << "long long:" << sizeof(long long) << " bytes\n"; + std::cout << std::setw(16) << "float:" << sizeof(float) << " bytes\n"; + std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n"; + std::cout << std::setw(16) << "long double:" << sizeof(long double) << " bytes\n\n"; +} +void string_demo() +{ + using namespace std; + cout << "Enter your full name: "; + string name{}; + getline(cin >> ws, name); + cout << "Enter your age: "; + int age{}; + cin >> age; + int letters{static_cast(name.length())}; + cout << "Your age + length of name is: " << (letters + age) << "\n"; +} \ No newline at end of file diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..74cc367 --- /dev/null +++ b/src/io.h @@ -0,0 +1,8 @@ +#pragma once + +void add_two_numbers(); +int read_number(); +void write_answer(int x); +void convert_char_to_ascii(); +void print_type_size_bytes(); +void string_demo(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b0c8dde --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,14 @@ +#include // for std::cout +#include "io.h" +#include "algo.h" + +int main() +{ + // string_demo(); + // add_two_numbers(); + // convert_char_to_ascii(); + print_type_size_bytes(); + // bubble_sort_print_demo(); + + return 0; +}