mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
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 <halfpacho@gmail.com> * Update conversions/celsius_to_fahrenheit.c Co-authored-by: David Leal <halfpacho@gmail.com> * chore: apply suggestions from code review Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
bb11a35227
commit
0f5f241a1d
74
conversions/celsius_to_fahrenheit.c
Normal file
74
conversions/celsius_to_fahrenheit.c
Normal file
@ -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 <assert.h> /// for assert
|
||||
#include <stdio.h> /// 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user