This commit is contained in:
Akbar Rahman 2023-10-06 16:44:56 +01:00
parent 5af4c707eb
commit 7bb7fc04b1
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
5 changed files with 99 additions and 0 deletions

1
C14/Makefile Normal file
View File

@ -0,0 +1 @@
include ../Makefile

32
C14/ex1.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <math.h>
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;
}

29
C14/ex2.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <math.h>
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", &degrees);
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);
}

33
C14/ex3.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <math.h>
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", &degrees_start);
printf("End: ");
scanf("%d", &degrees_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);
}
}

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
CFLAGS=-lm
%.c:
$(CC) $(CFLAGS) -c $< -o $*