ECE231 - Spring 2009 - Programming Assignment 2
This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.
So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.
1 2 3 4 5 6 7 8 |
#include <iostream> #include <cmath> #define PI 3.14159 or const double PI = 3.14159; |
Defining the Coordinate Class
For the coordinate class we need 2 private variables x and y, 3 constructors, and 11 member functions.
The way you do the constructors and member functions can be done 2 different ways. You can either do them inside the class, or you can define them inside the class, like you do prototypes, and then have the actual functions at the bottom of your code. I like to define them and then put the functions at the bottom, making the code a little bit easier to read so that is the way that I'll show you, but if you want to do it the other way then just do it how you normally would with a regular function.
So here is what your class should look like (keep in mind that this all goes at the top before the main function):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class coordinate { private: double x; // x value of the coordinate point double y; // y value of the coordinate point public: coordinate(); // 1st constructor requiring that no arguments are passed coordinate(double ); // 2nd constructor requiring that only 1 argument is passed coordinate(double , double ); // 3rd constructor requiring that 2 arguments are passed void set(double , double ); // sets both values and requires 2 arguments to be passed void setx(double ); // sets x value and requires 1 argument to be passed void sety(double ); // sets y value and requires 1 argument to be passed void read(); // allows user to input both x and y values void print(); // prints out the coordinate point in "(x, y)" form double distancezero(); // calculates distance of point from zero and returns the value double distancetwo(coordinate ); // calculates distance between 2 points (current instance and coordinate value passed) and returns the value double ranglezero(); // calculates the angle of the coordinate in radians and returns the value double danglezero(); // calculates the angle of the coordinate in degrees and returns the value int quadrant(); // find what quadrant the coordinate is in and returns the value void midpoint(coordinate ); // calculates the midpoint between 2 points (current instance and coordinate value passed) and prints the value as a coordinate }; |
Writing the Actual Functions for the Coordinate Class
Since we are not actually writing the functions in the same place we are doing the class we will write them at the bottom after the main function. However, to do this we will need a little more than what we would with regular functions. For member functions we have to define what class the function is actually a member of. To do this we have to do coordinate::[function]().
For example, here are the 3 constructors for the coordinate class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// takes no arguments and sets x and y to 0 coordinate::coordinate() { x = y = 0; } // takes 1 argument and sets x and y to that value coordinate::coordinate(double a) { x = y = a; } // takes 2 arguments and sets the corresponding x and y to those values coordinate::coordinate(double a, double b) { x = a; y = b; } |
Next, we have the member functions. The first 3 are the set functions. These allow the user to set the values of either the x or the y or both together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// take 2 arguments and sets the corresponding x and y to those values void coordinate::set(double a, double b) { x = a; y = b; } // takes 1 argument and sets the x to that value void coordinate::setx(double a) { x = a; } // takes 1 argument and sets the y to that value void coordinate::sety(double b) { y = b; } |
Next, is the read and print functions. All we want the read to do is to do a cin of the 2 values (x and y). The print is just the opposite. All it does is do a cout of the x and y values in the (x, y) format.
1 2 3 4 5 6 7 8 9 10 11 12 |
// takes no arguments and reads in the x and y values from the screen void coordinate::read() { cin >> x >> y; } // takes no arguments and prints the x and y values in (x, y) format void coordinate::print() { cout << "(" << x << ", " << y << ")"; } |
Next, we have the distance functions. distanczero calculates the distance of the current instance coordinate from 0. distancetwo calculates the distance between 2 points (the current instance and the coordinate passed in).
1 2 3 4 5 6 7 8 9 10 11 12 |
// takes no arguments and returns the distance of the current instance from zero double coordinate::distancezero() { return sqrt(x * x + y * y); } // take 1 argument and calculates the distance between them double coordinate::distancetwo(coordinate pt) { return sqrt(pow(x - pt.x, 2.0) + pow(y - pt.y, 2.0)); } |
Next, we have the angle functions. ranglezero uses the function in the math library atan()(arc tangent) to find the angle in radians from the positive x axis. danglezero does the same thing but converts the value from radians to degrees. To convert from radians to degrees you multiply the value by 180 / pi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// takes no arguments and returns the angle of the coordinate from the positive x axis in radians double coordinate::ranglezero() { double angle; // angle of coordinate from positive x axis if(x == 0) // if x == 0 then you will get a domain error so compute angle manually { if(y > 0) return PI / 2; if(y < 0) return -PI / 2; if(y == 0) return 0; } angle = atan(y / x); if(x < 0 and y >= 0) return PI - abs(angle); if(x < 0 and y < 0) return -(PI - abs(angle)); return angle; } // takes no arguments and returns the angle of the coordinate from the positive x axis in degrees double coordinate::danglezero() { double angle; // angle of coordinate from positive x axis if(x == 0) // if x == 0 then you will get a domain error so compute angle manually { if(y > 0) return 90; if(y < 0) return -90; if(y == 0) return 0; } angle = atan(y / x) * 180 / PI; if(x < 0 and y >= 0) return 180 - abs(angle); if(x < 0 and y < 0) return -(180 - abs(angle)); return angle; } |
Next, we have the quadrant function. This looks at whether the x and y values are positive or negative to determine which quadrant the coordinate is in. It will return the number of the quadrant as an integer 1-4 and 0 if the coordinate is (0, 0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int coordinate::quadrant() { if(x > 0 && y >= 0) return 1; // return quadrant 1 if(x <= 0 && y > 0) return 2; // return quadrant 2 if(x < 0 && y <= 0) return 3; // return quadrant 3 if(x >= 0 && y < 0) return 4; // return quadrant 4 return 0; // return 0 if point is (0, 0) } |
Last, we have the midpoint function. you will need to pass this function a coordinate instance and it will calculate the midpoint between the current instance and the passed instance and print the coordinate instance.
1 2 3 4 5 6 7 8 9 |
void coordinate::midpoint(coordinate pt) { coordinate np; // new coordinate point np.x = (x + pt.x) / 2.0; np.y = (y + pt.y) / 2.0; np.print(); } |
Now all you have to do is test each constructor and member function to make sure that everything is working properly.
Sorry, comments for this entry are closed at this time.