Program 8 (Rectangles) Source and header files prog8_main.c http://mjgeiger.gith
ID: 3713396 • Letter: P
Question
Program 8 (Rectangles) Source and header files
prog8_main.c http://mjgeiger.github.io/eece2160/programs/src/prog8_main.c
Point.h http://mjgeiger.github.io/eece2160/programs/src/Point.h
Point.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Point.c
Rectangle.h http://mjgeiger.github.io/eece2160/programs/src/Rectangle.h
Rectangle.c (starter file) Program 8 (Rectangles) Source and header files
prog8_main.c http://mjgeiger.github.io/eece2160/programs/src/prog8_main.c
Point.h http://mjgeiger.github.io/eece2160/programs/src/Point.h
Point.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Point.c
Rectangle.h http://mjgeiger.github.io/eece2160/programs/src/Rectangle.h
Rectangle.c (starter file) Source and header files
prog8_main.c http://mjgeiger.github.io/eece2160/programs/src/prog8_main.c
Point.h http://mjgeiger.github.io/eece2160/programs/src/Point.h
Point.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Point.c
Rectangle.h http://mjgeiger.github.io/eece2160/programs/src/Rectangle.h
Rectangle.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Rectangle.c
Explanation / Answer
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 */
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.h
#ifndef Rectangle_h
#define Rectangle_h
#include "Point.c"
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 */
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;
}
prog8_main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "Rectangle.c" // 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 <A | P | D | O | Q>: ");
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 < 0) || (ind >= nRect))
printf("Invalid index %d ", ind);
} while ((nread == 0) || (ind < 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 < 2)
{
printf("Format error for indices input ");
do
{
scanf("%c", &junk);
} while (junk != ' ');
flag = 1;
}
else
{
if ((ind < 0) || (ind >= nRect))
{
printf("Invalid index %d ", ind);
flag = 1;
}
if ((ind2 < 0) || (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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.