how to access to member;

Write your question here.

1
2
3
4
5
6
7
8
9
10
struct Point
{
    int x;
    int y;
};
struct Rectangle
{
    Point first_point;
    Point sec_point;
};


how can i store the information for first_point?

1
2
3
4
5
Point Z;
Rectangle t;
cout <<"What is first corr." << endl;
cin >>????


other question is how can i store two variable in one member?

For part one you will do Z.x or Z.y depending on which part of the point you wish to access. As far as the second question can you rephrase please. From what I am understanding you want a static array like: Point points[2]; or something along those lines.
How to access the members of a struct:
1
2
Rectangle t;
cin >> t.first_point.x >> t.first_point.y;


Alternately (you probably can't do this, just here for completion's sake:
1
2
3
4
5
6
7
8
9
10
std::istream& operator>> (std::istream& in, Point& p) {
    in >> p.x >> p.y;
    return in;
}

// ...

Rectangle t;
std::cout << "What is first corr." << std::endl;
std::cin >> t.first_point;


As for storing two variables in one member, there are a few ways. The most common and easy way is to use a struct (like you did with the 'Point' structure). Alternate ways include an array (don't forget to allocate and deallocate it properly, maybe use a smart pointer) or std::pair:
1
2
3
4
5
6
7
8
9
// struct
Point pointstruct = {1, 5};

// array
int pointarray[2] = {1, 5};

// std::pair
#include <utility>
std::pair<int, int> pointpair = {1, 5};


Just look them up for more details.
how do you pass the t as argument?
Last edited on
bump?
Question: Find coordinates of the center of t, returning information as a Point structure. (If either x or y coordinate of a center is not an integer, store its truncated value instead.)

I dont understand this question very much (ENglish is my 7th lang!)

here is what I got

1
2
3
4
5
6
7
8
9
10
11
12
findcricle(x1,y2,x1,y2);
    
    return 0;
}
void findcricle (int x1,int y1,int x2,int y2)
{
    int midpoint_x, midpoint_y;
    
    midpoint_x = (x1+x2)/2;
    midpoint_y = (y1+y2)/2;
    
}
1
2
3
4
5
6
7
8
Point centre( Rectangle r ) // returning information as a Point structure.
{
      const int midpoint_x = ( r.first_point.x + r.sec_point.x ) / 2;
      const int midpoint_y = ( r.first_point.y + r.sec_point.y ) / 2;

      const Point its_centre = { midpoint_x, midpoint_y } ;
      return its_centre ; 
}
Last edited on
why we have use const int? why not int?
Last edited on
cant i get to display in main

i put

t = centre (Rectangle t);
> why we have use const int? why not int?

You may use an int, as long as you do not modify the values.

The const int variables and the const Point were there to annotate the code; if that was not a requirement:
1
2
3
4
5
Point centre( Rectangle r )
{
      return { ( r.first_point.x + r.sec_point.x ) / 2,
               ( r.first_point.y + r.sec_point.y ) / 2 } ;
}



> cant i get to display in main

1
2
3
4
Rectangle t = { { 1, 2 }, { 8, 9 } } ;

const Point c = centre(t) ;
std::cout << '(' << c.x << ',' << c.y << ")\n" ; // (4,5) 
Topic archived. No new replies allowed.