From 7bb7fc04b1bf8ecc74a5fcd8b264159fc0a996a4 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Fri, 6 Oct 2023 16:44:56 +0100 Subject: [PATCH] c14 --- C14/Makefile | 1 + C14/ex1.c | 32 ++++++++++++++++++++++++++++++++ C14/ex2.c | 29 +++++++++++++++++++++++++++++ C14/ex3.c | 33 +++++++++++++++++++++++++++++++++ Makefile | 4 ++++ 5 files changed, 99 insertions(+) create mode 100644 C14/Makefile create mode 100644 C14/ex1.c create mode 100644 C14/ex2.c create mode 100644 C14/ex3.c create mode 100644 Makefile diff --git a/C14/Makefile b/C14/Makefile new file mode 100644 index 0000000..bb69e69 --- /dev/null +++ b/C14/Makefile @@ -0,0 +1 @@ +include ../Makefile diff --git a/C14/ex1.c b/C14/ex1.c new file mode 100644 index 0000000..eebd18d --- /dev/null +++ b/C14/ex1.c @@ -0,0 +1,32 @@ +#include +#include + +void cartesian_to_polar(float x, float y, float *r, float *theta) { + *r = sqrtf(x*x + y*y); + *theta = y/x; +} + +int main() { + int rc; + float x, y, r, theta; + + printf("x: "); + rc = scanf("%f", &x); + // check that scanf was successful by checking if it returned 1 (1 successfully scanned item) + if (rc != 1) { + printf("Please enter a real number\n"); + return 1; + } + + printf("y: "); + rc = scanf("%f", &y); + if (rc != 1) { + printf("Please enter a real number\n"); + return 1; + } + + cartesian_to_polar(x, y, &r, &theta); + printf("r=%f theta=%f\n", r, theta); + + return 0; +} diff --git a/C14/ex2.c b/C14/ex2.c new file mode 100644 index 0000000..28d0a70 --- /dev/null +++ b/C14/ex2.c @@ -0,0 +1,29 @@ +#include +#include + + +float degrees_to_radians(float degrees) { + return M_PI * degrees / 180.0; +} + +void process(float degrees, float* radians, float* sin_val, float* cos_val, float* tan_val) { + *radians = degrees_to_radians(degrees); + *sin_val = sin(*radians); + *cos_val = cos(*radians); + *tan_val = tan(*radians); +} + + +int main() { + float degrees, radians, sin_val, cos_val, tan_val; + + printf("Degrees: "); + scanf("%f", °rees); + + process(degrees, &radians, &sin_val, &cos_val, &tan_val); + + printf("Radians: %f\n", radians); + printf("Sine: %f\n", sin_val); + printf("Cosine: %f\n", cos_val); + printf("Tangent: %f\n", tan_val); +} diff --git a/C14/ex3.c b/C14/ex3.c new file mode 100644 index 0000000..de55733 --- /dev/null +++ b/C14/ex3.c @@ -0,0 +1,33 @@ +#include +#include + + +float degrees_to_radians(float degrees) { + return M_PI * degrees / 180.0; +} + +void process(float degrees, float* radians, float* sin_val, float* cos_val, float* tan_val) { + *radians = degrees_to_radians(degrees); + *sin_val = sin(*radians); + *cos_val = cos(*radians); + *tan_val = tan(*radians); +} + + +int main() { + int degrees_start, degrees_end; + float radians, sin_val, cos_val, tan_val; + + printf("Start: "); + scanf("%d", °rees_start); + printf("End: "); + scanf("%d", °rees_end); + printf("Degs\tRad\tsin\tcos\ttan\n"); + + for (int i = degrees_start; i <= degrees_end; i++) { + process((float)i, &radians, &sin_val, &cos_val, &tan_val); + printf("%d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n", i, radians, sin_val, cos_val, tan_val); + } + + +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..01e2cdf --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-lm + +%.c: + $(CC) $(CFLAGS) -c $< -o $*