The equations x = r cos (), y = rsin(),r 2 = x 2 + y 2 ,and tan() = y/x define t
ID: 3613006 • Letter: T
Question
The equations x = r cos (), y = rsin(),r2 = x2 + y2 ,and tan() = y/x define the relationship betweenCartesian coordinates (x,y) and polar coordinates (r,)
Assignment:
Write a program that prompts the user whether to convert fromCartesian coordinates to polar coordinates or the other way around;and then prompts the user for the name of an input file. Theinput file will contain a number of coordinates. The programwill read in and convert each coordinate in the inputfile. The converted coordinates are printed in an output filecalled “convert.out”. Use astructures to represent the points.
In order to do this assignment, you will need the mathematicsfunctions sin, cos, andtan. These functions are in the mathematicslibrary. You need to include this library in your program(i.e. the statement: #include <math.h> must beadded to your program). The sin,cos, and tan functions expects adouble value angle as anargument, and returns the double value as aresult. The value for angle must be inradians. For example, sin(/2) =1. Note /2 = 90°.
Suggested help for completing the assignment: Firstwrite a program that prompts the user to select the type ofconversion, prompts the user for each coordinate, and then displaysthe converted coordinate on the monitor screen. When you aresatisfied with the behavior of this program, modify the program toread in coordinates from an input file and put the result in theoutput file convert.out.
Explanation / Answer
please rate - thanks #include #include #include struct coordinates { double x; double y; double rho; double theta; } point; int main() {FILE *outfile; FILE *infile; char filename[30]; int choice; outfile = fopen("convert.out","w"); printf("what do you want to do "); printf("1. convert cartesian coordinates to polar "); printf("2. convert polar coordinates to cartesian "); scanf("%d",&choice); printf("what is the name of the input file? "); scanf("%s",&filename); infile=fopen(filename,"r"); if(infile==NULL) {printf("error opening file "); return 1; } fprintf(outfile," x y rho theta "); if(choice==1) {while(fscanf(infile,"%lf",&point.x)!=EOF) {fscanf(infile,"%lf",&point.y); point.rho=sqrt(pow(point.x,2)+pow(point.y,2)); point.theta=atan(point.x/point.y); fprintf(outfile,"%lf %lf %lf %lf ",point.x,point.y,point.rho,point.theta); } } else {while(fscanf(infile,"%lf%lf",&point.rho,&point.theta)!=EOF) {//fscanf(infile,"%d",&point.theta); point.x=point.rho*cos(point.theta); point.y=point.rho*sin(point.theta); fprintf(outfile,"%lf %lf %lf %lf ",point.x,point.y,point.rho,point.theta); } } fclose(infile); fclose(outfile); printf("program has completed "); getch(); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.