Starting from:

$29.99

Recitation Exercise 04 Solution

Struct, Arrays, Dynamic memory - functions



 

 

x        Define a struct Rect as follows:

 

typedef struct Point {

 

double x;

double y;

 

} PointT;

 

typedef struct Rect {

 

PointT location; char color; double w; //width; double h; //height;

 

} RectT;


 

 

 

y

 

 

 

 

 

w

 

h

 

(location.x, location.y)

 

x

 

x        Write a program that declares RectT a, b, *recs; and asks user to enter location.x, location.y, w, h for both a and b.

 

x        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. Note you need to pass the addresses of a an b to that function!

 

x        Dynamically create an array of 50 rectangles (an array of RectT type data) and save the address of that array to recs. Then randomly initialize location.x, location.y, w, h of each rectangle

 

(e.g., recs[i].location.x = rand()%20; etc...)

 

x        Count/print how many of these rectangles overlap with a alone, b alone, and both a and b. So you need to keep three counters.

After implementing the program

 

x        Run it with valgrind to see memory usage info valgrind rec04

 

x        Free the allocated memory then run it again with valgrind

 

x        Compile your program with –g option and use gdb and ddd

 

 

/*    Don’t forget to include comments about the problem, yourself and

 

each major step in your program!  */

 

More products