Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A complex number z may be expressed as z-x + jy in the Cartesian co-ordinate sys

ID: 675334 • Letter: A

Question

A complex number z may be expressed as z-x + jy in the Cartesian co-ordinate system or as z = re in the polar co-ordinate system, where x, y, r and are real quantities and j = sqrt (-I). Design a program in C using prototype functions and the "switch" command which will do one of two sets of computations depending upon the value of a runmode operator: for the value runmode = 1, the program will compute the values of the polar components r and from user-supplied values of x and y using the formulas r = sqrt(x^2 + y^2) and = aretan(y/x); and for. the value runmode = 2, the program will perform the opposite computation, i.e., find the values of the Cartesian components x = r cos and y = r sin from user-supplied values of rand . Use four prototype functions called cartesianToPolar1 cartesianToPolar2, polarToCartesian1. And polarToCartesian2. The prototype functions cartesianToPolar1 and cartesianToPoiar2 are used when one wants to convert user-supplied Cartesian components x and y of a complex number z into their equivalent polar components r and , respectively. In a like manner, the prototype functions polarToCartesian1 and polarToCartesian2 are used when converting user-specified polar components r and of z into equivalent Cartesian components x and y, respectively. All four prototype functions are both to be placed after the main function. The outputting of the computations performed by the prototype functions are all to be done from within the main function. The computation process is to be executed in an infinite loop so that, once a computation is done and displayed on the console, the process starts all over again.

Explanation / Answer

#include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTYPE FOR test_polar
void test_polar();

// FUNCTION PROTOTYPE FOR read_point
void read_point(double & radius, double & angle);


// FUNCTION PROTOTYPE FOR degrees2radians
double degrees2radians(double angle);

// FUNCTION PROTOTYPE FOR compute_coord
void compute_coord(double radius, double radians, double & x, double & y);

// FUNCTION PROTOTYPE FOR compute_distance
double compute_distance(double x1, double y1,double x2,double y2);


// FUNCTION PROTOTYPE FOR output_point
void output_point(double x, double y);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
double r1(0.0), a1(0.0); // Polar coord (r1, a1) of point 1
double r2(0.0), a2(0.0); // Polar coord (r2, a2) of point 2
double radians1(0.0); // Angle a1 in radians
double radians2(0.0); // Angle a2 in radians
double x1(0.0), y1(0.0); // Cartesian (x,y) coordinates of point 1
double x2(0.0), y2(0.0); // Cartesian (x,y) coordinates of point 2
double distance(0.0); // Distance between points

// Call test routine
test_polar();

// Read two points in polar coordinates
read_point(r1, a1);
read_point(r2, a2);

// Convert degrees to radians
radians1 = degrees2radians(a1);
radians2 = degrees2radians(a2);

// Compute Cartesian (x,y) coordinates
compute_coord(r1, radians1, x1, y1);
compute_coord(r2, radians2, x2, y2);

// Compute distance from point 1 to point 2
distance = compute_distance(x1, y1, x2, y2);

cout << "Distance from ";
output_point(x1, y1);
cout << " to ";
output_point(x2,y2);
cout << " = " << distance << endl;

return 0;
}

// DO NOT MODIFY THIS FUNCTION IN ANY WAY
// test_polar: Test functions in polar.cpp
void test_polar()
{
double radians(0.0); // angle in radians
double x(0.0), y(0.0); // Cartesian (x,y) coordinates
double distance(0.0); // distance between points

cout << "Testing:" << endl;

radians = degrees2radians(45);
cout << "45 degrees equals " << radians << " radians." << endl;

cout << "Polar coordinates (2,30) = Cartesian coordinates ";
output_point(1.73, 1.00);
cout << endl;

compute_coord(2, M_PI/3, x, y);
cout << "Polar coordinates (2,60) = Cartesian coordinates ";
output_point(x, y);
cout << endl;

distance = compute_distance(1.73, 1.00, 1.00, 1.73);
cout << "Distance from (1.73,1.00) to (1.00,1.73) = "
<< distance << endl;

cout << "End test." << endl;
cout << endl;
}

// DEFINE FUNCTION read_point here:

void read_point(double & radius, double & angle)
{
cout<<"Enter polar coordinates of point (radius, angle) : "<<endl;
cin>>radius>>angle;

}

// DEFINE FUNCTION degrees2radians here:

double degrees2radians(double angle)
{
double radians=angle * M_PI / 180;
return radians;
}

// DEFINE FUNCTION compute_coord here:
void compute_coord(double r, double radians, double & x, double & y)
{
x=r*cos(radians);
y=r*sin(radians);

}

// DEFINE FUNCTION distance here:

double compute_distance(double x1, double y1, double x2, double y2)

{
double distance=sqrt((x2-x1)^2+(y2-y1)^2);
return distance;
}


// DEFINE FUNCTION output_point here:

void output_point(double x, double y)
{
cout.precision(2);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote