Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

Test it in online simultor with below expression but couldn't get it right
mid_point(make_point(1.0, 1.0), make_point(3.0, 3.0))


my code:
Point * mid_point(Point *x, Point *y) {
    
    int mid;

    mid = make_point((x_of (x) + x_of(x) ) / 2,  (y_of(y) + y_of(y) ) / 2);
    
    print_point(mid);
    
}
Posted
Updated 18-Mar-18 7:53am
v2
Comments
Patrice T 18-Mar-18 7:43am    
What is the question?

Your syntax is wrong: you need to access your coordinates via the "->" operator with pointers:
C++
double x1 = x->x_off;
double y1 = x->y_off;
I'd suggest that you change the names of the function parameters to reduce confusion:
C++
Point* mid_point(Point* a, Point* b)
   {
   ...
   }
 
Share this answer
 
Quote:
Point * mid_point(Point *x, Point *y) {

int mid;

mid = make_point((x_of (x) + x_of(x) ) / 2, (y_of(y) + y_of(y) ) / 2);

print_point(mid);

}


Change to
Point * mid_point(Point *p, Point *q) {
    
    Point * m;

    m = make_point((x_of (p) + x_of(q) ) / 2,  (y_of(p) + y_of(q) ) / 2);
    
    print_point(m);

    return m;
}
 
Share this answer
 
You need to implement some functions like
C++
double x_of(Point *p)
{
  return p->x;
}
but this misses error handling of null pointers so
C++
double x_of(Point *p)
{
  return p ? p->x : 0;
}
should be correct.

And pay attention to memory managment when you allocate memory you must delete it.
Learn to use the debugger and the usage of print or TRACE for your homework.

Search for some tutorial on this language details.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900