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 alton

    sam alton Guest

    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
    quadrant 4.
     
    sam alton, Sep 15, 2010
    #1
    1. Advertising

  2. sam alton

    red floyd Guest

    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
    > quadrant 4.


    Your program may be found at the following address:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
     
    red floyd, Sep 15, 2010
    #2
    1. Advertising

  3. 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
    > quadrant 4.


    // 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;

    enum { ORIGIN, X_AXIS, Y_AXIS, QUAD_1, QUAD_2, QUAD_3, QUAD_4 };
    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";
    template<> std::string message<QUAD_1>::value = "In quadrant 1";
    template<> std::string message<QUAD_2>::value = "In quadrant 2";
    template<> std::string message<QUAD_3>::value = "In quadrant 3";
    template<> std::string message<QUAD_4>::value = "In quadrant 4";

    typedef mpl::and_<x_equal_0, y_equal_0> at_origin;
    typedef mpl::eval_if<x_less_0, message<QUAD_2>, message<QUAD_1> > q12_msg;
    typedef mpl::eval_if<x_less_0, message<QUAD_3>, message<QUAD_4> > q34_msg;
    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
    #3
  4. sam alton

    sam alton Guest

    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
    > > quadrant 4.

    >
    > // 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;
    >
    > enum { ORIGIN, X_AXIS, Y_AXIS, QUAD_1, QUAD_2, QUAD_3, QUAD_4 };
    > 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";
    > template<> std::string message<QUAD_1>::value = "In quadrant 1";
    > template<> std::string message<QUAD_2>::value = "In quadrant 2";
    > template<> std::string message<QUAD_3>::value = "In quadrant 3";
    > template<> std::string message<QUAD_4>::value = "In quadrant 4";
    >
    > typedef mpl::and_<x_equal_0, y_equal_0>                       at_origin;
    > typedef mpl::eval_if<x_less_0, message<QUAD_2>, message<QUAD_1> > q12_msg;
    > typedef mpl::eval_if<x_less_0, message<QUAD_3>, message<QUAD_4> > q34_msg;
    > 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
    #4
  5. 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
    > quadrant 4.


    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
    #5
  6. 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
    #6
  7. 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
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov

    Coordinate of a point in 3 dimensions

    Piotre Ugrumov, Jan 20, 2004, in forum: C++
    Replies:
    8
    Views:
    437
    Jumbo
    Jan 21, 2004
  2. =?UTF-8?Q?Edin_Salkovi=C4=87?=

    Problems with Unicode Plane 1 characters on Windows

    =?UTF-8?Q?Edin_Salkovi=C4=87?=, Jul 12, 2006, in forum: Python
    Replies:
    0
    Views:
    272
    =?UTF-8?Q?Edin_Salkovi=C4=87?=
    Jul 12, 2006
  3. vijay

    C++ in data plane

    vijay, Aug 23, 2007, in forum: C++
    Replies:
    10
    Views:
    580
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 24, 2007
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,341
    Saraswati lakki
    Jan 6, 2012
  5. Michael Linfield

    ruby graph: coordinate plane?

    Michael Linfield, Apr 5, 2008, in forum: Ruby
    Replies:
    4
    Views:
    140
    Marcelo
    Apr 6, 2008
Loading...

Share This Page