I am having trouble with the creating the following program in C programming. Pl
ID: 3606209 • Letter: I
Question
I am having trouble with the creating the following program in C programming. Please check to make sure the answer is correct because I have had some issues recently with programs provided not working properly. Please make it as simple and basic as possible. Here is the program I need help with:
Create a pointer structure that has members for the x and y coordinates. Use typedef to make a point_t data type. Create a rectangle structure that has a point_t member for the upper left corner of the rectangle, but also a float for the height and a float for the width. Use typedef to make a rectangle_t data type. Create a function called is_in() that takes in a point_t variable and a rectangle_t variable, both by reference, and returns 0 if the point is not in the rectangle and 1 if the point is in the rectangle. Use computer science y-axis (i.e., the origin is in the upper left corner of the screen and y values increases further down the screen). Write a program that gets a point from the user and the point for the upper left corner and dimensions of a rectangle and then tells whether the point is in the rectangle or not.
Sample Runs:
Enter a point (x and y): 0.0 0.0
Enter a rectangle (x, y, width, and height): -50.0 -50.0 100.0 100.0
Point is inside the rectangle
Enter a point (x and y): 200.0 200.0
Enter a rectangle (x, y, width, and height): -50.0 -50.0 100.0 100.0
Point is outside the rectangle
Explanation / Answer
the required code is as below :
-------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
typedef struct Point{
float x;
float y;
}point_t;
typedef struct Rectangle{
point_t l_corner;
float height;
float width;
}rectangle_t;
int is_in( point_t *p, rectangle_t *rect ){
/* if given x of the point is less that the left corner x and greter that left corner x + width of the rectangle*/
if( (rect->l_corner).x < (p->x) && ((rect->l_corner).x)+rect->width > p->x ){
/* if given y of the point is less that the left corner y and greater that left corner + height of the rectangle*/
if( (rect->l_corner).y < (p->y) && ((rect->l_corner).y)+rect->height > p->y ){
/* then the point is inside the rectangle */
/* return 1 */
return 1;
}
}
/* else point is outside the rectangle , return 0 */
return 0;
}
int main(){
point_t p;
rectangle_t rect;
/* scan the point */
printf("Enter a point : ");
scanf("%f %f",&(p.x),&(p.y));
/* scan left corner of rectangle and height, width */
printf("Enter a rectangel(x,y,height,width) : ");
scanf("%f %f %f %f",&(rect.l_corner).x,&(rect.l_corner).y,&rect.height,&rect.width);
if( is_in(&p,&rect) )
printf("Point inside the rectangle");
else
printf("Point outside the rectangle");
return 0;
}
/* hope this helps */
/* if any queries please comment */
/* thank you */
----------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.