Help coding in C++, please explain and show an example of the program working.
Posted: Mon May 09, 2022 5:53 am
Help coding in C++, please explain and show an
example of the program working.
[7 Points] Programs and header files on the next page illustrate a program in C++ that asks the reader to enter an order pair in rectangular coordinate system and convert it to polar coordinate. Modify this C++ program so that the program asks the user to enter an order pair in polar coordinate (use degree for angle) and convert it to rectangular coordinate. Note that your program must define and use class functions.
This program demonstrates the use of the xy_coordinate class and its functions. #include <iostream> #include <cmath> #include "xy_coordinate.h" using namespace std; int main(void) { // Declare and initialize variables. xy_coordinate pt1; // Read input point. cout << "Enter x and y coordinates:" << endl; pti.input(); // Print coordinate in xy form and polar form. cout.setf(ios::fixed); cout.precision (2); cout << "Coordinate in xy form:" << endl; pti.print() cout << "Coordinate in polar form:" << endl; cout << "magnitude: " « pt1.radius() << endl; cout << "phase (in degrees): << pti.angle () *180/3.141593 << endl; // Exit program. return 0; } //---- These statements define a class for xy-coordinates. // This declaration is stored in xy_coordinate.h. #include <iostream> #include <cmath> class xy_coordinate { // Declare function prototypes for public members. public: void input() void print () double radius (); double angle();
private: // Declare private data members. double x, y; }; //---- These statements define implementation of an // xy_coordinate class. They are stored in xy_coordinate.h. // This function reads the xy coordinates from the keyboard. void xy_coordinate::input() { cin >> x >> Y; } // This function prints the xy coordinates to the screen. void xy_coordinate: :print() { cout << "(" «x « "," « y « ")" «< "\n"; } // This function computes the radius. double xy_coordinate: :radius () { return sqrt(x*x + y*y); } This function computes the angle in radians. double xy_coordinate: : angle() { // Compute angle of polar form. double z, pi=3.141593; if (x >= 0) z = atan (y/x); if (x<0 && y>0) z = atan (y/x) + pi; if (x<0 && y<=0) z = atan (y/x) - pi; if (x==0 && y==0) z = 0; return z; } 17
example of the program working.
[7 Points] Programs and header files on the next page illustrate a program in C++ that asks the reader to enter an order pair in rectangular coordinate system and convert it to polar coordinate. Modify this C++ program so that the program asks the user to enter an order pair in polar coordinate (use degree for angle) and convert it to rectangular coordinate. Note that your program must define and use class functions.
This program demonstrates the use of the xy_coordinate class and its functions. #include <iostream> #include <cmath> #include "xy_coordinate.h" using namespace std; int main(void) { // Declare and initialize variables. xy_coordinate pt1; // Read input point. cout << "Enter x and y coordinates:" << endl; pti.input(); // Print coordinate in xy form and polar form. cout.setf(ios::fixed); cout.precision (2); cout << "Coordinate in xy form:" << endl; pti.print() cout << "Coordinate in polar form:" << endl; cout << "magnitude: " « pt1.radius() << endl; cout << "phase (in degrees): << pti.angle () *180/3.141593 << endl; // Exit program. return 0; } //---- These statements define a class for xy-coordinates. // This declaration is stored in xy_coordinate.h. #include <iostream> #include <cmath> class xy_coordinate { // Declare function prototypes for public members. public: void input() void print () double radius (); double angle();
private: // Declare private data members. double x, y; }; //---- These statements define implementation of an // xy_coordinate class. They are stored in xy_coordinate.h. // This function reads the xy coordinates from the keyboard. void xy_coordinate::input() { cin >> x >> Y; } // This function prints the xy coordinates to the screen. void xy_coordinate: :print() { cout << "(" «x « "," « y « ")" «< "\n"; } // This function computes the radius. double xy_coordinate: :radius () { return sqrt(x*x + y*y); } This function computes the angle in radians. double xy_coordinate: : angle() { // Compute angle of polar form. double z, pi=3.141593; if (x >= 0) z = atan (y/x); if (x<0 && y>0) z = atan (y/x) + pi; if (x<0 && y<=0) z = atan (y/x) - pi; if (x==0 && y==0) z = 0; return z; } 17