1. Consider the lab problem below and develop an algorithm to solve it based on the information provided.
2. Write a summary of your algorithm development, showing each step in the process and making sure to include all of the items listed below. You algorithm development should be typed in a text or word processor file. You may add hand drawn diagrams or hand written equations to your printed solution. Your discussion of algorithm development should include each of the following:
a) Statement of the problem
b) A list of inputs, and a list of outputs along with a list of any mathematical relations demonstrating the relations between the inputs and the outputs
c) An detailed algorithm
d) A test plan for testing the algorithm or the implemented program
3. Implement the algorithm above. Call the file containing the implementation of your algorithm (your program) coordTransform.c.
PRACTICE PROBLEM: Write a C program to convert the coordinates of a point between Cartesian and Polar coordinates.
The following standard math library functions will be useful to you as you implement your algorithm
double sqrt(double x); This function takes the square root of a positive number. You cannot take the square root of a negative number, so you must structure your program to take a square root of a number only after you have checked that it is a positive number.
double fabs(double
x); This
function calculates the absolute value of its argument.
double cos(double angleInRadians); This function calculates the cosine of angleInRadians
double sin(double angleInRadians); This function calculates the sine of angleInRadians
double atan2( double y, double x); This function calculates the arctangent (inverse tangent) of an angle between a line and the x axis. The line connects the point (0, 0) and the point (x, y);
Your implemented program should comply with the following requirements:
- You should write and use two functions.
int transformToCartesian(double r, double theta, double *xp, double *yp)
- The transformToCartesian ( ) function should take the polar coordinates, (r, Θ), of a point and convert them to the corresponding Cartesian coordinates..(x, y). Remember that
x = r cos(Θ) and that y = r sin(Θ). Note that r > 0. The function returns 0 if it completes successfully, 1 if there is an error of the answer cannot be determined.
int transformToPolar(double x, double y,
double *rp, double *thetap)
The transformToPolar( ) function should take the Cartesian coordinates, (x, y), of a point and convert them to the corresponding polar coordinates (r, Θ).. Remember that r = x2+y2 and that Θ=arctan(y/x). Note that for the point (0, 0) the value of Θ is undefined. The function returns 0 if it completes successfully, 1 if there is an error of the answer cannot be determined.
- You should write a main program to use these functions. Your main program should
1. Ask the user if they wish to convert from Cartesian to polar
a) If the user reply yes (Y or y) then
i. prompt the user to enter the Cartesian Coordinates and read those coordinates from the standard input (keyboard)
ii. Calculate r and Θ the polar coordinates of the point by calling transformToPolar
iii. Output the coordinates in both coordinate systems
2. Ask the user if they wish to convert from polar to Cartesian
a) If the user reply yes (Y or y) then
i. prompt the user to enter the polar Coordinates and read those coordinates from the standard input (keyboard)
ii. Calculate x and y the polar coordinates of the point by calling transformToCartesian
iii. Output the coordinates in both coordinate systems
3. The output of this part of the program should look like
This program converts coordinates between
polar and Cartesian:
Convert from Cartesian to polar Y/N y
the answer is y
Enter the x coordinate; 3
Enter the y coordinate: 4
The polar coordinates of the point (x=3.000000,
y=4.000000) is
(r=5.00, theta=0.93)
Convert from polar to Cartesian Y/N y
Enter the r coordinate; 5
Enter the theta coordinate: 0.927295
The Cartesian coordinates of the point (r=5.000000,
theta=0.927295) is
(X=3.00, Y=4.00)
- Your program should print results with two decimal places.
- Here are some things to keep in mind:
- You'll need to #include <math> to get the standard math library which includes sqrt() function and the fabs () function
- You can't take the square root of a negative number. You must make sure the number is non-negative before you try to take its root.
- Your code should be easy to read and understand.
- Your code should be clearly commented
- You don't have to check the user's input to assure it is the correct type (int, float …). For the moment, you can assume it is. This means that marks will not be deducted if your program fails when character input (not numbers) are entered by the user as values of the coefficients.
- You should check that the values input to your program are acceptable values to the program
Be sure to test your program extensively using the test plan you developed earlier
If you want more practice try some of the end of chapter problems in your textbook.