Please explain why the answer is what is is thoroughly. I am trying to study for
ID: 3827663 • Letter: P
Question
Please explain why the answer is what is is thoroughly. I am trying to study for my test. I will make sure to thumbs up! thank you very much for your time. I dont really get structs.
struct point center(struct rectangle r) {
struct point c;
c.x = (r.upper_left.x + r.lower_right.x) / 2; c.y = (r.upper_left.y + r.lower_right.y) / 2; return c;
}
17. The following structures are designed to store information about objects in a Cartesian coordinate system struct point int x, y struct rectangle fint upper left x, upper left y, lower right x, lower right y; A point structure stores the x and y coordinates of a point. A rectangle structure stores the coordinates of the upper left and lower right corners of a rectangle. Write a function that computes the center of rectangle r, returning it as a point value. If either the x or y coordinate of the center isn't an integer, store its truncated value in the point structure.Explanation / Answer
Please let me know if you have any doubt.
About structure:
A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.
‘struct’ keyword is used to create a structure. Following is an example.
struct addrress
{
char name[50];
char street[100];
char city[50];
char state[20]
int pin;
};
A structure variable can either be declared with structure declaration or as a separate declaration like basic types.
// A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'
// A variable declaration like basic data types
struct Point
{
int x, y;
};
int main()
{
struct Point p1; // The variable p1 is declared like a normal variable
}
Comining to Question:
struct point center(struct rectangle r) {
struct point c; // declaring a structure of type point
c.x = (r.upper_left.x + r.lower_right.x) / 2; // storing middle point in x member of c of upper_left and lower_right value of x
c.y = (r.upper_left.y + r.lower_right.y) / 2; // storing middle point in y member of c of upper_left and lower_right value of y
return c;
}