Complete this program, by adding a mag( ) function , and add another function re
ID: 3683175 • Letter: C
Question
Complete this program, by adding a mag( ) function , and add another function rect2polar (and prototype) that returns the polar form of a rectangular coordinate pair. The data type of the inputs, x, y should be double, as should the data type of the outputs r and theta. Since you already have a function which gets the magnitude of a vector, your rect2polar(...) function can call it to get half the job done. The commented code (which you'll need to un-comment) shows how your function should be called. Name your program pass_by_ref.cpp . This is C++
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
// for part a, put your function prototype for mag here!
double mag(double x, double y);
// for part b, put your function prototype for rec2polar here!
double rect2polar(double x, double y, double& r, double& theta);
//............................................................
#define PI 3.14159265
int main(void)
{
double x = 0;
double y = 0;
double v=-6.0, w=8.0;
double m;
cout << "Please enter the X coordinate: ";
cin >> x;
cout << "Please enter the Y coordinate: ";
cin >> y;
m = mag(x,y); // you need to write this function!
cout << "The magnitude of ( "<< x << " , "<< y <<" ) is: " << m << endl;
m = mag(v,w); // now get the magnitude of vector (v,w)
cout << "The magnitude of ( "<< v << " , "<< w <<" ) is: " << m << endl;
double r, theta;
rect2polar(x,y,r,theta);
cout << "the polar form of ( "<< x << " , "<< y <<" ) is: ( " << r << " , " << theta << " )"<<endl;
rect2polar(v,w,r,theta);
cout << "the polar form of ( "<< v << " , "<< w <<" ) is: ( " << r << " , " << theta << " )"<<endl;
return 0;
}
double mag(double x, double y)
{
return sqrt(x*x + y*y);
}
double rect2polar(double x, double y, double& r, double& theta)
{
theta = atan(x/y) * 180 / PI; //PI is a constant, atan convert tan inverse and return in radians
r = mag(x, y);
//theta is now in degrees
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.