Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This C programming assignment utilizes structures to model a group of rectangles

ID: 3713516 • Letter: T

Question

This C programming assignment utilizes structures to model a group of rectangles.

(This post is broken up into three sections: the first describes what the program should do,

the second shows test cases, the third is the text of what is done so far and what isn't).

The first three files are completed and should not be modified.

The last two files are incomplete and should be modified.

Sample output of final program:

https://imgur.com/a/srwinoW

These files are NOT MODIFIED:

prog8_main.c

Point.h

Rectangle.h

These files ARE MODIFIED:

Point.c

Rectangle.c

The main program (prog8 main.c) recognizes five different single-letter commands most of which call functions described in Point.h or Rectangle.h and defined in the corresponding source file 'A, a: Add a Rectangle to the array of Rectangles, which can contain up to 10 elements, using the readPoint) function to read all four vertices in clockwise fashion, starting with the lower left corner ‘P','p': Print the entire array of Rectangles using the printList ( ) function · "D', 'd: Prompts the user to enter an integer representing an index into the Rectangle array, then print the dimensions (area and perimeter) of that Rectangle, using the area and perimeter functions .'O', 'o': Prompts the user to enter two integers representing indices into the Rectangle array, then checks to see if the two Rectangles overlap one another, using the overlap ) function. .'Q', q' Exit the program Point.h contains the definition of the Point structure, which consists of two coordinates x and y. It also contains prototypes for the following functions, which you must properly define in Point.c: void printPoint (Point *p) Print the x and y coordinates of the Point referenced by the pointer p using the following format: (x.xx, y.yy) (for example, (3.14, 5.22)) void readPoint (Point *p) Read x and y coordinates from the user input into the Point referenced by the pointer p Note that this function does not have to prompt the user for coordinates. double dist (Point pl, Point p2) Return the distance between the Points referenced by pointers p1 and p2. The "distance formula" is fairly well known and can be found with a quick web search. (Note if you decide this function is unnecessary for your solution, you can remove the prototype from Pointh and the definition from Point.)

Explanation / Answer

If you have any doubts, please give me comment...

Point.c

#include "Point.h"

#include <stdio.h>

#include <math.h>

// Print coordinates as (x.xx, y.yy)

void printPoint(Point *p)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printPoint() ***");

printf("(%.2lf %.2lf)", p->x, p->y);

}

// Read input coordinates

void readPoint(Point *p)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF readPoint() ***");

scanf("%lf %lf", &p->x, &p->y);

}

// Return distance between two points

double dist(Point *p1, Point *p2)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF dist() ***");

return sqrt((p2->x - p1->x) * (p2->x - p1->x) + (p2->y - p1->y) * (p2->y - p1->y));

return 0;

}

Rectangle.c

#include "Rectangle.h" // Implicitly includes Point.h

#include <stdio.h>

// Print contents of rectangle

// Prints vertices in appropriate relative positions:

// vert[1] vert[2]

// vert[0] vert[3]

void printRectangle(Rectangle *r)

{

printPoint(&r->vert[1]);

printPoint(&r->vert[2]);

printf(" ");

printPoint(&r->vert[0]);

printPoint(&r->vert[3]);

printf(" ");

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printRectangle() ");

// printf("(%lf %lf) (%lf %lf) ", r->vert[1].x, r->vert[1].y, r->vert[2].x, r->vert[2].y);

// printf("(%lf %lf) (%lf %lf) ", r->vert[0].x, r->vert[0].y, r->vert[3].x, r->vert[3].y);

}

// Print list of n Rectangles

void printList(Rectangle list[], int n)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printList() ");

int i, j;

for (i = 0; i < n; i++){

printRectangle(&list[i]);

printf(" ");

}

}

// Returns area of rectangle

double area(Rectangle *r)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF area() ");

double l = r->vert[3].x - r->vert[0].x;

double w = r->vert[1].y - r->vert[0].y;

return l*w;

}

// Returns perimeter of rectangle

double perimeter(Rectangle *r)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF perimeter() ");

double l = r->vert[3].x - r->vert[0].x;

double w = r->vert[1].y - r->vert[0].y;

return 2*(l+w);

}

// Returns 1 if two rectangles overlap; 0 otherwise

int overlap(Rectangle *r1, Rectangle *r2)

{

// printf("*** YOU MUST WRITE YOUR OWN VERSION OF overlap() ");

if(r1->vert[1].x > r2->vert[3].x || r2->vert[1].x > r1->vert[3].x)

return 0;

if(r1->vert[1].y > r2->vert[3].y || r2->vert[1].y > r1->vert[3].y)

return 0;

return 1;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote