Lab 7 Ex1

You cannot submit for this problem because the homework's deadline is due.

Ex1. Points (20 marks)

Implement a data type to represent points in a 2D Cartesian coordinate system.

We have provided you with a file Point.h that contains a struct definition as well as declarations for three functions:

// Custom type for 2D points in Cartesian coordinate system.
typedef struct {
    double x;
    double y;
} Point;

// Create a point with coordinate (x, y) and return it.
Point create_point(double x, double y);

// Return the distance between two points. The inputs are given as pointers to
// reduce copy.
double point_distance(const Point* p1, const Point* p2);

// Scale the given point by multiplying its coordinates. The input point is
// passed in as a pointer and should be modified.
void scale_point(Point* p, double scale);

Implement the three functions in a new file named Point.c. Note that to test your function implementation, you need to write your own driver program. Submit Point.c only!

The distance here refers to the usual Euclidean distance.

You can use any C standard library function, including those declared in math.h.

We will use a driver program to test your implementation. An example driver program ex1_sample_test.c can be downloaded from Canvas, and its output is shown below:

p1: (0.500, 0.000)
p2: (3.500, -4.000)
distance between p1 and p2: 5.000
scaled p1: (1.250, 0.000)

As long as your implementation is correct, you will pass the tests on JOJ. You do not need to worry about I/O.

Lab 7 Exercises

Not Claimed
Status
Finished
Problems
4
Open Since
2022-06-30 18:15
DDL
2022-07-01 23:59
Extension
0.0 hour(s)