This repository has been archived on 2023-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
VSMechatronics/C14/ex1.c

33 lines
606 B
C
Raw Normal View History

2023-10-06 15:44:56 +00:00
#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;
}