Answer to Question #74774 in C for jw

Question #74774
A point represents a coordinate in an x-y plane. It is supported by the following functions:

Point * make_point(double x, double y)
double x_of(Point *p)
double y_of(Point *p)
void print_point(Point *p)

Write a function Point * mid_point that accepts two points as arguments and returns a point that is the mid-point of these two input coordinates.
1
Expert's answer
2018-03-19T08:54:06-0400
#include <stdio.h>
#include <stdlib.h>


typedef struct point_t
{
double x;
double y;
}Point;

Point * make_point(double x, double y)
{
Point* p = malloc(sizeof(Point));
p->x = x;
p->y = y;
}

double x_of(Point *p)
{
return p->x;
}

double y_of(Point *p)
{
return p->y;
}

void print_point(Point *p)
{
printf("Point %lf - %lf", p->x, p->y);
}

Point* mid_point(Point* p1, Point* p2)
{
return make_point((p1->x + p2->x)/2, (p1->y + p2->y)/2);
}


int main()
{
Point* p1 = make_point(3,3);
Point* p2 = make_point(7,7);

Point* mid = mid_point(p1, p2);

print_point(mid);

free(p1);
free(p2);
free(mid);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

jw
18.03.18, 13:13

the input expression will be : mid_point(make_point(1.0, 1.0), make_point(3.0, 3.0))

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS