Ok here's my revised code;
I don't think I am allowed to change the members of struct point.
Is there any other way to get floating-point variables to be returned in the
FUNCTON: struct point center_of_r(struct rectangle rect);
????
I didn't change anything here. I tried typecasting the assignments to center.x and center.y to float but that didn't work (and yes I changed the printf line to %5.1f to write a float )
What else can I do here??
Code:
#include <stdio.h>
struct point {
int x, y;
};
struct rectangle {
struct point upper_left, lower_right;
} ;
int area_of_r(struct rectangle rect);
struct point center_of_r(struct rectangle rect);
int main() {
struct rectangle r;
struct point center;
int area;
int coord_x, coord_y;
printf( "input upper left corner of rectangle r "
"as 0 0\n");
scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
printf( "input lower right corner of rectangle r "
"as 0 0\n");
scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
area = area_of_r(r);
printf( "\nThe area of rectangle r is "
"%d\n", area);
center = center_of_r(r);
printf("\nThe center point of rectangle r is "
"%d %d\n\n",center.x, center.y);
return 0;
}
int area_of_r ( struct rectangle rect )
{
int area;
area = (rect.lower_right.x - rect.upper_left.x) *
(rect.upper_left.y - rect.lower_right.y);
return area;
}
struct point center_of_r(struct rectangle rect)
{
struct point center;
center.x = ((rect.lower_right.x) +
(rect.upper_left.x))/2;
center.y = ((rect.upper_left.y) +
(rect.lower_right.y))/2;
return center;
}