From cd71ae9453171c411736be3ab6211d91794e0039 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Wed, 2 Sep 2026 19:52:48 -0300 Subject: [PATCH 1/8] feat: add caesar cipher algorithm --- cipher/caesar_cipher.c | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 cipher/caesar_cipher.c diff --git a/cipher/caesar_cipher.c b/cipher/caesar_cipher.c new file mode 100644 index 0000000000..3fa9b76680 --- /dev/null +++ b/cipher/caesar_cipher.c @@ -0,0 +1,92 @@ +/** + * @file + * @brief A [caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) is a + * substitution cipher that replaces each letter in a text by another located + * a given number of positions along the alphabet. + * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) + */ + +#include /// for strcmp +#include /// for Input/Output +#include /// for isalpha, isupper and islower +#include /// for assert + +#define MAX_STRING_LENGTH 99 // max length of a string or a char * + +/* + * @brief calculates the encrypted char for the encryption + * @param c the character to be used + * @param key the key to be used + * @returns the encrypted char + */ +int calculate_offset(char c, int key) +{ + int new_c; + + // calculates the offset based on the character's case + if (isupper(c)) + { + new_c = (((int)c - 'A') + key) % 26; + } + else + { + new_c = (((int)c - 'a') + key) % 26; + } + + if (new_c < 0) new_c += 26; + + return isupper(c) ? new_c + 'A' : new_c + 'a'; +} + +/* @brief encrypts the passed text using the passed key integer variable + * @param text the text to be encrypted + * @param key used for shifting a letter, shifts left when negative, right + * when positive + * @returns void + */ +void caesar_cipher(char *text, int key) +{ + for (int i = 0; text[i] != '\0'; i++) + { + if (isalpha(text[i])) text[i] = calculate_offset(text[i], key); + } +} + +/* + * @brief tests the caesar_cipher function by passing some strings to it + * @returns void + */ +void test(void) +{ + char first[] = "ILOVETV"; + char second[] = "ILOVETV"; + char third[] = "The smoke... was blue."; + char fourth[] = "They think they are the fuckin big shot, like the world revolves around them. It\'s funny at times though x\'D"; + + caesar_cipher(first, 15); + assert(strcmp(first, "XADKTIK") == 0); + printf("First test OK\n"); + + caesar_cipher(second, -15); + assert(strcmp(second, "TWZGPEG") == 0); + printf("Second test OK\n"); + + caesar_cipher(third, 35); + assert(strcmp(third, "Cqn bvxtn... fjb kudn.") == 0); + printf("Third test OK\n"); + + caesar_cipher(fourth, -81); + assert(strcmp(fourth, "Qebv qefkh qebv xob qeb crzhfk yfd pelq, ifhb qeb tloia obslisbp xolrka qebj. Fq\'p crkkv xq qfjbp qelrde u\'A") == 0); + printf("Fourth test OK\n"); +} + +/* + * @brief main function; calls the test function + * @returns 0 if all tests pass + */ +int main(void) +{ + test(); + + return 0; +} From 38cdefd9f603e3d547b112bca0bdbcd107856278 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Thu, 3 Sep 2026 23:12:15 -0300 Subject: [PATCH 2/8] docs: clarify documentation --- cipher/caesar_cipher.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/cipher/caesar_cipher.c b/cipher/caesar_cipher.c index 3fa9b76680..579688f685 100644 --- a/cipher/caesar_cipher.c +++ b/cipher/caesar_cipher.c @@ -2,45 +2,39 @@ * @file * @brief A [caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) is a * substitution cipher that replaces each letter in a text by another located - * a given number of positions along the alphabet. + * a given number of positions along the alphabet. * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) */ #include /// for strcmp #include /// for Input/Output -#include /// for isalpha, isupper and islower +#include /// for isalpha, isupper, islower and tolower #include /// for assert -#define MAX_STRING_LENGTH 99 // max length of a string or a char * - -/* +/** * @brief calculates the encrypted char for the encryption * @param c the character to be used * @param key the key to be used * @returns the encrypted char */ -int calculate_offset(char c, int key) +int calculate_encrypted_char(char c, int key) { int new_c; - // calculates the offset based on the character's case - if (isupper(c)) - { - new_c = (((int)c - 'A') + key) % 26; - } - else - { - new_c = (((int)c - 'a') + key) % 26; - } + new_c = ((tolower(c) - 'a') + key) % 26; + // because the % operator sucks if (new_c < 0) new_c += 26; return isupper(c) ? new_c + 'A' : new_c + 'a'; } -/* @brief encrypts the passed text using the passed key integer variable +/** + * @brief encrypts the passed text by calling and assigning the return value + * of calculate_encrypted_char for each alphabetical character in the passed + * text string * @param text the text to be encrypted - * @param key used for shifting a letter, shifts left when negative, right + * @param key used for shifting a letter - shifts left when negative, right * when positive * @returns void */ @@ -48,11 +42,14 @@ void caesar_cipher(char *text, int key) { for (int i = 0; text[i] != '\0'; i++) { - if (isalpha(text[i])) text[i] = calculate_offset(text[i], key); + if (isalpha(text[i])) + { + text[i] = calculate_encrypted_char(text[i], key); + } } } -/* +/** * @brief tests the caesar_cipher function by passing some strings to it * @returns void */ @@ -62,6 +59,7 @@ void test(void) char second[] = "ILOVETV"; char third[] = "The smoke... was blue."; char fourth[] = "They think they are the fuckin big shot, like the world revolves around them. It\'s funny at times though x\'D"; + char fifth[] = "My compass was swallowed by the sea..."; caesar_cipher(first, 15); assert(strcmp(first, "XADKTIK") == 0); @@ -78,9 +76,13 @@ void test(void) caesar_cipher(fourth, -81); assert(strcmp(fourth, "Qebv qefkh qebv xob qeb crzhfk yfd pelq, ifhb qeb tloia obslisbp xolrka qebj. Fq\'p crkkv xq qfjbp qelrde u\'A") == 0); printf("Fourth test OK\n"); + + caesar_cipher(fifth, 0); + assert(strcmp(fifth, "My compass was swallowed by the sea...") == 0); + printf("Fifth test OK\n"); } -/* +/** * @brief main function; calls the test function * @returns 0 if all tests pass */ From d218d7496fbf19dae79557e92a4e6f26602f68c3 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Wed, 16 Sep 2026 09:11:03 -0300 Subject: [PATCH 3/8] docs: clarify comments --- cipher/caesar_cipher.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/cipher/caesar_cipher.c b/cipher/caesar_cipher.c index 579688f685..f846cf653f 100644 --- a/cipher/caesar_cipher.c +++ b/cipher/caesar_cipher.c @@ -1,43 +1,45 @@ /** * @file - * @brief A [caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) is a - * substitution cipher that replaces each letter in a text by another located + * @brief A [caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) is a + * substitution cipher that replaces each letter in a text by another located * a given number of positions along the alphabet. * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) */ #include /// for strcmp #include /// for Input/Output -#include /// for isalpha, isupper, islower and tolower +#include /// for isalpha, isupper and tolower #include /// for assert /** * @brief calculates the encrypted char for the encryption * @param c the character to be used * @param key the key to be used - * @returns the encrypted char + * @returns the encrypted char (case-sensitive) */ int calculate_encrypted_char(char c, int key) { int new_c; - + + // calculates the offset new_c = ((tolower(c) - 'a') + key) % 26; - - // because the % operator sucks + + // (because the % operator doesn't handle negative left inputs) + // puts new_c back on the 0-26 range if it turned negative if (new_c < 0) new_c += 26; - + return isupper(c) ? new_c + 'A' : new_c + 'a'; } /** - * @brief encrypts the passed text by calling and assigning the return value - * of calculate_encrypted_char for each alphabetical character in the passed + * @brief encrypts the passed text by calling and assigning the return value + * of calculate_encrypted_char for each alphabetical character in the passed * text string * @param text the text to be encrypted - * @param key used for shifting a letter - shifts left when negative, right + * @param key used for shifting a letter - shifts left when negative, right * when positive * @returns void - */ + */ void caesar_cipher(char *text, int key) { for (int i = 0; text[i] != '\0'; i++) @@ -60,23 +62,23 @@ void test(void) char third[] = "The smoke... was blue."; char fourth[] = "They think they are the fuckin big shot, like the world revolves around them. It\'s funny at times though x\'D"; char fifth[] = "My compass was swallowed by the sea..."; - + caesar_cipher(first, 15); assert(strcmp(first, "XADKTIK") == 0); printf("First test OK\n"); - + caesar_cipher(second, -15); assert(strcmp(second, "TWZGPEG") == 0); printf("Second test OK\n"); - + caesar_cipher(third, 35); assert(strcmp(third, "Cqn bvxtn... fjb kudn.") == 0); printf("Third test OK\n"); - + caesar_cipher(fourth, -81); assert(strcmp(fourth, "Qebv qefkh qebv xob qeb crzhfk yfd pelq, ifhb qeb tloia obslisbp xolrka qebj. Fq\'p crkkv xq qfjbp qelrde u\'A") == 0); printf("Fourth test OK\n"); - + caesar_cipher(fifth, 0); assert(strcmp(fifth, "My compass was swallowed by the sea...") == 0); printf("Fifth test OK\n"); @@ -89,6 +91,6 @@ void test(void) int main(void) { test(); - + return 0; } From 55d4d2d8699fc7dc2b5cec7096fa35a5601d9da2 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Mon, 28 Sep 2026 14:56:03 -0300 Subject: [PATCH 4/8] feat: add newton_raphson_root_2.c file Added alternate implementation --- numerical_methods/newton_raphson_root_2.c | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 numerical_methods/newton_raphson_root_2.c diff --git a/numerical_methods/newton_raphson_root_2.c b/numerical_methods/newton_raphson_root_2.c new file mode 100644 index 0000000000..0a948f698c --- /dev/null +++ b/numerical_methods/newton_raphson_root_2.c @@ -0,0 +1,43 @@ +#include +#include +#include + +float newtons_method(float a, float diff) +{ + float x_n = a; + float x_nplus1; + + for (int i = 1; i <= log(a) * 20; i++) + { + x_nplus1 = 0.5 * (x_n + a / x_n); + + if (fabs(x_nplus1 - x_n) <= diff) break; + + x_n = x_nplus1; + } + + return x_nplus1; +} + +int test(float test[]) +{ + return fabs(newtons_method(test[0], test[1]) - sqrtf(test[0])) <= test[1]; +} + +int main(void) +{ + float test1[] = {625, 0.1}; + float test2[] = {2, 0.001}; + float test3[] = {1024, 0}; + + assert(test(test1)); + printf("Test 1 OK\n"); + + assert(test(test2)); + printf("Test 2 OK\n"); + + assert(test(test3)); + printf("Test 3 OK\n"); + + return 0; +} From d7e800b39d1f4d19db4fd8fc081706b2d4c98959 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Mon, 28 Sep 2026 16:02:52 -0300 Subject: [PATCH 5/8] docs: add comments --- numerical_methods/newton_raphson_root_2.c | 88 +++++++++++++++++++---- 1 file changed, 74 insertions(+), 14 deletions(-) diff --git a/numerical_methods/newton_raphson_root_2.c b/numerical_methods/newton_raphson_root_2.c index 0a948f698c..279d0326f7 100644 --- a/numerical_methods/newton_raphson_root_2.c +++ b/numerical_methods/newton_raphson_root_2.c @@ -1,43 +1,103 @@ -#include -#include -#include +/** + * @file + * + * + * @brief The [Newton-Raphson method](https://en.wikipedia.org/wiki/Newton%27s_method) + * is a root-finding algorithm which successively produces better + * approximations to the roots of a real-valued function. + * + * + * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) + */ +#include // for Input/Output +#include // for fabs, sqrtf and log +#include // for assert + +/** + * @brief Produces better approximations of the root of a. + * + * @details Assigns the next approximation to x_nplus1. If the absolute + * difference between the next approximation and the current is less than + * or equal to the provided diff parameter, or if the number of iterations is + * greater than the logarithm of the a paramater multiplied by 20, + * the for loop breaks. Else, the value of x_nplus1 gets assigned to x_n and + * the loop continues. + * + * + * @param a the value to find the approximate root of + * + * @param diff the maximum deviation to be accepted + * + * + * @returns the best approximation of the root + */ float newtons_method(float a, float diff) { float x_n = a; float x_nplus1; - for (int i = 1; i <= log(a) * 20; i++) + for (int i = 1; 1; i++) { x_nplus1 = 0.5 * (x_n + a / x_n); - if (fabs(x_nplus1 - x_n) <= diff) break; + if (i > log(a) * 20 || fabs(x_nplus1 - x_n) <= diff) break; x_n = x_nplus1; } return x_nplus1; } - -int test(float test[]) +/** + * @brief Tests if the implementation is within an acceptable range. + * + * @details Checks if the absolute difference between the return value of + * newtons_method and the square root is less than or equal to the passed + * range. + * + * + * @param test[] an array containing two values; the first is the number to + * get the root of and the second is the maximum deviation to be accepted + * + * + * @returns whether the aformentioned check is true + */ +int individual_test(float test[]) { return fabs(newtons_method(test[0], test[1]) - sqrtf(test[0])) <= test[1]; } -int main(void) -{ - float test1[] = {625, 0.1}; +/** + * @brief passes and asserts some tests to the individual_test() function + * + * + * @returns void + */ +void test(void) +{ + float test1[] = {625, 0.1}; float test2[] = {2, 0.001}; float test3[] = {1024, 0}; - assert(test(test1)); + assert(individual_test(test1)); printf("Test 1 OK\n"); - assert(test(test2)); + assert(individual_test(test2)); printf("Test 2 OK\n"); - assert(test(test3)); - printf("Test 3 OK\n"); + assert(individual_test(test3)); + printf("Test 3 OK\n"); +} + +/** + * @brief calls the test() function + * + * + * @returns 0 if all tests passed + */ +int main(void) +{ + test(); return 0; } From c935f373fbb0cdb6334510ac49b6227b9193d239 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Wed, 30 Sep 2026 08:46:44 -0300 Subject: [PATCH 6/8] docs: clarify comments --- numerical_methods/newton_raphson_root_2.c | 93 ++++++++++++----------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/numerical_methods/newton_raphson_root_2.c b/numerical_methods/newton_raphson_root_2.c index 279d0326f7..c4ddfe71d7 100644 --- a/numerical_methods/newton_raphson_root_2.c +++ b/numerical_methods/newton_raphson_root_2.c @@ -1,47 +1,49 @@ /** * @file - * - * + * + * * @brief The [Newton-Raphson method](https://en.wikipedia.org/wiki/Newton%27s_method) * is a root-finding algorithm which successively produces better * approximations to the roots of a real-valued function. - * - * + * + * * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) */ #include // for Input/Output -#include // for fabs, sqrtf and log +#include // for fabs, sqrtf and logf #include // for assert /** * @brief Produces better approximations of the root of a. - * - * @details Assigns the next approximation to x_nplus1. If the absolute + * + * @details Assigns the absolute value of the a parameter to itself. Inside + * the loop, assigns the next approximation to x_nplus1. If the absolute * difference between the next approximation and the current is less than - * or equal to the provided diff parameter, or if the number of iterations is - * greater than the logarithm of the a paramater multiplied by 20, - * the for loop breaks. Else, the value of x_nplus1 gets assigned to x_n and - * the loop continues. - * - * - * @param a the value to find the approximate root of - * + * or equal to the diff parameter, or if the number of iterations is greater + * than the logarithm of the a paramater multiplied by 20, the for loop breaks. + * Else, the value of x_nplus1 gets assigned to x_n and the loop continues. + * + * + * @param a the positive value to find the approximate root of + * * @param diff the maximum deviation to be accepted - * - * + * + * * @returns the best approximation of the root */ -float newtons_method(float a, float diff) +double newtons_method(double a, double diff) { - float x_n = a; - float x_nplus1; + a = fabs(a); + + double x_n = a; + double x_nplus1; for (int i = 1; 1; i++) { x_nplus1 = 0.5 * (x_n + a / x_n); - if (i > log(a) * 20 || fabs(x_nplus1 - x_n) <= diff) break; + if (i > logf(a) * 20 || fabs(x_nplus1 - x_n) <= diff) break; x_n = x_nplus1; } @@ -50,49 +52,54 @@ float newtons_method(float a, float diff) } /** * @brief Tests if the implementation is within an acceptable range. - * - * @details Checks if the absolute difference between the return value of - * newtons_method and the square root is less than or equal to the passed - * range. - * - * + * + * @details Checks if the absolute difference between the return value + * of newtons_method and the square root is less than or equal to the + * passed range. + * + * * @param test[] an array containing two values; the first is the number to * get the root of and the second is the maximum deviation to be accepted - * - * - * @returns whether the aformentioned check is true + * + * + * @returns whether the aforementioned check is true */ -int individual_test(float test[]) +int individual_test(double test[]) { - return fabs(newtons_method(test[0], test[1]) - sqrtf(test[0])) <= test[1]; + return fabs(newtons_method(test[0], test[1]) - sqrtf(fabs(test[0]))) <= test[1]; } /** - * @brief passes and asserts some tests to the individual_test() function - * - * + * @brief passes 2-element arrays to the individual_test() function + * for validation + * + * * @returns void */ void test(void) -{ - float test1[] = {625, 0.1}; - float test2[] = {2, 0.001}; - float test3[] = {1024, 0}; +{ + double test1[] = {625, 0.1}; + double test2[] = {2, 0.001}; + double test3[] = {1024, 0}; + double test4[] = {-25, 0}; assert(individual_test(test1)); printf("Test 1 OK\n"); assert(individual_test(test2)); printf("Test 2 OK\n"); - + assert(individual_test(test3)); - printf("Test 3 OK\n"); + printf("Test 3 OK\n"); + + assert(individual_test(test4)); + printf("Test 4 OK\n"); } /** * @brief calls the test() function - * - * + * + * * @returns 0 if all tests passed */ int main(void) From d8d550b160393536945792b1c77f169e2a23844d Mon Sep 17 00:00:00 2001 From: agbi623 Date: Thu, 1 Oct 2026 02:07:20 -0300 Subject: [PATCH 7/8] feat: add visualization feature Added the ability for the user to specify whether the functionality should be visualized via printf. --- numerical_methods/newton_raphson_root_2.c | 83 ++++++++++++++++------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/numerical_methods/newton_raphson_root_2.c b/numerical_methods/newton_raphson_root_2.c index c4ddfe71d7..1e4d060db5 100644 --- a/numerical_methods/newton_raphson_root_2.c +++ b/numerical_methods/newton_raphson_root_2.c @@ -12,6 +12,7 @@ #include // for Input/Output #include // for fabs, sqrtf and logf +#include // for tolower #include // for assert /** @@ -23,30 +24,41 @@ * or equal to the diff parameter, or if the number of iterations is greater * than the logarithm of the a paramater multiplied by 20, the for loop breaks. * Else, the value of x_nplus1 gets assigned to x_n and the loop continues. + * If the visualize parameter is 1 (true), each approximation is printed. * * * @param a the positive value to find the approximate root of * * @param diff the maximum deviation to be accepted + * + * @param visualize whether the user chose to visualize the process * * * @returns the best approximation of the root */ -double newtons_method(double a, double diff) +double newtons_method(double a, double diff, int visualize) { a = fabs(a); double x_n = a; double x_nplus1; + + int i; - for (int i = 1; 1; i++) + if (visualize) printf("Start of approximations\n"); + + for (i = 1; 1; i++) { + if (visualize) printf("x_%d: %.12lf\n", i - 1, x_n); + x_nplus1 = 0.5 * (x_n + a / x_n); if (i > logf(a) * 20 || fabs(x_nplus1 - x_n) <= diff) break; x_n = x_nplus1; } + + if (visualize) printf("Final approximation (x_%d): %.12lf\n\n", i, x_nplus1); return x_nplus1; } @@ -55,56 +67,79 @@ double newtons_method(double a, double diff) * * @details Checks if the absolute difference between the return value * of newtons_method and the square root is less than or equal to the - * passed range. - * - * + * passed deviation tolerance. + * + * * @param test[] an array containing two values; the first is the number to * get the root of and the second is the maximum deviation to be accepted + * + * @param visualize whether the user chose to visualize the process * * * @returns whether the aforementioned check is true */ -int individual_test(double test[]) +int individual_test(double test[], int visualize) { - return fabs(newtons_method(test[0], test[1]) - sqrtf(fabs(test[0]))) <= test[1]; + return fabs(newtons_method(test[0], test[1], visualize) - sqrtf(fabs(test[0]))) <= test[1]; } /** - * @brief passes 2-element arrays to the individual_test() function - * for validation + * @brief Tests a given number of values. + * + * @details Iterates through the tests[] array and passes each element to + * individual_test(). If the visualize parameter is 1 (true), the current test, + * the number to take the root of and the accepted deviation are printed. + * + * + * @param visualize whether the user chose to visualize the process * * * @returns void */ -void test(void) +void test(int visualize) { double test1[] = {625, 0.1}; double test2[] = {2, 0.001}; double test3[] = {1024, 0}; double test4[] = {-25, 0}; - - assert(individual_test(test1)); - printf("Test 1 OK\n"); - - assert(individual_test(test2)); - printf("Test 2 OK\n"); - - assert(individual_test(test3)); - printf("Test 3 OK\n"); - - assert(individual_test(test4)); - printf("Test 4 OK\n"); + + double *tests[] = {test1, test2, test3, test4}; + + int tests_num = 4; + + for (int i = 0; i < tests_num; i++) + { + if (visualize) + { + printf("------------\n"); + printf("Test %d\n", i + 1); + printf("a: %.12lf\n", tests[i][0]); + printf("Precision: %.12lf\n\n", tests[i][1]); + } + + assert(individual_test(tests[i], visualize)); + printf("Test %d OK\n", i + 1); + + if (visualize) printf("\n"); + } } /** - * @brief calls the test() function + * @brief asks if the user would like to visualize + * then calls the test() function * * * @returns 0 if all tests passed */ int main(void) { - test(); + int visualize = 0; + + printf("Would you like to see the process? (y/N) - "); + if (tolower(getchar()) == 'y') visualize = 1; + printf("\n"); + + test(visualize); return 0; } From 43d849fe49f041d38627da0daceed9b96ed8a60d Mon Sep 17 00:00:00 2001 From: agbi623 Date: Tue, 6 Oct 2026 18:01:43 -0300 Subject: [PATCH 8/8] chore: various changes in individual_test(): - chore: separation of the return into variables in test(): - test: added a 5th test - chore: changed the tests_num variable to calculate the length of the tests[] array - chore: added a printf() to mark the end of the tests in newtons_method(): - chore: added a stop condition to the for loop docs: changed comments --- numerical_methods/newton_raphson_root_2.c | 85 +++++++++++++---------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/numerical_methods/newton_raphson_root_2.c b/numerical_methods/newton_raphson_root_2.c index 1e4d060db5..057ab4c2a2 100644 --- a/numerical_methods/newton_raphson_root_2.c +++ b/numerical_methods/newton_raphson_root_2.c @@ -1,12 +1,12 @@ /** * @file - * - * + * + * * @brief The [Newton-Raphson method](https://en.wikipedia.org/wiki/Newton%27s_method) * is a root-finding algorithm which successively produces better * approximations to the roots of a real-valued function. - * - * + * + * * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) */ @@ -17,23 +17,22 @@ /** * @brief Produces better approximations of the root of a. - * + * * @details Assigns the absolute value of the a parameter to itself. Inside * the loop, assigns the next approximation to x_nplus1. If the absolute * difference between the next approximation and the current is less than - * or equal to the diff parameter, or if the number of iterations is greater - * than the logarithm of the a paramater multiplied by 20, the for loop breaks. - * Else, the value of x_nplus1 gets assigned to x_n and the loop continues. - * If the visualize parameter is 1 (true), each approximation is printed. - * - * + * or equal to the diff parameter, the for loop breaks. Else, the value of + * x_nplus1 gets assigned to x_n and the loop continues. If the visualize + * parameter is 1 (true), each approximation is printed. + * + * * @param a the positive value to find the approximate root of - * + * * @param diff the maximum deviation to be accepted * * @param visualize whether the user chose to visualize the process - * - * + * + * * @returns the best approximation of the root */ double newtons_method(double a, double diff, int visualize) @@ -47,13 +46,13 @@ double newtons_method(double a, double diff, int visualize) if (visualize) printf("Start of approximations\n"); - for (i = 1; 1; i++) + for (i = 1; i <= logf(a) * 20; i++) { if (visualize) printf("x_%d: %.12lf\n", i - 1, x_n); x_nplus1 = 0.5 * (x_n + a / x_n); - if (i > logf(a) * 20 || fabs(x_nplus1 - x_n) <= diff) break; + if (fabs(x_nplus1 - x_n) <= diff) break; x_n = x_nplus1; } @@ -63,37 +62,47 @@ double newtons_method(double a, double diff, int visualize) return x_nplus1; } /** - * @brief Tests if the implementation is within an acceptable range. - * - * @details Checks if the absolute difference between the return value - * of newtons_method and the square root is less than or equal to the - * passed deviation tolerance. + * @brief Tests if the implementation of the Newton-Raphson method is valid. + * + * @details Checks if the absolute difference between the return value of + * newtons_method and the square root is less than or equal to the passed + * deviation tolerance within the test[] parameter. * * * @param test[] an array containing two values; the first is the number to - * get the root of and the second is the maximum deviation to be accepted + * get the root of and the second is the maximum deviation from the square + * root to be accepted * * @param visualize whether the user chose to visualize the process - * - * + * + * * @returns whether the aforementioned check is true */ int individual_test(double test[], int visualize) { - return fabs(newtons_method(test[0], test[1], visualize) - sqrtf(fabs(test[0]))) <= test[1]; + double function_return = newtons_method(test[0], test[1], visualize); + double square_root = sqrtf(fabs(test[0])); + double abs_diff = fabs(function_return - square_root); + + return abs_diff <= test[1]; } /** * @brief Tests a given number of values. * - * @details Iterates through the tests[] array and passes each element to - * individual_test(). If the visualize parameter is 1 (true), the current test, - * the number to take the root of and the accepted deviation are printed. + * @details Iterates through the tests[] array and passes each element to + * individual_test(). Calculates the number of elements inside the tests[] + * array. Inside the loop, if the visualize parameter is 1 (true), the current + * test, the number to take the root of and the accepted deviation are + * printed. Then, the current element of the tests[] array and the visualize + * parameter are passed to the individual_test() function, in that order. If + * this function returns 0, the program halts because of assert. If not, the + * loop continues. * * * @param visualize whether the user chose to visualize the process - * - * + * + * * @returns void */ void test(int visualize) @@ -102,16 +111,17 @@ void test(int visualize) double test2[] = {2, 0.001}; double test3[] = {1024, 0}; double test4[] = {-25, 0}; + double test5[] = {4294967296, 0}; - double *tests[] = {test1, test2, test3, test4}; + double *tests[] = {test1, test2, test3, test4, test5}; - int tests_num = 4; + int tests_num = sizeof(tests) / sizeof(tests[0]); for (int i = 0; i < tests_num; i++) { if (visualize) { - printf("------------\n"); + printf("-------------------\n"); printf("Test %d\n", i + 1); printf("a: %.12lf\n", tests[i][0]); printf("Precision: %.12lf\n\n", tests[i][1]); @@ -122,13 +132,14 @@ void test(int visualize) if (visualize) printf("\n"); } + + printf("\nAll tests passed\n"); } /** - * @brief asks if the user would like to visualize - * then calls the test() function - * - * + * @brief asks if the user would like to visualize then calls the test() function + * + * * @returns 0 if all tests passed */ int main(void)