### POLAR COORDINATES

Write a program that prompts the user for the x and y coordinates of a point on the x-y plane, and then displays the position of that point in polar coordinates.

The program must execute via a function ‘polar’ that is called from the main function. This function accepts x & y coordinates as input value parameters, and then returns, through reference parameters, the polar coordinate position of the point (r and theta).

Could in theory be written entirely in the main function; however, to meet the problem statement the code must include a user defined function that returns polar coordinates to the main function via reference parameters.

Some helpful programming hints:

(i) Reference parameters are declared using an ampersand after the declaration type: for example – ‘int& number’ declares a reference parameter ‘number’ of type ‘integer’.

(ii) You’ll need the cmath library for arctan and sqrt functions. The C++ syntax for the arctan function is “atan(x)”.

(iii) The C++ arctan function returns an angle in radians. For output to the screen convert the angle theta from radians to degrees.

(iv) Program should work for x, y values in all four quadrants of the x-y plane.
And what's the problem you're having with your code?
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include using namespace std; double Radius(double x, double y); double Theta (double x, double y); int main() { double x, y; y = 0.0; x = 0.0; cin >> x, y; cout << Radius (x,y) << endl; cout << Theta (x,y) << endl; return 0; } double Radius(double x, double y) { double rad; rad = sqrt((pow(x,2))+(pow(y,2))); return rad; } double Theta(double x, double y) { double Thta; Thta = atan(y/x); return Thta; } ``````
 sqrt((pow(x,2))+(pow(y,2)));

The function to do this is called hypot() (first appeared in C99 and C++11)
http://en.cppreference.com/w/cpp/numeric/math/hypot

 atan(y/x);

The function to do this is called atan2() (first appeared in the beginning of time)
http://en.cppreference.com/w/cpp/numeric/math/atan2
http://cplusplus.com/reference/cmath/atan2/

Also, the C++ complex number library can do polar/cartesian conversions rather simply.

(those are just what jumps at me.. but if your question is how to "include a user defined function that returns polar coordinates to the main function via reference parameters.", I believe you're asked to write a function `void polar(double x, double y, double& r, double& theta)`)
Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````using namespace std; void polar(double x, double y, double& r, double& theta); int main() { double xMain, yMain, radiusMain, thetaMain, rMain; cin >> xMain; cout << endl; cin >> yMain; cout << endl; cout << endl; cout << endl; polar(xMain, yMain, rMain, thetaMain); cout << rMain; cout << endl; cout << thetaMain; cout << endl; cout << endl; cout << endl; return 0; } void polar(double x, double y, double& r, double& theta) { r = sqrt((pow(x,2))+(pow(y,2))); theta = atan(y/x); theta = (theta*180)/3.141592654; }``````
Topic archived. No new replies allowed.