CMPT 102   Lab 5

This lab is designed primarily to give you practice using functions with a selections of arguments passed both by value and by reference.

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:

 

int transformToCartesian(double r, double theta, double *xp, double *yp)

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.

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)

 

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.