From 0f5f241a1d0a586cc7eb634c05f9320d793d1ad2 Mon Sep 17 00:00:00 2001 From: Focus <65309793+Focusucof@users.noreply.github.com> Date: Fri, 2 Dec 2022 00:01:16 -0500 Subject: [PATCH] feat: add Celsius to Fahrenheit conversion (#1129) * feat: added celcius_to_fahrenheit.c * docs: added comment to 1st test * updating DIRECTORY.md * fix: changed spelling of 'celcius' to 'celsius' * updating DIRECTORY.md * Update conversions/celsius_to_fahrenheit.c Co-authored-by: David Leal * Update conversions/celsius_to_fahrenheit.c Co-authored-by: David Leal * chore: apply suggestions from code review Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal --- conversions/celsius_to_fahrenheit.c | 74 +++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 conversions/celsius_to_fahrenheit.c diff --git a/conversions/celsius_to_fahrenheit.c b/conversions/celsius_to_fahrenheit.c new file mode 100644 index 00000000..3c1bda9e --- /dev/null +++ b/conversions/celsius_to_fahrenheit.c @@ -0,0 +1,74 @@ +/** + * @file + * @brief Conversion of temperature in degrees from [Celsius](https://en.wikipedia.org/wiki/Celsius) + * to [Fahrenheit](https://en.wikipedia.org/wiki/Fahrenheit). + * + * @author [Focusucof](https://github.com/Focusucof) + */ + +#include /// for assert +#include /// for IO operations + +/** + * @brief Convert celsius to Fahrenheit + * @param celsius Temperature in degrees celsius double + * @returns Double of temperature in degrees Fahrenheit + */ + double celcius_to_fahrenheit(double celsius) { + return (celsius * 9.0 / 5.0) + 32.0; + } + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // 1st test + double input = 0.0; + double expected = 32.0; + + double output = celcius_to_fahrenheit(input); + + // 1st test + printf("TEST 1\n"); + printf("Input: %f\n", input); + printf("Expected Output: %f\n", expected); + printf("Output: %f\n", output); + assert(output == expected); + printf("== TEST PASSED ==\n\n"); + + // 2nd test + input = 100.0; + expected = 212.0; + + output = celcius_to_fahrenheit(input); + + printf("TEST 2\n"); + printf("Input: %f\n", input); + printf("Expected Output: %f\n", expected); + printf("Output: %f\n", output); + assert(output == expected); + printf("== TEST PASSED ==\n\n"); + + // 3rd test + input = 22.5; + expected = 72.5; + + output = celcius_to_fahrenheit(input); + + printf("TEST 3\n"); + printf("Input: %f\n", input); + printf("Expected Output: %f\n", expected); + printf("Output: %f\n", output); + assert(output == expected); + printf("== TEST PASSED ==\n\n"); +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}