Define a struct rect as follows: typedef struct rect } double x double y char co
ID: 3652672 • Letter: D
Question
Define a struct rect as follows: typedef struct rect } double x double y char color double w width double h height } rectt write a program that declares rectt a, b, *recs and asks user to enter x, y, w, h of both a and b. write a function int chk_overlap(rectt *r1, rectt *r2) which can be called to check if a and b overlap or not. if so return 1, else return 0. dynamically create an array of 50 rectangles and save their address in recs. then randomly initialize x, y, w, h of each rectangle (e.g., recs[i].x = rand() ) countprint how many of these rectangles overlap with a or b.Explanation / Answer
#include #include #include typedef struct rect { double x; double y; double w; double h; }rectt; int chk_overlap(rectt *r1,rectt *r2) { if((r2->x)>=(r1->x)+(r1->w)) return 0; if((r1->x)>=(r2->x)+(r2->w)) return 0; if((r2->y)>=(r1->y)+(r1->h)) return 0; if((r1->y)>=(r2->y)+(r2->h)) return 0; return 1; } int main() { srand(unsigned(time(NULL))); rectt *a, *b, *recs; a = (rectt *)malloc(sizeof(rectt)); b = (rectt *)malloc(sizeof(rectt)); double temp; printf("Enter x-coordinate of a: "); scanf("%lf",&temp); a->x=temp; printf("Enter y-coordinate of a: "); scanf("%lf",&temp); a->y=temp; printf("Enter width of a: "); scanf("%lf",&temp); a->w=temp; printf("Enter height of a: "); scanf("%lf",&temp); a->h=temp; printf("Enter x-coordinate of b: "); scanf("%lf",&temp); b->x=temp; printf("Enter y-coordinate of b: "); scanf("%lf",&temp); b->y=temp; printf("Enter width of b: "); scanf("%lf",&temp); b->w=temp; printf("Enter height of b: "); scanf("%lf",&temp); b->h=temp; if(chk_overlap(a,b)) printf(" Rectangle a and b overlaps. "); else printf(" Rectangle a and b does not overlap. "); recs = (rectt *)malloc(50*sizeof(rectt)); int i; for(i=0;ix=rand()%1000000; (recs+i)->y=rand()%1000000; (recs+i)->w=rand()%1000000; (recs+i)->h=rand()%1000000; } printf("Randomly generated 50 rectangles. "); int counta=0; int countb=0; for(i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.