# Writting program which will read a coordinate pair for a point in thex-y plane.

Discussion in 'C++' started by sam alton, Sep 15, 2010.

1. ### sam altonGuest

Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in

sam alton, Sep 15, 2010

2. ### red floydGuest

Re: Writting program which will read a coordinate pair for a pointin the x-y plane.

On 9/14/2010 7:34 PM, sam alton wrote:
> Write a program which will read a coordinate pair for a point in the x-
> y plane. It should output whether the point is at the origin (0, 0),
> on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
> one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
> is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

red floyd, Sep 15, 2010

3. ### Garrett HartshawGuest

Re: Writting program which will read a coordinate pair for a pointin the x-y plane.

On 09/14/2010 10:34 PM, sam alton wrote:
> Write a program which will read a coordinate pair for a point in the x-
> y plane. It should output whether the point is at the origin (0, 0),
> on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
> one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
> is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in

// Point.cpp
//
// To use: Compile with the point's coordinates defined as XPOS and YPOS
// and run the resulting file
// e.g. To test the origin (0, 0) run the following command
// (assuming you are using gcc):
// g++ Point.cpp -DXPOS=0 -DYPOS=0 && ./a.out
//
#include <iostream>
#include <string>

#include <boost/mpl/int.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/eval_if.hpp>
namespace mpl = boost::mpl;

typedef mpl::int_<XPOS> xpos;
typedef mpl::int_<YPOS> ypos;
typedef mpl::int_<0> zero;

typedef mpl::less<xpos, zero> x_less_0;
typedef mpl::equal_to<xpos, zero> x_equal_0;
typedef mpl::greater<xpos, zero> x_greater_0;

typedef mpl::less<ypos, zero> y_less_0;
typedef mpl::equal_to<ypos, zero> y_equal_0;
typedef mpl::greater<ypos, zero> y_greater_0;

template <int msg> struct message { typedef message<msg> type; static
std::string value; };
template<> std::string message<ORIGIN>::value = "At the origin";
template<> std::string message<X_AXIS>::value = "On the x-axis";
template<> std::string message<Y_AXIS>::value = "On the y-axis";

typedef mpl::and_<x_equal_0, y_equal_0> at_origin;
typedef mpl::eval_if<y_greater_0, q12_msg, q34_msg> q_msg;
typedef mpl::eval_if<x_equal_0, message<Y_AXIS>, q_msg> ya_msg;
typedef mpl::eval_if<y_equal_0, message<X_AXIS>, ya_msg> aq_msg;
typedef mpl::eval_if<at_origin, message<ORIGIN>, aq_msg> msg;

int main() { std::cout << msg::type::value << std::endl; }

-Garrett

P.S. this is not meant to be an actual solution, I just wanted to
practice template metaprogramming.

Garrett Hartshaw, Sep 15, 2010
4. ### sam altonGuest

Re: Writting program which will read a coordinate pair for a point inthe x-y plane.

On Sep 14, 10:13 pm, Garrett Hartshaw <> wrote:
> On 09/14/2010 10:34 PM, sam alton wrote:
>
> > Write a program which will read a coordinate pair for a point in the x-
> > y plane. It should output whether the point is at the origin (0, 0),
> > on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
> > one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
> > is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in

>
> // Point.cpp
> //
> // To use: Compile with the point's coordinates defined as XPOS and YPOS
> //         and run the resulting file
> // e.g. To test the origin (0, 0) run the following command
> //       (assuming you are using gcc):
> // g++ Point.cpp -DXPOS=0 -DYPOS=0 && ./a.out
> //
> #include <iostream>
> #include <string>
>
> #include <boost/mpl/int.hpp>
> #include <boost/mpl/and.hpp>
> #include <boost/mpl/comparison.hpp>
> #include <boost/mpl/eval_if.hpp>
> namespace mpl = boost::mpl;
>
> typedef mpl::int_<XPOS> xpos;
> typedef mpl::int_<YPOS> ypos;
> typedef mpl::int_<0>    zero;
>
> typedef mpl::less<xpos, zero>     x_less_0;
> typedef mpl::equal_to<xpos, zero> x_equal_0;
> typedef mpl::greater<xpos, zero>  x_greater_0;
>
> typedef mpl::less<ypos, zero>     y_less_0;
> typedef mpl::equal_to<ypos, zero> y_equal_0;
> typedef mpl::greater<ypos, zero>  y_greater_0;
>
> template <int msg> struct message { typedef message<msg> type; static
> std::string value; };
> template<> std::string message<ORIGIN>::value = "At the origin";
> template<> std::string message<X_AXIS>::value = "On the x-axis";
> template<> std::string message<Y_AXIS>::value = "On the y-axis";
>
> typedef mpl::and_<x_equal_0, y_equal_0>                       at_origin;
> typedef mpl::eval_if<y_greater_0, q12_msg, q34_msg>               q_msg;
> typedef mpl::eval_if<x_equal_0, message<Y_AXIS>, q_msg>           ya_msg;
> typedef mpl::eval_if<y_equal_0, message<X_AXIS>, ya_msg>          aq_msg;
> typedef mpl::eval_if<at_origin, message<ORIGIN>, aq_msg>          msg;
>
> int main() { std::cout << msg::type::value << std::endl; }
>
>   -Garrett
>
> P.S. this is not meant to be an actual solution, I just wanted to
> practice template metaprogramming.

Thank you so much actually i know alittle bit of template
metaprogramming, and this working fine except some few error but i
corrected. Can you try writing in C++ is you can?

sam alton, Sep 15, 2010
5. ### Saeed AmrollahiGuest

Re: Writting program which will read a coordinate pair for a point inthe x-y plane.

On Sep 15, 5:34 am, sam alton <> wrote:
> Write a program which will read a coordinate pair for a point in the x-
> y plane. It should output whether the point is at the origin (0, 0),
> on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
> one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
> is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in

Hi Sam
Your question seems to be a homework/assignment.
You have to make an effort to C++ programming at first place.
BTW, I'll show you some clues to your problem:
1. You can design/define a Point class like this:
class Point { // 2D point
int x, y;
public:
// set of constructors
Point() : x(0), y(0) {}
Point(int x_, int y_) : x(x_), y(y_) {}
// ...
// Examining object states
int X() const { return x; }
int Y() const { return y; }
// Where is point?
enum EWhere { CENTER, X_AXIS, Y_AXIS, QUADRANT_1, ... };
EWhere Where() const
{
// A lot of if-else
if (x == 0 && y == 0) return CENTER;
else if (y == 0) return X_AXIS;
// ...
}
};
2. You can define 2 (friend) I/O operators:
istream& operator>>(const istream&, Point&);
ostream& operator<<(ostream&, const Point&);
3. If the type of coordinates of points may be vary (sometimes int,
sometimes double, ...), you can define a template class:
template<class T>
class Point {
// like before
};

4. You can do a lot of good things to extend the problem statement.

Regards,
-- Saeed Amrollahi

Saeed Amrollahi, Sep 15, 2010
6. ### Garrett HartshawGuest

Re: Writting program which will read a coordinate pair for a pointin the x-y plane.

>> P.S. this is not meant to be an actual solution, I just wanted to
>> practice template metaprogramming.

>
> Thank you so much actually i know alittle bit of template
> metaprogramming, and this working fine except some few error but i
> corrected. Can you try writing in C++ is you can?

Since this does seem to be a homework question, I will not translate it
for you, however, as you said you know some metaprogramming, it should
not be that hard to translate it to runtime executable code.
-Garrett

Garrett Hartshaw, Sep 15, 2010
7. ### Seungbeom KimGuest

Re: Writting program which will read a coordinate pair for a pointin the x-y plane.

On 2010-09-15 02:00, Saeed Amrollahi wrote:
> 1. You can design/define a Point class like this:
> class Point { // 2D point
> int x, y;
> public:
> // set of constructors
> Point() : x(0), y(0) {}
> Point(int x_, int y_) : x(x_), y(y_) {}
> // ...
> // Examining object states
> int X() const { return x; }
> int Y() const { return y; }
> // Where is point?
> enum EWhere { CENTER, X_AXIS, Y_AXIS, QUADRANT_1, ... };
> EWhere Where() const
> {
> // A lot of if-else
> if (x == 0 && y == 0) return CENTER;
> else if (y == 0) return X_AXIS;
> // ...
> }
> };

I wonder if you really need a class with private data members and
public accessor functions, when the need for information hiding and
invariant maintenance is not clear. Wouldn't

struct point { int x, y; };

suffice? My rule has been to write classes with private data members
only for those that need information hiding and invariant maintenance.

(I might even be tempted to go further and write:

typedef std::array<int, 2> point;

which would be better with a "strong typedef" that created a new type.)

Then Where can be a simple free function that takes a const point&
argument, and you don't have to make operator<< and operator>> friends.

--
Seungbeom Kim

Seungbeom Kim, Sep 16, 2010