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/ diff --git a/CMakeLists.txt b/CMakeLists.txt index ff568fc9d..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,21 +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) - add_compile_options(/Za) -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.") diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e163d414..47842b9ca 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -141,8 +141,7 @@ * [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) - * [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) 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; } diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 4f6e02081..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) { @@ -50,30 +56,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; } diff --git a/others/Palindromeofnumber.cpp b/others/Palindromeofnumber.cpp deleted file mode 100644 index 647803997..000000000 --- a/others/Palindromeofnumber.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -using namespace std; - -int main() -{ - int num; - cout << "Enter number = "; - cin >> num; - - string s1 = to_string(num); - string s2 = s1; - - reverse(s1.begin(), s1.end()); - - if (s1 == s2) - cout << "true"; - else - cout << "false"; - - return 0; -} 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; -} diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp new file mode 100644 index 000000000..621b09ab6 --- /dev/null +++ b/others/palindrome_of_number.cpp @@ -0,0 +1,27 @@ +#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 = "; + std::cin >> num; + + std::string s1 = std::to_string(num); + std::string s2 = s1; + + reverse(s1.begin(), s1.end()); + + if (s1 == s2) + std::cout << "true"; + else + std::cout << "false"; + + return 0; +} diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp index 1861163f1..d9878fda4 100644 --- a/others/sparse_matrix.cpp +++ b/others/sparse_matrix.cpp @@ -1,41 +1,46 @@ -/*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 = new int *[m]; + for (int i = 0; i < m; i++) a[i] = new int[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"; + + for (int i = 0; i < m; i++) delete[] a[i]; + delete[] a; + return 0; }