From a1663277c76e8d814a745a596f864c2e1fdd9080 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:51:18 -0400 Subject: [PATCH 01/14] cherry pick changes from "cmake branch" From 8207c776ebdbd3a77018651223e37780a151cc2a Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:52 -0400 Subject: [PATCH 02/14] syntax correction (cherry picked from commit 260ba8d3a46e76b8d319c96b23983c9c2d93d046) --- math/fibonacci.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 1c07cd93f..e82875da3 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -1,5 +1,5 @@ -#include #include +#include /* Calculate the the value on Fibonacci's sequence given an integer as input @@ -7,14 +7,13 @@ Fibonacci = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... */ -int fibonacci(uint n) { +int fibonacci(unsigned int n) { /* If the input is 0 or 1 just return the same This will set the first 2 values of the sequence */ - if (n <= 1) - return n; + if (n <= 1) return n; /* Add the last 2 values of the sequence to get next */ - return fibonacci(n-1) + fibonacci(n-2); + return fibonacci(n - 1) + fibonacci(n - 2); } int main() { From b36322c629809375de3abc30d48479efa4acddbf Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:39 -0400 Subject: [PATCH 03/14] file rename to standards (cherry picked from commit 412d3ff1a2fc491e4c8e0b05db814c186bdf7dc4) --- math/{primes_up_to_10^8.cpp => primes_up_to_billion.cpp} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename math/{primes_up_to_10^8.cpp => primes_up_to_billion.cpp} (79%) diff --git a/math/primes_up_to_10^8.cpp b/math/primes_up_to_billion.cpp similarity index 79% rename from math/primes_up_to_10^8.cpp rename to math/primes_up_to_billion.cpp index db9b56cab..d8f5a9f9d 100644 --- a/math/primes_up_to_10^8.cpp +++ b/math/primes_up_to_billion.cpp @@ -1,12 +1,12 @@ -#include #include +#include char prime[100000000]; void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index - prime[0] = '0'; // 0 is not prime - prime[1] = '0'; // 1 is not prime + prime[0] = '0'; // 0 is not prime + prime[1] = '0'; // 1 is not prime for (int p = 2; p * p <= n; p++) { if (prime[p] == '1') { for (int i = p * p; i <= n; i += p) @@ -15,7 +15,6 @@ void Sieve(int64_t n) { } } - int main() { Sieve(100000000); int64_t n; From 1e5b9f842da6d3d25ef608f5fbdaf54f38631db8 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:13:08 -0400 Subject: [PATCH 04/14] ignore build directory (cherry picked from commit 0e3a57ab3fdf9bf11a4dd9c3b575656ba50decc3) --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ed3934e45..12318c6cd 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ a.out *.out *.app + +build/ \ No newline at end of file From baea3b07c1588361ed1a514d39a2979c56ff4a6a Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 22:56:38 -0400 Subject: [PATCH 05/14] major corrections in the files - bad CPPLINT and non-compilable codes --- .../Bisection_method.CPP | 75 ++++++++----------- .../Newton_Raphson.CPP | 53 ------------- .../Secant_method.CPP | 70 +++++++---------- .../newton_raphson_method.cpp | 41 ++++++++++ .../successive_approximation.CPP | 63 +++++++--------- math/prime_factorization.cpp | 46 +++++------- 6 files changed, 145 insertions(+), 203 deletions(-) delete mode 100644 computer_oriented_statistical_methods/Newton_Raphson.CPP create mode 100644 computer_oriented_statistical_methods/newton_raphson_method.cpp diff --git a/computer_oriented_statistical_methods/Bisection_method.CPP b/computer_oriented_statistical_methods/Bisection_method.CPP index 717987321..266083de1 100644 --- a/computer_oriented_statistical_methods/Bisection_method.CPP +++ b/computer_oriented_statistical_methods/Bisection_method.CPP @@ -1,53 +1,40 @@ -#include -#include -#include +#include +#include -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation +float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation } -void main() -{ - float a, b, x, z; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } +int main() { + float a, b, x, z; -START: + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - x = (a + b) / 2; - z = eq(x); - cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]"; + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + x = (a + b) / 2; + z = eq(x); + std::cout << "\n\nz: " << z << "\t[" << a << " , " << b + << " | Bisect: " << x << "]"; - if (z < 0) - { - a = x; - } - else - { - b = x; - } + if (z < 0) { + a = x; + } else { + b = x; + } - if (z > 0 && z < 0.0009) // stoping criteria - { - goto END; - } - } + if (z > 0 && z < 0.0009) // stoping criteria + break; + } -END: - cout << "\n\nRoot: " << x; - getch(); + std::cout << "\n\nRoot: " << x; + return 0; } diff --git a/computer_oriented_statistical_methods/Newton_Raphson.CPP b/computer_oriented_statistical_methods/Newton_Raphson.CPP deleted file mode 100644 index 183a78daf..000000000 --- a/computer_oriented_statistical_methods/Newton_Raphson.CPP +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation -} -float eq_der(float i) -{ - return ((3 * pow(i, 2)) - 4); // derivative of equation -} - -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } - -START: - - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - c = (a + b) / 2; - - for (i = 0; i < 100; i++) - { - float h; - m = eq(c); - n = eq_der(c); - - z = c - (m / n); - c = z; - - if (m > 0 && m < 0.009) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << z; - getch(); -} diff --git a/computer_oriented_statistical_methods/Secant_method.CPP b/computer_oriented_statistical_methods/Secant_method.CPP index b897e9184..bd805ed36 100644 --- a/computer_oriented_statistical_methods/Secant_method.CPP +++ b/computer_oriented_statistical_methods/Secant_method.CPP @@ -1,49 +1,37 @@ -#include -#include -#include +#include +#include -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation +float eq(float i) { + return (pow(i, 3) - (4 * i) - 9); // original equation } -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } +int main() { + float a, b, z, c, m, n; + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } -START: + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + float h, d; + m = eq(a); + n = eq(b); - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - float h, d; - m = eq(a); - n = eq(b); + c = ((a * n) - (b * m)) / (n - m); + a = b; + b = c; - c = ((a * n) - (b * m)) / (n - m); - a = b; - b = c; + z = eq(c); + if (z > 0 && z < 0.09) // stoping criteria + break; + } - z = eq(c); - if (z > 0 && z < 0.09) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << c; - getch(); + std::cout << "\n\nRoot: " << c; + return 0; } diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp new file mode 100644 index 000000000..a2a57768e --- /dev/null +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -0,0 +1,41 @@ +#include +#include + +float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation +} +float eq_der(float i) { + return ((3 * std::pow(i, 2)) - 4); // derivative of equation +} + +int main() { + float a, b, z, c, m, n; + + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } + + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + c = (a + b) / 2; + + for (int i = 0; i < 100; i++) { + float h; + m = eq(c); + n = eq_der(c); + + z = c - (m / n); + c = z; + + if (m > 0 && m < 0.009) // stoping criteria + break; + } + + std::cout << "\n\nRoot: " << z << std::endl; + return 0; +} diff --git a/computer_oriented_statistical_methods/successive_approximation.CPP b/computer_oriented_statistical_methods/successive_approximation.CPP index b42ab8f8c..85c42c9fb 100644 --- a/computer_oriented_statistical_methods/successive_approximation.CPP +++ b/computer_oriented_statistical_methods/successive_approximation.CPP @@ -1,37 +1,28 @@ -#include -#include -#include -float eq(float y) -{ - return ((3 * y) - (cos(y)) - 2); + +#include +#include + +float eq(float y) { return ((3 * y) - (cos(y)) - 2); } +float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } + +int main() { + float y, x1, x2, x3, sum, s, a, f1, f2, gd; + int i, n; + + for (i = 0; i < 10; i++) { + sum = eq(y); + std::cout << "value of equation at " << i << " " << sum << "\n"; + y++; + } + std::cout << "enter the x1->"; + std::cin >> x1; + std::cout << "enter the no iteration to perform->\n"; + std::cin >> n; + + for (i = 0; i <= n; i++) { + x2 = eqd(x1); + std::cout << "\nenter the x2->" << x2; + x1 = x2; + } + return 0; } -float eqd(float y) -{ - return ((0.5) * ((cos(y)) + 2)); -} - -void main() -{ - float y, x1, x2, x3, sum, s, a, f1, f2, gd; - int i, n; - - clrscr(); - for (i = 0; i < 10; i++) - { - sum = eq(y); - cout << "value of equation at " << i << " " << sum << "\n"; - y++; - } - cout << "enter the x1->"; - cin >> x1; - cout << "enter the no iteration to perform->\n"; - cin >> n; - - for (i = 0; i <= n; i++) - { - x2 = eqd(x1); - cout << "\nenter the x2->" << x2; - x1 = x2; - } - getch(); -} \ No newline at end of file diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index 822cad332..bd1b99d97 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -1,67 +1,56 @@ +#include +#include #include #include -#include using namespace std; -// Declaring variables for maintaing prime numbers and to check whether a number is prime or not +// Declaring variables for maintaing prime numbers and to check whether a number +// is prime or not bool isprime[1000006]; vector prime_numbers; vector> factors; // Calculating prime number upto a given range -void SieveOfEratosthenes(int N) -{ +void SieveOfEratosthenes(int N) { // initializes the array isprime memset(isprime, true, sizeof isprime); - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - { - for (int j = 2 * i; j <= N; j += i) - isprime[j] = false; + for (int i = 2; i <= N; i++) { + if (isprime[i]) { + for (int j = 2 * i; j <= N; j += i) isprime[j] = false; } } - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - prime_numbers.push_back(i); + for (int i = 2; i <= N; i++) { + if (isprime[i]) prime_numbers.push_back(i); } } // Prime factorization of a number -void prime_factorization(int num) -{ - +void prime_factorization(int num) { int number = num; - for (int i = 0; prime_numbers[i] <= num; i++) - { + for (int i = 0; prime_numbers[i] <= num; i++) { int count = 0; // termination condition - if (number == 1) - { + if (number == 1) { break; } - while (number % prime_numbers[i] == 0) - { + while (number % prime_numbers[i] == 0) { count++; number = number / prime_numbers[i]; } - if (count) - factors.push_back(make_pair(prime_numbers[i], count)); + if (count) factors.push_back(make_pair(prime_numbers[i], count)); } } /* I added a simple UI. */ -int main() -{ +int main() { int num; cout << "\t\tComputes the prime factorization\n\n"; cout << "Type in a number: "; @@ -72,8 +61,7 @@ int main() prime_factorization(num); // Prime factors with their powers in the given number in new line - for (auto it : factors) - { + for (auto it : factors) { cout << it.first << " " << it.second << endl; } From ef24ae3a09a0db7a8988ddf9fcaff985e2d7d43f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 02:57:04 +0000 Subject: [PATCH 06/14] updating DIRECTORY.md --- DIRECTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e36309dc..17cd5f5c6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -13,7 +13,7 @@ * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP) * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) - * [Newton Raphson](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Newton_Raphson.CPP) + * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP) * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP) @@ -116,7 +116,7 @@ * [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp) * [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp) * [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp) - * [Primes Up To 10^8](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_10^8.cpp) + * [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_billion.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp) * [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp) From 4e8a2f715197b92879cf599d9073578b940ab9af Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:12:12 -0400 Subject: [PATCH 07/14] non compiling and illegible code removed --- others/Strassen Matrix Multiplication.cpp | 56 ----------------------- 1 file changed, 56 deletions(-) delete mode 100644 others/Strassen Matrix Multiplication.cpp diff --git a/others/Strassen Matrix Multiplication.cpp b/others/Strassen Matrix Multiplication.cpp deleted file mode 100644 index 85b627763..000000000 --- a/others/Strassen Matrix Multiplication.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -using namespace std; - -Multiply(int A[][], int B[][], int n) -{ - if (n == 2) - { - int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]); - int p2 = (a[1][0] + a[1][1]) * b[0][0]; - int p3 = a[0][0] * (b[0][1] - b[1][1]); - int p4 = a[1][1] * (b[1][0] - b[0][0]); - int p5 = (a[0][0] + a[0][1]) * b[1][1]; - int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]); - int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]); - - int c[n][n]; - c[0][0] = p1 + p4 - p5 + p7; - c[0][1] = p3 + p5; - c[1][0] = p2 + p4; - c[1][1] = p1 - p2 + p3 + p6; - - return c[][]; - } - else - { - } -} - -int main() -{ - int p, q, r, s; - cout << "Enter the dimensions of Matrices"; - cin >> n; - int A[n][n], ; - int B[n][n], ; - cout << "Enter the elements of Matrix A"; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - cin >> A[i][j]; - } - } - - cout << "Enter the elements of Matrix B"; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - cin >> B[i][j]; - } - } - - Multiply(A, B, n); - return 0; -} \ No newline at end of file From 120fe06fcb0d75c944c9453d703a20ce91e46e80 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:13:26 -0400 Subject: [PATCH 08/14] files renamed to standard - without spaces and made CPPLINT compatible --- others/GCD_of_n_numbers.cpp | 41 ++++--- ...al To Binary.cpp => decimal_to_binary.cpp} | 0 ...ecimal .cpp => decimal_to_hexadecimal.cpp} | 0 ...meral.cpp => decimal_to_roman_numeral.cpp} | 0 others/fibonacci.cpp | 42 ------- others/fibonacci_fast.cpp | 37 ++++++ others/matrix_exponentiation.cpp | 14 +-- ... Matching.cpp => paranthesis_matching.cpp} | 0 others/pascal_triangle.cpp | 96 +++++++--------- ...{Primality Test.cpp => primality_test.cpp} | 0 .../{Sparse matrix.cpp => sparse_matrix.cpp} | 0 ...ing Fibonacci.cpp => string_fibonacci.cpp} | 0 ...{Tower of Hanoi.cpp => tower_of_hanoi.cpp} | 0 sorting/shell_sort2.cpp | 105 ++++++++++++++++++ 14 files changed, 211 insertions(+), 124 deletions(-) rename others/{Decimal To Binary.cpp => decimal_to_binary.cpp} (100%) rename others/{Decimal To Hexadecimal .cpp => decimal_to_hexadecimal.cpp} (100%) rename others/{Decimal to Roman Numeral.cpp => decimal_to_roman_numeral.cpp} (100%) delete mode 100644 others/fibonacci.cpp create mode 100644 others/fibonacci_fast.cpp rename others/{Paranthesis Matching.cpp => paranthesis_matching.cpp} (100%) rename others/{Primality Test.cpp => primality_test.cpp} (100%) rename others/{Sparse matrix.cpp => sparse_matrix.cpp} (100%) rename others/{String Fibonacci.cpp => string_fibonacci.cpp} (100%) rename others/{Tower of Hanoi.cpp => tower_of_hanoi.cpp} (100%) create mode 100644 sorting/shell_sort2.cpp diff --git a/others/GCD_of_n_numbers.cpp b/others/GCD_of_n_numbers.cpp index 8158052f8..b592e5931 100644 --- a/others/GCD_of_n_numbers.cpp +++ b/others/GCD_of_n_numbers.cpp @@ -1,23 +1,22 @@ -//This program aims at calculating the GCD of n numbers by division method +// This program aims at calculating the GCD of n numbers by division method #include -using namepsace std; -int main() -{ - cout << "Enter value of n:" << endl; - cin >> n; - int a[n]; - int i, j, gcd; - cout << "Enter the n numbers:" << endl; - for (i = 0; i < n; i++) - cin >> a[i]; - j = 1; //to access all elements of the array starting from 1 - gcd = a[0]; - while (j < n) - { - if (a[j] % gcd == 0) //value of gcd is as needed so far - j++; //so we check for next element - else - gcd = a[j] % gcd; //calculating GCD by division method - } - cout << "GCD of entered n numbers:" << gcd; + +int main() { + int n; + std::cout << "Enter value of n:" << std::endl; + std::cin >> n; + int a[n]; + int i, j, gcd; + std::cout << "Enter the n numbers:" << std::endl; + for (i = 0; i < n; i++) std::cin >> a[i]; + j = 1; // to access all elements of the array starting from 1 + gcd = a[0]; + while (j < n) { + if (a[j] % gcd == 0) // value of gcd is as needed so far + j++; // so we check for next element + else + gcd = a[j] % gcd; // calculating GCD by division method + } + std::cout << "GCD of entered n numbers:" << gcd; + return 0; } diff --git a/others/Decimal To Binary.cpp b/others/decimal_to_binary.cpp similarity index 100% rename from others/Decimal To Binary.cpp rename to others/decimal_to_binary.cpp diff --git a/others/Decimal To Hexadecimal .cpp b/others/decimal_to_hexadecimal.cpp similarity index 100% rename from others/Decimal To Hexadecimal .cpp rename to others/decimal_to_hexadecimal.cpp diff --git a/others/Decimal to Roman Numeral.cpp b/others/decimal_to_roman_numeral.cpp similarity index 100% rename from others/Decimal to Roman Numeral.cpp rename to others/decimal_to_roman_numeral.cpp diff --git a/others/fibonacci.cpp b/others/fibonacci.cpp deleted file mode 100644 index 87ccda6d3..000000000 --- a/others/fibonacci.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation -//This works by using both recursion and dynamic programming. -//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci -//we can use it for 10000th fibonacci etc, if we implement bigintegers. -//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci -//It is a property of fibonacci similar to matrix exponentiation. - -#include -#include -using namespace std; - -const long long MAX = 93; - -long long f[MAX] = {0}; - -long long fib(long long n) -{ - - if (n == 0) - return 0; - if (n == 1 || n == 2) - return (f[n] = 1); - - if (f[n]) - return f[n]; - - long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; - - f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) - : (2 * fib(k - 1) + fib(k)) * fib(k); - return f[n]; -} - -int main() -{ - //Main Function - for (long long i = 1; i < 93; i++) - { - cout << i << " th fibonacci number is " << fib(i) << "\n"; - } - return 0; -} diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp new file mode 100644 index 000000000..dd3481422 --- /dev/null +++ b/others/fibonacci_fast.cpp @@ -0,0 +1,37 @@ +// An efficient way to calculate nth fibonacci number faster and simpler than +// O(nlogn) method of matrix exponentiation This works by using both recursion +// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be +// stored in a single long long variable, we can only use it till 92nd fibonacci +// we can use it for 10000th fibonacci etc, if we implement bigintegers. +// This algorithm works with the fact that nth fibonacci can easily found if we +// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci +// similar to matrix exponentiation. + +#include +#include +using namespace std; + +const long long MAX = 93; + +long long f[MAX] = {0}; + +long long fib(long long n) { + if (n == 0) return 0; + if (n == 1 || n == 2) return (f[n] = 1); + + if (f[n]) return f[n]; + + long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; + + f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) + : (2 * fib(k - 1) + fib(k)) * fib(k); + return f[n]; +} + +int main() { + // Main Function + for (long long i = 1; i < 93; i++) { + cout << i << " th fibonacci number is " << fib(i) << "\n"; + } + return 0; +} diff --git a/others/matrix_exponentiation.cpp b/others/matrix_exponentiation.cpp index 25c411dda..f9b4997e9 100644 --- a/others/matrix_exponentiation.cpp +++ b/others/matrix_exponentiation.cpp @@ -19,6 +19,8 @@ The first element of this matrix is the required result. */ #include +#include + using std::cin; using std::cout; using std::vector; @@ -46,8 +48,7 @@ vector> multiply(vector> A, vector> B) { // computing power of a matrix vector> power(vector> A, ll p) { - if (p == 1) - return A; + if (p == 1) return A; if (p % 2 == 1) { return multiply(A, power(A, p - 1)); } else { @@ -58,14 +59,11 @@ vector> power(vector> A, ll p) { // main function ll ans(ll n) { - if (n == 0) - return 0; - if (n <= k) - return b[n - 1]; + if (n == 0) return 0; + if (n <= k) return b[n - 1]; // F1 vector F1(k + 1); - for (ll i = 1; i <= k; i++) - F1[i] = b[i - 1]; + for (ll i = 1; i <= k; i++) F1[i] = b[i - 1]; // Transpose matrix vector> T(k + 1, vector(k + 1)); diff --git a/others/Paranthesis Matching.cpp b/others/paranthesis_matching.cpp similarity index 100% rename from others/Paranthesis Matching.cpp rename to others/paranthesis_matching.cpp diff --git a/others/pascal_triangle.cpp b/others/pascal_triangle.cpp index 101100018..063a6666a 100644 --- a/others/pascal_triangle.cpp +++ b/others/pascal_triangle.cpp @@ -1,63 +1,53 @@ -#include +#include +#include -using namespace std; - -void show_pascal(int **arr, int n) -{ - //pint Pascal's Triangle - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n + i; ++j) - { - if (arr[i][j] == 0) - cout << " "; - else - cout << arr[i][j]; - } - cout << endl; - } +void show_pascal(int **arr, int n) { + // pint Pascal's Triangle + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n + i; ++j) { + if (arr[i][j] == 0) + std::cout << " "; + else + std::cout << arr[i][j]; + } + std::cout << std::endl; + } } -int **pascal_triangle(int **arr, int n) -{ - for (int i = 0; i < n; ++i) - { - for (int j = n - i - 1; j < n + i; ++j) - { - if (j == n - i - 1 || j == n + i - 1) - arr[i][j] = 1; //The edge of the Pascal triangle goes in 1 - else - arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; - } - } +int **pascal_triangle(int **arr, int n) { + for (int i = 0; i < n; ++i) { + for (int j = n - i - 1; j < n + i; ++j) { + if (j == n - i - 1 || j == n + i - 1) + arr[i][j] = 1; // The edge of the Pascal triangle goes in 1 + else + arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; + } + } - return arr; + return arr; } -int main() -{ - int n = 0; +int main() { + int n = 0; - cout << "Set Pascal's Triangle Height" << endl; - cin >> n; - - //memory allocation (Assign two-dimensional array to store Pascal triangle) - int **arr = new int*[n]; - for (int i = 0; i < n; ++i) - { - arr[i] = new int[2 * n - 1]; - memset(arr[i], 0, sizeof(int)*(2 * n - 1)); - } - - pascal_triangle(arr, n); - show_pascal(arr, n); + std::cout << "Set Pascal's Triangle Height" << std::endl; + std::cin >> n; - //deallocation - for (int i = 0; i < n; ++i) - { - delete[] arr[i]; - } - delete[] arr; + // memory allocation (Assign two-dimensional array to store Pascal triangle) + int **arr = new int *[n]; + for (int i = 0; i < n; ++i) { + arr[i] = new int[2 * n - 1]; + memset(arr[i], 0, sizeof(int) * (2 * n - 1)); + } - return 0; + pascal_triangle(arr, n); + show_pascal(arr, n); + + // deallocation + for (int i = 0; i < n; ++i) { + delete[] arr[i]; + } + delete[] arr; + + return 0; } diff --git a/others/Primality Test.cpp b/others/primality_test.cpp similarity index 100% rename from others/Primality Test.cpp rename to others/primality_test.cpp diff --git a/others/Sparse matrix.cpp b/others/sparse_matrix.cpp similarity index 100% rename from others/Sparse matrix.cpp rename to others/sparse_matrix.cpp diff --git a/others/String Fibonacci.cpp b/others/string_fibonacci.cpp similarity index 100% rename from others/String Fibonacci.cpp rename to others/string_fibonacci.cpp diff --git a/others/Tower of Hanoi.cpp b/others/tower_of_hanoi.cpp similarity index 100% rename from others/Tower of Hanoi.cpp rename to others/tower_of_hanoi.cpp diff --git a/sorting/shell_sort2.cpp b/sorting/shell_sort2.cpp new file mode 100644 index 000000000..1268c7a50 --- /dev/null +++ b/sorting/shell_sort2.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +// for std::swap +#include + +template void show_data(T *arr, size_t LEN) { + size_t i; + + for (i = 0; i < LEN; i++) + std::cout << arr[i] << ", "; + std::cout << std::endl; +} + +template void show_data(T (&arr)[N]) { show_data(arr, N); } + +/** + * Optimized algorithm - takes half the time by utilizing + * Mar + **/ +template void shell_sort(T *arr, size_t LEN) { + const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; + const unsigned int gap_len = 8; + size_t i, j, g; + + for (g = 0; g < gap_len; g++) { + unsigned int gap = gaps[g]; + for (i = gap; i < LEN; i++) { + T tmp = arr[i]; + + for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) + arr[j] = arr[j - gap]; + + arr[j] = tmp; + } + } +} + +template void shell_sort(T (&arr)[N]) { + shell_sort(arr, N); +} + +/** + * function to compare sorting using cstdlib's qsort + **/ +int compare(const void *a, const void *b) { + int arg1 = *static_cast(a); + int arg2 = *static_cast(b); + + if (arg1 < arg2) + return -1; + if (arg1 > arg2) + return 1; + return 0; + + // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut + // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) +} + +int main(int argc, char *argv[]) { + int i, NUM_DATA; + + if (argc == 2) + NUM_DATA = atoi(argv[1]); + else + NUM_DATA = 200; + + // int array = new int[NUM_DATA]; + int *data = new int[NUM_DATA]; + int *data2 = new int[NUM_DATA]; + // int array2 = new int[NUM_DATA]; + int range = 1800; + + std::srand(time(NULL)); + for (i = 0; i < NUM_DATA; i++) + data[i] = data2[i] = (std::rand() % range) - (range >> 1); + + std::cout << "Unsorted original data: " << std::endl; + show_data(data, NUM_DATA); + std::clock_t start = std::clock(); + shell_sort(data, NUM_DATA); + std::clock_t end = std::clock(); + + std::cout << std::endl + << "Data Sorted using custom implementation: " << std::endl; + show_data(data, NUM_DATA); + + double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + start = std::clock(); + qsort(data2, NUM_DATA, sizeof(data2[0]), compare); + end = std::clock(); + std::cout << "Data Sorted using cstdlib qsort: " << std::endl; + show_data(data2, NUM_DATA); + + elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + free(data); + free(data2); + return 0; +} From 15d481f8ca7e7a33b2d567108ea58252ec2318fe Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 03:13:48 +0000 Subject: [PATCH 09/14] updating DIRECTORY.md --- DIRECTORY.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 17cd5f5c6..644c57056 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -133,27 +133,26 @@ ## Others * [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Buzz_number.cpp) - * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Binary.cpp) - * [Decimal To Hexadecimal ](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Hexadecimal%20.cpp) - * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20to%20Roman%20Numeral.cpp) + * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp) + * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp) + * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp) * [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp) - * [Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci.cpp) + * [Fibonacci Fast](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci_fast.cpp) * [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) - * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Paranthesis%20Matching.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%20Test.cpp) + * [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sieve_of_Eratosthenes.cpp) * [Smallest-Circle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/smallest-circle.cpp) - * [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Sparse%20matrix.cpp) + * [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sparse_matrix.cpp) * [Spiral Print](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/spiral_print.cpp) * [Stairs Pattern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/stairs_pattern.cpp) - * [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Strassen%20Matrix%20Multiplication.cpp) - * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/String%20Fibonacci.cpp) - * [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Tower%20of%20Hanoi.cpp) + * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/string_fibonacci.cpp) + * [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/tower_of_hanoi.cpp) * [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp) ## Probability @@ -200,6 +199,7 @@ * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp) * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp) + * [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp) * [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.cpp) * [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp) * [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Tim%20Sort.cpp) From 82dab7515cfc8a0ce11843ecb5bebcec64788c7c Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:22:26 -0400 Subject: [PATCH 10/14] filenames and namespace fixes --- .../{Secant_method.CPP => secant_method.cpp} | 0 ...oximation.CPP => successive_approximation.cpp} | 0 math/prime_factorization.cpp | 15 +++++++-------- others/GCD_of_n_numbers.cpp | 3 ++- others/fibonacci_fast.cpp | 3 +-- 5 files changed, 10 insertions(+), 11 deletions(-) rename computer_oriented_statistical_methods/{Secant_method.CPP => secant_method.cpp} (100%) rename computer_oriented_statistical_methods/{successive_approximation.CPP => successive_approximation.cpp} (100%) diff --git a/computer_oriented_statistical_methods/Secant_method.CPP b/computer_oriented_statistical_methods/secant_method.cpp similarity index 100% rename from computer_oriented_statistical_methods/Secant_method.CPP rename to computer_oriented_statistical_methods/secant_method.cpp diff --git a/computer_oriented_statistical_methods/successive_approximation.CPP b/computer_oriented_statistical_methods/successive_approximation.cpp similarity index 100% rename from computer_oriented_statistical_methods/successive_approximation.CPP rename to computer_oriented_statistical_methods/successive_approximation.cpp diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index bd1b99d97..c018de666 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -2,13 +2,12 @@ #include #include #include -using namespace std; // Declaring variables for maintaing prime numbers and to check whether a number // is prime or not bool isprime[1000006]; -vector prime_numbers; -vector> factors; +std::vector prime_numbers; +std::vector> factors; // Calculating prime number upto a given range void SieveOfEratosthenes(int N) { @@ -43,7 +42,7 @@ void prime_factorization(int num) { number = number / prime_numbers[i]; } - if (count) factors.push_back(make_pair(prime_numbers[i], count)); + if (count) factors.push_back(std::make_pair(prime_numbers[i], count)); } } @@ -52,9 +51,9 @@ void prime_factorization(int num) { */ int main() { int num; - cout << "\t\tComputes the prime factorization\n\n"; - cout << "Type in a number: "; - cin >> num; + std::cout << "\t\tComputes the prime factorization\n\n"; + std::cout << "Type in a number: "; + std::cin >> num; SieveOfEratosthenes(num); @@ -62,7 +61,7 @@ int main() { // Prime factors with their powers in the given number in new line for (auto it : factors) { - cout << it.first << " " << it.second << endl; + std::cout << it.first << " " << it.second << std::endl; } return 0; diff --git a/others/GCD_of_n_numbers.cpp b/others/GCD_of_n_numbers.cpp index b592e5931..37e82173a 100644 --- a/others/GCD_of_n_numbers.cpp +++ b/others/GCD_of_n_numbers.cpp @@ -5,7 +5,7 @@ int main() { int n; std::cout << "Enter value of n:" << std::endl; std::cin >> n; - int a[n]; + int *a = new int[n]; int i, j, gcd; std::cout << "Enter the n numbers:" << std::endl; for (i = 0; i < n; i++) std::cin >> a[i]; @@ -18,5 +18,6 @@ int main() { gcd = a[j] % gcd; // calculating GCD by division method } std::cout << "GCD of entered n numbers:" << gcd; + delete[] a; return 0; } diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp index dd3481422..381a82d7c 100644 --- a/others/fibonacci_fast.cpp +++ b/others/fibonacci_fast.cpp @@ -9,7 +9,6 @@ #include #include -using namespace std; const long long MAX = 93; @@ -31,7 +30,7 @@ long long fib(long long n) { int main() { // Main Function for (long long i = 1; i < 93; i++) { - cout << i << " th fibonacci number is " << fib(i) << "\n"; + std::cout << i << " th fibonacci number is " << fib(i) << "\n"; } return 0; } From 4c7685fdf6f14a6ae9924757f5f9bf05281812d8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 03:22:58 +0000 Subject: [PATCH 11/14] updating DIRECTORY.md --- DIRECTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 644c57056..f21574454 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -15,8 +15,8 @@ * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) - * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP) - * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP) + * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/secant_method.cpp) + * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.cpp) ## Data Structure * [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structure/AVLtree.cpp) From c93f3ef35eb05cef312be1cab83bafda83f6011b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:25:44 -0400 Subject: [PATCH 12/14] fix inttypes --- others/fibonacci_fast.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp index 381a82d7c..a0b83b640 100644 --- a/others/fibonacci_fast.cpp +++ b/others/fibonacci_fast.cpp @@ -7,20 +7,21 @@ // have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci // similar to matrix exponentiation. +#include #include #include -const long long MAX = 93; +const uint64_t MAX = 93; -long long f[MAX] = {0}; +uint64_t f[MAX] = {0}; -long long fib(long long n) { +uint64_t fib(uint64_t n) { if (n == 0) return 0; if (n == 1 || n == 2) return (f[n] = 1); if (f[n]) return f[n]; - long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; + uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) : (2 * fib(k - 1) + fib(k)) * fib(k); @@ -29,7 +30,7 @@ long long fib(long long n) { int main() { // Main Function - for (long long i = 1; i < 93; i++) { + for (uint64_t i = 1; i < 93; i++) { std::cout << i << " th fibonacci number is " << fib(i) << "\n"; } return 0; From b9bb6caa73083da8fc0d74d366dd9262da081c3d Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 25 May 2020 23:32:31 -0400 Subject: [PATCH 13/14] made functions static to files *BUG* in CPP lint github action --- .../Gaussian_elimination.cpp | 52 ++++++++----------- ...ection_method.CPP => bisection_method.cpp} | 2 +- .../false-position.cpp | 21 ++++---- .../newton_raphson_method.cpp | 5 +- .../secant_method.cpp | 2 +- .../successive_approximation.cpp | 5 +- 6 files changed, 40 insertions(+), 47 deletions(-) rename computer_oriented_statistical_methods/{Bisection_method.CPP => bisection_method.cpp} (96%) diff --git a/computer_oriented_statistical_methods/Gaussian_elimination.cpp b/computer_oriented_statistical_methods/Gaussian_elimination.cpp index 58e634505..ecfbfacc9 100644 --- a/computer_oriented_statistical_methods/Gaussian_elimination.cpp +++ b/computer_oriented_statistical_methods/Gaussian_elimination.cpp @@ -1,29 +1,23 @@ #include -using namespace std; -int main() -{ +int main() { int mat_size, i, j, step; - cout << "Matrix size: "; - cin >> mat_size; + std::cout << "Matrix size: "; + std::cin >> mat_size; double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1]; - cout << endl - << "Enter value of the matrix: " << endl; - for (i = 0; i < mat_size; i++) - { - for (j = 0; j <= mat_size; j++) - { - cin >> mat[i][j]; //Enter (mat_size*mat_size) value of the matrix. + std::cout << std::endl << "Enter value of the matrix: " << std::endl; + for (i = 0; i < mat_size; i++) { + for (j = 0; j <= mat_size; j++) { + std::cin >> + mat[i][j]; // Enter (mat_size*mat_size) value of the matrix. } } - for (step = 0; step < mat_size - 1; step++) - { - for (i = step; i < mat_size - 1; i++) - { + for (step = 0; step < mat_size - 1; step++) { + for (i = step; i < mat_size - 1; i++) { double a = (mat[i + 1][step] / mat[step][step]); for (j = step; j <= mat_size; j++) @@ -31,24 +25,20 @@ int main() } } - cout << endl - << "Matrix using Gaussian Elimination method: " << endl; - for (i = 0; i < mat_size; i++) - { - for (j = 0; j <= mat_size; j++) - { + std::cout << std::endl + << "Matrix using Gaussian Elimination method: " << std::endl; + for (i = 0; i < mat_size; i++) { + for (j = 0; j <= mat_size; j++) { x[i][j] = mat[i][j]; - cout << mat[i][j] << " "; + std::cout << mat[i][j] << " "; } - cout << endl; + std::cout << std::endl; } - cout << endl - << "Value of the Gaussian Elimination method: " << endl; - for (i = mat_size - 1; i >= 0; i--) - { + std::cout << std::endl + << "Value of the Gaussian Elimination method: " << std::endl; + for (i = mat_size - 1; i >= 0; i--) { double sum = 0; - for (j = mat_size - 1; j > i; j--) - { + for (j = mat_size - 1; j > i; j--) { x[i][j] = x[j][j] * x[i][j]; sum = x[i][j] + sum; } @@ -57,7 +47,7 @@ int main() else x[i][i] = (x[i][mat_size] - sum) / (x[i][i]); - cout << "x" << i << "= " << x[i][i] << endl; + std::cout << "x" << i << "= " << x[i][i] << std::endl; } return 0; } diff --git a/computer_oriented_statistical_methods/Bisection_method.CPP b/computer_oriented_statistical_methods/bisection_method.cpp similarity index 96% rename from computer_oriented_statistical_methods/Bisection_method.CPP rename to computer_oriented_statistical_methods/bisection_method.cpp index 266083de1..089828c9a 100644 --- a/computer_oriented_statistical_methods/Bisection_method.CPP +++ b/computer_oriented_statistical_methods/bisection_method.cpp @@ -1,7 +1,7 @@ #include #include -float eq(float i) { +static float eq(float i) { return (std::pow(i, 3) - (4 * i) - 9); // original equation } diff --git a/computer_oriented_statistical_methods/false-position.cpp b/computer_oriented_statistical_methods/false-position.cpp index 5e15e92cc..c5a314508 100644 --- a/computer_oriented_statistical_methods/false-position.cpp +++ b/computer_oriented_statistical_methods/false-position.cpp @@ -1,9 +1,11 @@ -#include -#include +#include +#include #include -float eq(float i) { + +static float eq(float i) { return (pow(i, 3) - (4 * i) - 9); // origial equation } + int main() { float a, b, z, c, m, n; system("clear"); @@ -12,12 +14,13 @@ int main() { if (z >= 0) { b = i; a = --i; - goto START; - } + break; } - START: + } + std::cout << "\nFirst initial: " << a; std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { float h, d; m = eq(a); @@ -26,10 +29,10 @@ int main() { a = c; z = eq(c); if (z > 0 && z < 0.09) { // stoping criteria - goto END; + break; } } - END: + std::cout << "\n\nRoot: " << c; - system("pause"); + return 0; } diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp index a2a57768e..47d276490 100644 --- a/computer_oriented_statistical_methods/newton_raphson_method.cpp +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -1,10 +1,11 @@ #include #include -float eq(float i) { +static float eq(float i) { return (std::pow(i, 3) - (4 * i) - 9); // original equation } -float eq_der(float i) { + +static float eq_der(float i) { return ((3 * std::pow(i, 2)) - 4); // derivative of equation } diff --git a/computer_oriented_statistical_methods/secant_method.cpp b/computer_oriented_statistical_methods/secant_method.cpp index bd805ed36..c353ef850 100644 --- a/computer_oriented_statistical_methods/secant_method.cpp +++ b/computer_oriented_statistical_methods/secant_method.cpp @@ -1,7 +1,7 @@ #include #include -float eq(float i) { +static float eq(float i) { return (pow(i, 3) - (4 * i) - 9); // original equation } diff --git a/computer_oriented_statistical_methods/successive_approximation.cpp b/computer_oriented_statistical_methods/successive_approximation.cpp index 85c42c9fb..efbcc3bbf 100644 --- a/computer_oriented_statistical_methods/successive_approximation.cpp +++ b/computer_oriented_statistical_methods/successive_approximation.cpp @@ -1,9 +1,8 @@ - #include #include -float eq(float y) { return ((3 * y) - (cos(y)) - 2); } -float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } +static float eq(float y) { return ((3 * y) - (cos(y)) - 2); } +static float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } int main() { float y, x1, x2, x3, sum, s, a, f1, f2, gd; From 2e2f9c209ca0ea04e59117859f2f0f08f638fbf4 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 03:32:54 +0000 Subject: [PATCH 14/14] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index f21574454..26210d6d4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,7 +10,7 @@ * [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp) ## Computer Oriented Statistical Methods - * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP) + * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp) * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp)