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
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
#define _CRT_SECURE_NO_WARNINGS #include#include "Rectangle.h" // Includes "Point.h" as well int main() { Rectangle rlist[10]; // Array containing up to 10 rectangles int nRect = 0; // Number of rectangles char cmd; // Single character command int ind, ind2; // Loop index int flag; // Flag to simplify conditionals int nread; // Number of inputs read char junk; // Used to clear input // Loop until user enters 'Q' while (1) { printf(" Enter command : "); scanf(" %c", &cmd); switch (cmd) { // Add rectangle case 'A': case 'a': if (nRect == 10) // List is full printf("No room in list of rectangles "); else { printf("Enter coordinates as x y, starting with lower left hand corner: "); readPoint(&rlist[nRect].vert[0]); readPoint(&rlist[nRect].vert[1]); readPoint(&rlist[nRect].vert[2]); readPoint(&rlist[nRect].vert[3]); nRect++; } break; // Print list case 'P': case 'p': if (nRect == 0) // List is empty printf("No rectangles in list "); else printList(rlist, nRect); break; // Print dimensions case 'D': case 'd': // Ask for index into array and validate input do { printf("Enter index into array: "); nread = scanf("%d", &ind); if (nread == 0) { printf("Format error for index input "); do { scanf("%c", &junk); } while (junk != ' '); } else if ((ind = nRect)) printf("Invalid index %d ", ind); } while ((nread == 0) || (ind = nRect)); // Actually print dimensions of desired rectangle printf("Area of rectangle %d: %.2lf ", ind, area(&rlist[ind])); printf("Perimeter of rectangle %d: %.2lf ", ind, perimeter(&rlist[ind])); break; // Test for overlap of two rectangles case 'O': case 'o': // Prompt for indices into array and validate input // Uses a slightly different method for looping // than we've used before (set a "flag" to show // error has occurred and only test that flag // at the end of each loop iteration) do { flag = 0; printf("Enter indices to test: "); nread = scanf("%d %d", &ind, &ind2); if (nread = nRect)) { printf("Invalid index %d ", ind); flag = 1; } if ((ind2 = nRect)) { printf("Invalid index %d ", ind2); flag = 1; } } } while (flag == 1); // Use overlap function to test for overlap if (overlap(&rlist[ind], &rlist[ind2])) printf("Rectangles %d and %d overlap ", ind, ind2); else printf("Rectangles %d and %d do not overlap ", ind, ind2); break; // Exit program case 'Q': case 'q': return 0; default: printf("Invalid command %c ", cmd); } } return 0; }
Point.h
#ifndef Point_h #define Point_h typedef struct { double x; // X coordinate double y; // Y coordinate } Point; // Print coordinates as (x.xx, y.yy) void printPoint(Point *p); // Read input coordinates void readPoint(Point *p); // Return distance between two points double dist(Point *p1, Point *p2); #endif /* Point_h */
Rectangle.h
#ifndef Rectangle_h #define Rectangle_h #include "Point.h" typedef struct { Point vert[4]; // List of 4 vertices // Functions all assume that vert[0] = lower // left corner, vert[1] = upper left corner // vert[2] = upper right corner, // vert[3] = lower right corner } Rectangle; // Print contents of rectangle void printRectangle(Rectangle *r); // Print list of n Rectangles void printList(Rectangle list[], int n); // Returns area of rectangle double area(Rectangle *r); // Returns perimeter of rectangle double perimeter(Rectangle *r); // Returns 1 if two rectangles overlap; 0 otherwise int overlap(Rectangle *r1, Rectangle *r2); #endif /* Rectangle_h */
These files ARE MODIFIED:
Point.c
#include "Point.h" #include#include // Print coordinates as (x.xx, y.yy) void printPoint(Point *p) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF printPoint() ***"); } // Read input coordinates void readPoint(Point *p) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF readPoint() ***"); } // Return distance between two points double dist(Point *p1, Point *p2) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF dist() ***"); return 0; }
Rectangle.c
#include "Rectangle.h" // Implicitly includes Point.h #include// Print contents of rectangle // Prints vertices in appropriate relative positions: // vert[1] vert[2] // vert[0] vert[3] void printRectangle(Rectangle *r) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF printRectangle() "); } // Print list of n Rectangles void printList(Rectangle list[], int n) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF printList() "); } // Returns area of rectangle double area(Rectangle *r) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF area() "); return 0; } // Returns perimeter of rectangle double perimeter(Rectangle *r) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF perimeter() "); return 0; } // Returns 1 if two rectangles overlap; 0 otherwise int overlap(Rectangle *r1, Rectangle *r2) { printf("*** YOU MUST WRITE YOUR OWN VERSION OF overlap() "); return 0; }
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.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
