diff --git a/assignments/02/02_1.cpp b/assignments/02/02_1.cpp index 937b72f..bbe77eb 100644 --- a/assignments/02/02_1.cpp +++ b/assignments/02/02_1.cpp @@ -15,7 +15,7 @@ int guess(int to_guess) { int guess_number; int check = check_number(guess, &guess_number); if (check < 0) { - std::cout << "Something seems to have went wrong." << std::endl; + std::cout << "Something went wrong. :/" << std::endl; return check; } if (!(guess_number == to_guess)) { diff --git a/assignments/02/02_2.cpp b/assignments/02/02_2.cpp new file mode 100644 index 0000000..5971bdf --- /dev/null +++ b/assignments/02/02_2.cpp @@ -0,0 +1,39 @@ +#include +#include + +int check_number(std::string number, double* checked_number) { + std::size_t pos; + *checked_number = std::stoi(number, &pos); // how to properly type check? or is that not necessary? + return 0; +} + +int main(int argc, char *argv[]) { + if (argc < 4) { + std::cout << "No/Not enough parameters given." << std::endl; + return -1; + } + double a, b, c; + int check = check_number(argv[1], &a) & check_number(argv[2], &b) & check_number(argv[3], &c); + if (check < 0) { + std::cout << "Something went wrong. :/" << std::endl; + return check; + } + double p = b/a; + double q = c/a; + + double w = (p*p / 4) - q; + if (w < 0) { + std::cout << "No real solutions." << std::endl; + return 0; + } + + double x_1 = -(p / 2) + sqrt(w); + double x_2 = -(p / 2) - sqrt(w); + + if (x_1 == x_2) + std::cout << "Double zero at x = " << x_1 << std::endl; + else + std::cout << "Zeroes found at x_1 = " << x_1 << " and x_2 = " << x_2 << std::endl; + + return 0; +} diff --git a/assignments/02/CMakeLists.txt b/assignments/02/CMakeLists.txt index 7ccac2a..daa9c7b 100644 --- a/assignments/02/CMakeLists.txt +++ b/assignments/02/CMakeLists.txt @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.11.4) project (01) add_executable(02_1 02_1.cpp) -#add_executable(01_2 01_2.cpp) +add_executable(02_2 02_2.cpp)