33 lines
606 B
C
33 lines
606 B
C
|
#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;
|
||
|
}
|