From 614ea8eb5841199d86c40414935e68e10bc3b918 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 08:48:09 -0400 Subject: [PATCH 01/15] newline character at EOF of gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 12318c6cd..5275088d0 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,4 @@ a.out *.out *.app -build/ \ No newline at end of file +build/ From 0ad756f8609efdb5fd3d27508fa692c738fc1fc7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:05:29 -0400 Subject: [PATCH 02/15] `rand_r` is non-portable and obsolete --- math/fast_power.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 4f6e02081..5dd724085 100644 --- a/math/fast_power.cpp +++ b/math/fast_power.cpp @@ -50,30 +50,28 @@ double fast_power_linear(int64_t a, int64_t b) { } int main() { - std::srand(time(NULL)); + std::srand(std::time(nullptr)); std::ios_base::sync_with_stdio(false); std::cout << "Testing..." << std::endl; for (int i = 0; i < 20; i++) { - unsigned int *rand1, *rand2; - int a = rand_r(rand1) % 20 - 10; - int b = rand_r(rand2) % 20 - 10; + int a = std::rand() % 20 - 10; + int b = std::rand() % 20 - 10; std::cout << std::endl << "Calculating " << a << "^" << b << std::endl; assert(fast_power_recursive(a, b) == std::pow(a, b)); assert(fast_power_linear(a, b) == std::pow(a, b)); - std::cout << "------ " << a << "^" << b << " = "<< - fast_power_recursive(a, b) << std::endl; + std::cout << "------ " << a << "^" << b << " = " + << fast_power_recursive(a, b) << std::endl; } int64_t a, b; std::cin >> a >> b; - std::cout << a << "^" << b << " = "<< - fast_power_recursive(a, b) << std::endl; + std::cout << a << "^" << b << " = " << fast_power_recursive(a, b) + << std::endl; - std::cout << a << "^" << b << " = "<< - fast_power_linear(a, b) << std::endl; + std::cout << a << "^" << b << " = " << fast_power_linear(a, b) << std::endl; return 0; } From 139964d32563b3c92e0651bf46350e107391c5a5 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:05:44 -0400 Subject: [PATCH 03/15] improved documentation --- math/fast_power.cpp | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 5dd724085..0ffbcd40d 100644 --- a/math/fast_power.cpp +++ b/math/fast_power.cpp @@ -1,22 +1,29 @@ -#include -#include -#include -#include -#include -#include - -/* - Program that computes a^b in O(logN) time. +/** + * @file + Program that computes \f$a^b\f$ in \f$O(logN)\f$ time. It is based on formula that: - case1) if b is even: a^b = a^(b/2) * a^(b/2) = (a^(b/2))ˆ2 - case2) if b is odd: a^b = a^((b-1)/2) * a^((b-1)/2) * a = (a^((b-1)/2))^2 * a - We can compute a^b recursively using above algorithm. + 1. if \f$b\f$ is even: \f$a^b = a^\frac{b}{2} \cdot a^\frac{b}{2} = + {a^\frac{b}{2}}^2\f$ + 2. if \f$b\f$ is odd: \f$a^b = a^\frac{b-1}{2} \cdot + a^\frac{b-1}{2} \cdot a = {a^\frac{b-1}{2}}^2 \cdot a\f$ + + We can compute \f$a^b\f$ + recursively using above algorithm. */ +#include +#include +#include +#include +#include +#include + +/** + * algorithm implementation for \f$a^b\f$ + */ double fast_power_recursive(int64_t a, int64_t b) { // negative power. a^b = 1 / (a^-b) - if (b < 0) - return 1.0 / fast_power_recursive(a, -b); + if (b < 0) return 1.0 / fast_power_recursive(a, -b); if (b == 0) return 1; int64_t bottom = fast_power_recursive(a, b >> 1); @@ -31,14 +38,13 @@ double fast_power_recursive(int64_t a, int64_t b) { return result; } -/* +/** Same algorithm with little different formula. It still calculates in O(logN) */ double fast_power_linear(int64_t a, int64_t b) { // negative power. a^b = 1 / (a^-b) - if (b < 0) - return 1.0 / fast_power_linear(a, -b); + if (b < 0) return 1.0 / fast_power_linear(a, -b); double result = 1; while (b) { From 2def9abcc2306ab569687f85f1802c4e5493d244 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:10:28 -0400 Subject: [PATCH 04/15] cin accepts only one variable at a time & fixed cpplint --- others/sparse_matrix.cpp | 65 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp index 1861163f1..de4759a3e 100644 --- a/others/sparse_matrix.cpp +++ b/others/sparse_matrix.cpp @@ -1,41 +1,40 @@ -/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2, -where m and n are the dimensions of the matrix.*/ +/** @file + * A sparse matrix is a matrix which has number of zeroes greater than + * \f$\frac{m*n}{2}\f$, where m and n are the dimensions of the matrix. + */ + #include -using namespace std; -int main() -{ - int m, n; - int counterZeros = 0; - cout << "Enter dimensions of matrix (seperated with space): "; - cin >> m >> n; - int a[m][n]; - cout << "Enter matrix elements:"; - cout << "\n"; +int main() { + int m, n; + int counterZeros = 0; - // reads the matrix from stdin - for (int i = 0; i < m; i++) - { - for (int j = 0; j < n; j++) - { - cout << "element? "; - cin >> a[i][j]; + std::cout << "Enter dimensions of matrix (seperated with space): "; + std::cin >> m; + std::cin >> n; + + int a[m][n]; + std::cout << "Enter matrix elements:"; + std::cout << "\n"; + + // reads the matrix from stdin + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + std::cout << "element? "; + std::cin >> a[i][j]; + } } - } - // counts the zero's - for (int i = 0; i < m; i++) - { - for (int j = 0; j < n; j++) - { - if (a[i][j] == 0) - counterZeros++; //Counting number of zeroes + // counts the zero's + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (a[i][j] == 0) counterZeros++; // Counting number of zeroes + } } - } - // makes sure the matrix is a sparse matrix - if (counterZeros > ((m * n) / 2)) //Checking for sparse matrix - cout << "Sparse matrix"; - else - cout << "Not a sparse matrix"; + // makes sure the matrix is a sparse matrix + if (counterZeros > ((m * n) / 2)) // Checking for sparse matrix + std::cout << "Sparse matrix"; + else + std::cout << "Not a sparse matrix"; } From d6d328c8c9a760b86e61b6e3e491a6a4416926db Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:25:42 -0400 Subject: [PATCH 05/15] file rename --- others/{Palindromeofnumber.cpp => palindrome_of_number.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename others/{Palindromeofnumber.cpp => palindrome_of_number.cpp} (100%) diff --git a/others/Palindromeofnumber.cpp b/others/palindrome_of_number.cpp similarity index 100% rename from others/Palindromeofnumber.cpp rename to others/palindrome_of_number.cpp From fb3a8091f9492ccac9b66572612e39e394eed4ad Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:26:40 -0400 Subject: [PATCH 06/15] fixed lint --- others/palindrome_of_number.cpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp index 647803997..dda872882 100644 --- a/others/palindrome_of_number.cpp +++ b/others/palindrome_of_number.cpp @@ -1,23 +1,20 @@ -#include #include +#include -using namespace std; +int main() { + int num; + std::cout << "Enter number = "; + std::cin >> num; -int main() -{ - int num; - cout << "Enter number = "; - cin >> num; + std::string s1 = std::to_string(num); + std::string s2 = s1; - string s1 = to_string(num); - string s2 = s1; + reverse(s1.begin(), s1.end()); - reverse(s1.begin(), s1.end()); + if (s1 == s2) + std::cout << "true"; + else + std::cout << "false"; - if (s1 == s2) - cout << "true"; - else - cout << "false"; - - return 0; + return 0; } From 4b25222d430a3be8b2072cd671df7513412c8a5c Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:32:37 -0400 Subject: [PATCH 07/15] fixed windows build error --- .../gaussian_elimination.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/computer_oriented_statistical_methods/gaussian_elimination.cpp b/computer_oriented_statistical_methods/gaussian_elimination.cpp index ecfbfacc9..0b8bb693d 100644 --- a/computer_oriented_statistical_methods/gaussian_elimination.cpp +++ b/computer_oriented_statistical_methods/gaussian_elimination.cpp @@ -6,7 +6,11 @@ int main() { std::cout << "Matrix size: "; std::cin >> mat_size; - double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1]; + double **mat = new double *[mat_size + 1], **x = new double *[mat_size]; + for (i = 0; i <= mat_size; i++) { + mat[i] = new double[mat_size + 1]; + if (i < mat_size) x[i] = new double[mat_size + 1]; + } std::cout << std::endl << "Enter value of the matrix: " << std::endl; for (i = 0; i < mat_size; i++) { @@ -49,5 +53,13 @@ int main() { std::cout << "x" << i << "= " << x[i][i] << std::endl; } + + for (i = 0; i <= mat_size; i++) { + delete[] mat[i]; + if (i < mat_size) delete[] x[i]; + } + delete[] mat; + delete[] x; + return 0; } From a3a57aea2d1aee64f7e9ce5e0b7f3eac686642ce Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 13:33:02 +0000 Subject: [PATCH 08/15] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e163d414..7b0fb2b6b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -142,7 +142,7 @@ * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) * [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp) - * [Palindromeofnumber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Palindromeofnumber.cpp) + * [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/palindrome_of_number.cpp) * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp) * [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp) * [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp) From 899be6a1f17e4f1bd1e7fee6375432767757cc0b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:35:25 -0400 Subject: [PATCH 09/15] removed in-favor of chronos library of c++11 --- others/measure_time_elapsed.cpp | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 others/measure_time_elapsed.cpp diff --git a/others/measure_time_elapsed.cpp b/others/measure_time_elapsed.cpp deleted file mode 100644 index d0830ab79..000000000 --- a/others/measure_time_elapsed.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// To calculate the time taken by a code to execute -#include -#include - -__int64_t getTimeInMicroseconds() { - struct timeval start; - gettimeofday(&start, NULL); - return start.tv_sec * 1000000 + start.tv_usec; -} - -// write function sample(args) - -int main() { - // write code - __int64_t starttime = getTimeInMicroseconds(); - // sample(args) function run - // Any other functions (if present) run - std::cout << getTimeInMicroseconds() - starttime; -} From dd20bf94427392ea0baa428ecf2587d6a8a7a131 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:37:36 -0400 Subject: [PATCH 10/15] fixed dynamic array --- others/sparse_matrix.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp index de4759a3e..d9878fda4 100644 --- a/others/sparse_matrix.cpp +++ b/others/sparse_matrix.cpp @@ -13,7 +13,9 @@ int main() { std::cin >> m; std::cin >> n; - int a[m][n]; + int **a = new int *[m]; + for (int i = 0; i < m; i++) a[i] = new int[n]; + std::cout << "Enter matrix elements:"; std::cout << "\n"; @@ -37,4 +39,8 @@ int main() { std::cout << "Sparse matrix"; else std::cout << "Not a sparse matrix"; + + for (int i = 0; i < m; i++) delete[] a[i]; + delete[] a; + return 0; } From 3d5a6fbce3f7ad219c2946e6faeb03335e66367d Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 13:40:20 +0000 Subject: [PATCH 11/15] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7b0fb2b6b..47842b9ca 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -141,7 +141,6 @@ * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/gcd_of_n_numbers.cpp) * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) - * [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp) * [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/palindrome_of_number.cpp) * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp) * [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp) From 2217049119a2ecfcd7f2aad8feae03594be0787c Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:40:57 -0400 Subject: [PATCH 12/15] add cstring for std::to_string() --- others/palindrome_of_number.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp index dda872882..93c54a965 100644 --- a/others/palindrome_of_number.cpp +++ b/others/palindrome_of_number.cpp @@ -1,4 +1,5 @@ #include +#include #include int main() { From fdee12d82af42f86a3df5964a6be4182084bbe26 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:50:41 -0400 Subject: [PATCH 13/15] remove redundant /Za for VC --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff568fc9d..14079562a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) if(MSVC) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) - add_compile_options(/Za) endif(MSVC) add_subdirectory(math) From 8bc1559b082454a397cd6f8f0104a7cc0c0b7737 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 10:00:09 -0400 Subject: [PATCH 14/15] for windows build, MSVC knows C++-14 and not 11 --- CMakeLists.txt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 14079562a..4394f84e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,14 @@ project(Algorithms_in_C++ # set(CMAKE_CXX_CPPLINT "~/anaconda3/bin/cpplint --filter=-legal/copyright --std=c++11") # find_program(CLANG_FORMAT "clang-format") +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +if(MSVC) + set(CMAKE_CXX_STANDARD 14) + add_compile_definitions(_CRT_SECURE_NO_WARNINGS) +endif(MSVC) + option(USE_OPENMP "flag to use OpenMP for multithreading" ON) cmake_policy(SET CMP0054 NEW) @@ -40,20 +48,13 @@ if(DOXYGEN_FOUND) ) endif() -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -if(MSVC) - add_compile_definitions(_CRT_SECURE_NO_WARNINGS) -endif(MSVC) - add_subdirectory(math) add_subdirectory(others) add_subdirectory(computer_oriented_statistical_methods) if(USE_OPENMP) find_package(OpenMP) - if (OpenMP_C_FOUND) + if (OpenMP_CXX_FOUND) message(STATUS "Building with OpenMP Multithreading.") else() message(STATUS "No OpenMP found, no multithreading.") From fe9d2b9bc66ee9f2e5112ced6b6d5aab7d5e5f05 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 10:06:01 -0400 Subject: [PATCH 15/15] MSVC does not know cstring-use string-for toString --- others/palindrome_of_number.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp index 93c54a965..621b09ab6 100644 --- a/others/palindrome_of_number.cpp +++ b/others/palindrome_of_number.cpp @@ -1,7 +1,13 @@ #include -#include #include +#ifdef _MSC_VER +// Required to compile std::toString function using MSVC +#include +#else +#include +#endif + int main() { int num; std::cout << "Enter number = ";