/* Define a struct to describe a point in the 2D plane, modify the coordinate using pointer and find perimeter of an arbitrary triangle Emad Arasteh December 6, 2018 */ #include #include typedef struct { int x; int y; } point_t; void display(point_t p) { printf("(x, y) coordinate is (%d, %d) \n", p.x, p.y); } void modify(point_t* p, int x, int y) { p->x = x; p->y = y; } double perimeter(const point_t vertices[]) { double perimeter = 0; int i; for (i = 0; i < 2; i++) { perimeter += sqrt(pow(vertices[i+1].x - vertices[i].x, 2) + pow(vertices[i+1].y - vertices[i].y, 2)); } return perimeter; } int main() { point_t p1; point_t* p2; point_t tri[3] = { {0, 0}, {6, 0}, {6, 8}} ; /* vertices of an arbitrary triangle */ int i; p1.x = 76; p1.y = 10; p2 = &p1; display(p1); modify(p2, 42, 42); display(p1); printf("Vertices of a triangle has the following coordinates: \n"); for(i = 0; i < 3; i++) { display(tri[i]); } printf("Perimter of the triangle is %f \n", perimeter(tri)); return 0; }