1. ## structures

I am rereading about structures and some of it just doesn't make sense right now.

What I need to do is have two structures :

Code:
```struct point {
int x, y;
};

struct rectangle (
struct point upper_left, lower_right;
};```
The point structure stores the x and y coordinates of a point on a screen. // this makes sense of what I recall of graphing points

The rectangle structure stores the coordinates of the upper left and lower right corners of a rectangle // I have no idea what this means ; rectangles have 4 right angles thus 4 corners, why only coordinates of 2 corners ???

I need to write several functions using the above and with r being passed as an argument in rectangle structure // have no idea what this means in laymen's terms.

If I could get help with the first function I would appreciate it.

(1) compute the area of r.

2. > why only coordinates of 2 corners ?
Because that's all you need to describe a rectangle.
You can easily work out the other two corners -
lower_left is upper_left.x and lower_right.y

3. >You can easily work out the other two corners -
>lower_left is upper_left.x and lower_right.y

Well, it isn't so easy for me to work out as you suggest.
I am new to C and structures by far are a bit disconcerting to
get.

But thinking back to my math days...
if a point (input by user of course) is say (5,7) on a graph.
lower_right corner would point to (5,0) and
upper_left corner would point to (0,7)
Right ??

now I am not understanding the syntax of how to
use the members of the rectangle structure labeled
lower_right and upper_left.

(how is it written out to use as variable???)

rectangle.lower_right // does this work OR
rectangle.lower_right.point.x // does this work

I see that there is a structure as a member of a structure (nested structures as they're called)

Please recall my structure for rectangle below
Code:
```struct rectangle {
struct point upper_left, lower_right;
};```
and my structure for point (the point coordinates that will be input by user ) below:

Code:
`struct point {  int x, y;        };`
now the first instruction is to :

write a function that performs the operation (compute the area of r) on a rectangle structure r passed as an argument.

What is r ? Where does it go in the rectangle structure?
Is it a member of the structure?
Code:
```struct rectangle {
struct point upper_left, lower_right;
int r;
};```
Or is it a variable of struct rectangle?
Code:
```struct rectangle {
struct point upper_left, lower_right;
} r ;```
If I could understand the instructions as stated and then the context they are to be used in a problem, it would help a great deal to go on with the rest of the full program.

and I do not know how yet to define own header files

4. In this case 'r' is an object. It is an instance of the structure that you define in main and pass as an arguement to the caller.

struct rectangle r;

Function(r);

If you have a structure in a structure than you access the data in the internal structure through the object as in:

r.upper_left.x = 2;
r.upper_left.y = 3;

This is if the object is passed to the function by value. If you pass by reference as in a pointer you need to use the '->' operator instead of the dot.

5. so if I ask user to input the x value (point structure member) and
the y value (point structure member) as the coordinates of a point

then to compute area of r

int area;
area = (x.point) * (y.point);
r.rectangle = area;

Is any of this referencing variables correctly???

6. To get area you would have to do this:

int area = (r.upper_left.x) * (r.upper_left.y);

This is doing the calculation through the rectangle object and would likely be the correct answer due to the fact that the point structure is declared in the rectangle structure.

Actually how do you calculate the area. First you would have to tall me how to use the algorithm. This is just how to access the integers stored in the points. Actually the true area of the rectangle would be slightly different. Figure it out on paper first.

7. It would be more like:

int area = (r.lower_right.x - r.upper_left.x) * (r.lower_right.y - r.upper_left.y);

correct?

8. ## CODE so far

Here's what code written so far;
given 4 separate functions.

Right now working on understanding the referencing the members and variables of the named structures.

Trying to use area_of_r to be the function used to calculated the area of r ( r being the point referenced with the x and y values input by user). x an y are int variables. thus r coordinates will be something like (6,7)

Code:
```

#include <stdio.h>

struct point {
int x, y;
};

struct rectangle {
struct point upper_left, lower_right;
} r;

void area_of_r(int x, int y);
void center_of_r(int x, int y);
void move_r(int x, int y);
void point_on_r(int x, int y);

main()

{

/*  get value of x and y of point r on graph   */

printf("input x value of point r : ");
scanf("%d",x.point);
printf("\n");
printf("input y value of point r : ");
scanf("%d",y.point);
printf("\n");

printf("point r coordinates are : (%d,%d)",x.point.y.point`);

}

void area_of_r(int x, int y)
{
int area;

r.upper_left.y = y;
r.lower_right.x = x;

area = (r.upper_left.y * r.lower_right.x);
}```

9. Code:
```#include <stdio.h>

struct point {
int x, y;
};

struct rectangle {
struct point upper_left, lower_right;
} ;

void area_of_r(int x, int y);
void center_of_r(int x, int y);
void move_r(int x, int y);
void point_on_r(int x, int y);

int main()

{
int area;
struct rectangle r;
/*  get value of x and y of point r on graph   */

printf("input upper left (x,y) x value of point r : ");
scanf("%d",&r.upper_left.x);

printf("\ninput upper left (x,y) y value of point r : ");
scanf("%d",&r.upper_left.y);

printf("\npoint r coordinates are : (%d,%d)",r.upper_left.x, r.upper_left.y);

printf("input lower_right (x,y) x value of point r : ");
scanf("%d",&r.lower_right.x);

printf("\ninput lower_right (x,y) y value of point r : ");
scanf("%d",&r.lower_right.y);

printf("\npoint r coordinates are : (%d,%d)",r.lower_right.x, r.lower_right.y);

area = area_of_r(r);

return 0;
}

int area_of_r(struct rectangle r)
{
int area = (r.lower_right.x - r.upper_left.x) * (r.lower_right.y - r.upper_left.y);
return area;

}```
Now the upper left (x,y) and lower right (x,y) input has to make sense. Try something like (2,2) and (6,8).

10. Code:
```#include <stdio.h>
struct point {
int x, y;
};
struct rectangle {
struct point upper_left, lower_right;
} r;

int area_of_r(struct rectangle rect);

int main() {
printf( "input upper left corner of r : " );
scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
printf( "input lower right corner of r : " );
scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
printf( "area of r is %d\n", area_of_r(r) );
return 0;
}

int area_of_r ( struct rectangle rect ) {
int area;
area = (rect.lower_right.x - rect.upper_left.x ) *
(rect.lower_right.y - rect.upper_left.y );
return area;
}```
bah!
beaten by a few mins...

11. Does it make sense now sballew?

12. ## yuneek

hold it
hold it

let me see here

struct point { int x, y; };

is used to give coordinates of any point
struct rectangle { struct point upper_left, lower_right; };
is making a rectangle out of the points (coordinates) of more than one point that when connected forms a rectangle , eh?

so the point structure is refered to more than once to find the 2 points that make up 2 corners of the rectangle (am I getting close here??)

int main() {
printf( "input upper left corner of r : " );
scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
printf( "input lower right corner of r : " );
scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
printf( "area of r is %d\n", area_of_r(r) );
return 0;
}
and r is an actual rectangle not a variable of struct rectangle

I swear I am trying to get the idea of what the problem is asking for and getting hung up on the "picture" of it in my head.

If r is a rectangle,
then you can

1. compute the area of r
2. find it's center (returning it as a point value)
3. move r by x units in x-direction and y units in y-direction (where x and y are additional arguments to the function)
4. determine whether a point p lies within r (the rectangle) - returning TRUE or FALSE (p being an additional argument of type struct point)