mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge branch 'cmake'
* cmake: MSVC does not know cstring-use string-for toString for windows build, MSVC knows C++-14 and not 11 remove redundant /Za for VC add cstring for std::to_string() updating DIRECTORY.md fixed dynamic array removed in-favor of chronos library of c++11 updating DIRECTORY.md fixed windows build error fixed lint file rename cin accepts only one variable at a time & fixed cpplint improved documentation `rand_r` is non-portable and obsolete newline character at EOF of gitignore
This commit is contained in:
commit
3939a8caff
@ -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.")
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -1,22 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
|
||||
/*
|
||||
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 <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
// To calculate the time taken by a code to execute
|
||||
#include <sys/time.h>
|
||||
#include <iostream>
|
||||
|
||||
__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;
|
||||
}
|
27
others/palindrome_of_number.cpp
Normal file
27
others/palindrome_of_number.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// Required to compile std::toString function using MSVC
|
||||
#include <string>
|
||||
#else
|
||||
#include <cstring>
|
||||
#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;
|
||||
}
|
@ -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.*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
/** @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.
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
#include <iostream>
|
||||
|
||||
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";
|
||||
|
||||
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++)
|
||||
{
|
||||
cout << "element? ";
|
||||
cin >> a[i][j];
|
||||
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
|
||||
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";
|
||||
if (counterZeros > ((m * n) / 2)) // Checking for sparse matrix
|
||||
std::cout << "Sparse matrix";
|
||||
else
|
||||
cout << "Not a sparse matrix";
|
||||
std::cout << "Not a sparse matrix";
|
||||
|
||||
for (int i = 0; i < m; i++) delete[] a[i];
|
||||
delete[] a;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user