Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that gives user a choice to enter a point either in 2D space Car

ID: 3814685 • Letter: W

Question

Write a program that gives user a choice to enter a point either in 2D space Cartesian coordinate (x and y coordinates) or Polar coordinates (r, theta), theta in degrees. Write a function toPolar() that takes Cartesian coordinates as input and converts that to Polar coordinates and displays the radial coordinate and angular coordinate in degrees. Then write another function toCartesian() that takes Polar coordinates (r, theta), theta in degrees, as input and converts that to Cartesian coordinates and displays that. When a user chooses to enter Cartesian coordinate, (s)he should get the output as Polar coordinate and vice-versa. You can look up the Polar Coordinate systems here - Use the math library functions for this program. You can use atan2(y, x) for Arc tangent calculation. So, you will have to convert radians to degrees and vice-versa as required. Enter P for Polar coordinate or C for Cartesian Coordinate: C Enter Cartesian coordinate (x, y) with space: -10 10 The Polar Coordinate for (x = -10.000000, y = 10.000000) is r=14.142136, theta=135.000000 degrees

Explanation / Answer

#include<stdio.h>
#include<math.h>
#define PI 3.14
void toPolar(double x,double y)
{
double theta,r;
double val = 180.0 / PI;
r=sqrt(x*x+y*y); // square root function in math.h
theta= atan2 (y,x) * val; // Tan inverse function in math.h
printf("The value in polar coordinate is(%lf,%lf degrees)",r,theta);
}
void toCartiesian(double r,double angle)
{
double x,y;
double val = PI / 180.0;
x = r* cos( angle*val );
   y=r*sin(angle*val);
printf("The value in cartesian coordinate is(%f,%lf) ",x,y);
}
int main()
{
char choice;
double x,y,r,angle;
printf("Enter P for Polar coordinate and C for cartesian coordinate ");
scanf("%c",&choice);
if(choice=='c' ||choice=='C' ){
printf("Enter the Cartesian coordinates (x,y) with space : ");
scanf("%lf,%lf",&x,&y);
toPolar(x,y);
}else if(choice=='p' ||choice=='P' ){
printf("Enter the Polar coordinates (r,theta in degrees) with space : ");
scanf("%lf,%lf",&r,&angle);
toCartiesian(r,angle);
}else{
printf("Iavlid Choice ");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote