{"id":51930,"date":"2009-02-23T04:13:00","date_gmt":"2009-02-23T04:13:00","guid":{"rendered":"\/2009\/03\/01\/ece231-spring-2009-programming-assignment-2"},"modified":"2009-08-30T00:17:13","modified_gmt":"2009-08-30T00:17:13","slug":"ece231-spring-2009-programming-assignment-2","status":"publish","type":"post","link":"http:\/\/pullmonkey.com\/2009\/02\/23\/ece231-spring-2009-programming-assignment-2\/","title":{"rendered":"ECE231 – Spring 2009 – Programming Assignment 2"},"content":{"rendered":"

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<\/a>.<\/p>\n

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<\/b> and the cmath<\/b> libraries. We will also need the value of pi<\/b>.<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>#include<\/span> <iostream><\/span>\n<\/tt>#include<\/span> <cmath><\/span>\n<\/tt>\n<\/tt>#define<\/span> PI 3<\/span>.14159<\/span>\n<\/tt>or\n<\/tt>const<\/span> double<\/span> PI = 3<\/span>.14159<\/span>;\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Defining the Coordinate Class<\/h2>\n

For the coordinate<\/b> class we need 2 private variables x<\/i><\/b> and y<\/i><\/b>, 3 constructors<\/b>, and 11 member functions<\/b>.<\/p>\n

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.<\/p>\n

So here is what your class should look like (keep in mind that this all goes at the top before the main function<\/b>):<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>class coordinate\n<\/tt>{\n<\/tt>  private:<\/span>\n<\/tt>    double<\/span> x;  \/\/ x value of the coordinate point<\/span>\n<\/tt>    double<\/span> y;  \/\/ y value of the coordinate point<\/span>\n<\/tt>  public:<\/span>\n<\/tt>    coordinate(); \/\/ 1st constructor requiring that no arguments are passed<\/span>\n<\/tt>    coordinate(double<\/span> ); \/\/ 2nd constructor requiring that only 1 argument is passed<\/span>\n<\/tt>    coordinate(double<\/span> , double<\/span> ); \/\/ 3rd constructor requiring that 2 arguments are passed<\/span>\n<\/tt>\n<\/tt>    void<\/span> set(double<\/span> , double<\/span> ); \/\/ sets both values and requires 2 arguments to be passed<\/span>\n<\/tt>    void<\/span> setx(double<\/span> ); \/\/ sets x value and requires 1 argument to be passed<\/span>\n<\/tt>    void<\/span> sety(double<\/span> ); \/\/ sets y value and requires 1 argument to be passed<\/span>\n<\/tt>    void<\/span> read(); \/\/ allows user to input both x and y values <\/span>\n<\/tt>    void<\/span> print(); \/\/ prints out the coordinate point in "(x, y)" form<\/span>\n<\/tt>    double<\/span> distancezero(); \/\/ calculates distance of point from zero and returns the value<\/span>\n<\/tt>    double<\/span> distancetwo(coordinate ); \/\/ calculates distance between 2 points (current instance and coordinate value passed) and returns the value<\/span>\n<\/tt>    double<\/span> ranglezero(); \/\/ calculates the angle of the coordinate in radians and returns the value<\/span>\n<\/tt>    double<\/span> danglezero(); \/\/ calculates the angle of the coordinate in degrees and returns the value<\/span>\n<\/tt>    int<\/span> quadrant(); \/\/ find what quadrant the coordinate is in and returns the value<\/span>\n<\/tt>    void<\/span> midpoint(coordinate ); \/\/ calculates the midpoint between 2 points (current instance and coordinate value passed) and prints the value as a coordinate<\/span>\n<\/tt>};\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Writing the Actual Functions for the Coordinate Class<\/h2>\n

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<\/b>. However, to do this we will need a little more than what we would with regular functions. For member functions<\/b> we have to define what class the function is actually a member of. To do this we have to do coordinate::[function]()<\/b>.<\/p>\n

For example, here are the 3 constructors for the coordinate<\/i><\/b> class:<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>\/\/ takes no arguments and sets x and y to 0<\/span>\n<\/tt>coordinate::coordinate()\n<\/tt>{\n<\/tt>  x = y = 0<\/span>;\n<\/tt>} \n<\/tt>\/\/ takes 1 argument and sets x and y to that value<\/span>\n<\/tt>coordinate::coordinate(double<\/span> a)\n<\/tt>{\n<\/tt>  x = y = a;\n<\/tt>} \n<\/tt>\/\/ takes 2 arguments and sets the corresponding x and y to those values<\/span>\n<\/tt>coordinate::coordinate(double<\/span> a, double<\/span> b)\n<\/tt>{\n<\/tt>  x = a;\n<\/tt>  y = b;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Next, we have the member functions<\/b>. The first 3 are the set functions. These allow the user to set the values of either the x<\/i<\/b> or the y<\/i><\/b> or both together.<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>\/\/ take 2 arguments and sets the corresponding x and y to those values<\/span>\n<\/tt>void<\/span> coordinate::set(double<\/span> a, double<\/span> b)\n<\/tt>{\n<\/tt>  x = a;\n<\/tt>  y = b;\n<\/tt>}\n<\/tt>\/\/ takes 1 argument and sets the x to that value<\/span>\n<\/tt>void<\/span> coordinate::setx(double<\/span> a)\n<\/tt>{\n<\/tt>  x = a;\n<\/tt>}\n<\/tt>\/\/ takes 1 argument and sets the y to that value<\/span>\n<\/tt>void<\/span> coordinate::sety(double<\/span> b)\n<\/tt>{\n<\/tt>  y = b;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Next, is the read<\/b> and print<\/b> functions. All we want the read to do is to do a cin<\/b> of the 2 values (x<\/i><\/b> and y<\/i><\/b>). The print<\/b> is just the opposite. All it does is do a cout<\/b> of the x<\/i><\/b> and y<\/i><\/b> values in the (x<\/i>, y<\/i>)<\/b> format.<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>\/\/ takes no arguments and reads in the x and y values from the screen<\/span>\n<\/tt>void<\/span> coordinate::read()\n<\/tt>{\n<\/tt>  cin >> x >> y;\n<\/tt>}\n<\/tt>\/\/ takes no arguments and prints the x and y values in (x, y) format<\/span>\n<\/tt>void<\/span> coordinate::print()\n<\/tt>{\n<\/tt>  cout << "<\/span>(<\/span>"<\/span><\/span> << x << "<\/span>, <\/span>"<\/span><\/span> << y << "<\/span>)<\/span>"<\/span><\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Next, we have the distance functions. distanczero<\/b> calculates the distance of the current instance<\/b> coordinate from 0. distancetwo<\/b> calculates the distance between 2 points (the current instance<\/b> and the coordinate passed in).<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>\/\/ takes no arguments and returns the distance of the current instance from zero<\/span>\n<\/tt>double<\/span> coordinate::distancezero()\n<\/tt>{\n<\/tt>  return<\/span> sqrt(x * x + y * y);\n<\/tt>}\n<\/tt>\/\/ take 1 argument and calculates the distance between them<\/span>\n<\/tt>double<\/span> coordinate::distancetwo(coordinate pt)\n<\/tt>{\n<\/tt>  return<\/span> sqrt(pow(x - pt.x, 2<\/span>.0<\/span>) + pow(y - pt.y, 2<\/span>.0<\/span>));\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Next, we have the angle functions. ranglezero<\/b> uses the function in the math library atan()<\/b>(arc tangent) to find the angle in radians from the positive x axis. danglezero<\/b> 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<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt>25<\/strong>\n<\/tt>26\n<\/tt>27\n<\/tt>28\n<\/tt>29\n<\/tt>30<\/strong>\n<\/tt>31\n<\/tt>32\n<\/tt>33\n<\/tt>34\n<\/tt>35<\/strong>\n<\/tt>36\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>\/\/ takes no arguments and returns the angle of the coordinate from the positive x axis in radians<\/span>\n<\/tt>double<\/span> coordinate::ranglezero()\n<\/tt>{\n<\/tt>  double<\/span> angle; \/\/ angle of coordinate from positive x axis<\/span>\n<\/tt>  if<\/span>(x == 0<\/span>) \/\/ if x == 0 then you will get a domain error so compute angle manually<\/span>\n<\/tt>  {\n<\/tt>    if<\/span>(y > 0<\/span>) return<\/span> PI \/ 2<\/span>;\n<\/tt>    if<\/span>(y < 0<\/span>) return<\/span> -PI \/ 2<\/span>;\n<\/tt>    if<\/span>(y == 0<\/span>) return<\/span> 0<\/span>;\n<\/tt>  }\n<\/tt>  angle = atan(y \/ x);\n<\/tt>  if<\/span>(x < 0<\/span> and y >= 0<\/span>)\n<\/tt>    return<\/span> PI - abs(angle);\n<\/tt>  if<\/span>(x < 0<\/span> and y < 0<\/span>)\n<\/tt>    return<\/span> -(PI - abs(angle));\n<\/tt>  return<\/span> angle;\n<\/tt>}\n<\/tt>\/\/ takes no arguments and returns the angle of the coordinate from the positive x axis in degrees<\/span>\n<\/tt>double<\/span> coordinate::danglezero()\n<\/tt>{\n<\/tt>  double<\/span> angle; \/\/ angle of coordinate from positive x axis<\/span>\n<\/tt>  if<\/span>(x == 0<\/span>) \/\/ if x == 0 then you will get a domain error so compute angle manually<\/span>\n<\/tt>  {\n<\/tt>    if<\/span>(y > 0<\/span>) return<\/span> 90<\/span>;\n<\/tt>    if<\/span>(y < 0<\/span>) return<\/span> -90<\/span>;\n<\/tt>    if<\/span>(y == 0<\/span>) return<\/span> 0<\/span>;\n<\/tt>  }\n<\/tt>  angle = atan(y \/ x) * 180<\/span> \/ PI;\n<\/tt>  if<\/span>(x < 0<\/span> and y >= 0<\/span>)\n<\/tt>    return<\/span> 180<\/span> - abs(angle);\n<\/tt>  if<\/span>(x < 0<\/span> and y < 0<\/span>)\n<\/tt>    return<\/span> -(180<\/span> - abs(angle));\n<\/tt>  return<\/span> angle;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Next, we have the quadrant<\/b> function. This looks at whether the x<\/i><\/b> and y<\/i><\/b> 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)<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>int<\/span> coordinate::quadrant()\n<\/tt>{\n<\/tt>  if<\/span>(x > 0<\/span> && y >= 0<\/span>)\n<\/tt>    return<\/span> 1<\/span>; \/\/ return quadrant 1<\/span>\n<\/tt>  if<\/span>(x <= 0<\/span> && y > 0<\/span>)\n<\/tt>    return<\/span> 2<\/span>; \/\/ return quadrant 2<\/span>\n<\/tt>  if<\/span>(x < 0<\/span> && y <= 0<\/span>)\n<\/tt>    return<\/span> 3<\/span>; \/\/ return quadrant 3<\/span>\n<\/tt>  if<\/span>(x >= 0<\/span> && y < 0<\/span>)\n<\/tt>    return<\/span> 4<\/span>; \/\/ return quadrant 4<\/span>\n<\/tt>  return<\/span> 0<\/span>;   \/\/ return 0 if point is (0, 0)<\/span>\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Last, we have the midpoint<\/b> function. you will need to pass this function a coordinate instance and it will calculate the midpoint between the current instance<\/b> and the passed instance<\/b> and print the coordinate instance.<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>void<\/span> coordinate::midpoint(coordinate pt)\n<\/tt>{\n<\/tt>  coordinate np; \/\/ new coordinate point<\/span>\n<\/tt>  np.x = (x + pt.x) \/ 2<\/span>.0<\/span>;\n<\/tt>  np.y = (y + pt.y) \/ 2<\/span>.0<\/span>;\n<\/tt>  np.print();\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Now all you have to do is test each constructor and member function to make sure that everything is working properly.<\/p>\n","protected":false},"excerpt":{"rendered":"

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 […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,6,3,14,30],"tags":[33,34,35],"_links":{"self":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts\/51930"}],"collection":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/comments?post=51930"}],"version-history":[{"count":2,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts\/51930\/revisions"}],"predecessor-version":[{"id":57502,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts\/51930\/revisions\/57502"}],"wp:attachment":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/media?parent=51930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/categories?post=51930"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/tags?post=51930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}