Question: C Code - Sorting // points.c // This program reads a list of points, sorts them by x-coordinates // and y-coordinates, and computes the total
C Code - Sorting

// points.c // This program reads a list of points, sorts them by x-coordinates // and y-coordinates, and computes the total length of horizontal and // vertical lines if we trace the points in the sorted list. #include#define MAX_POINTS 20 typedef struct { int x, y; // x- and y-coordinates of a point } point_t; int scanPoints(point_t []); void printPoints(point_t [], int); void sortPoints(point_t [], int); int lessThan(point_t [], int, int); int traceLines(point_t [], int); int main(void) { point_t points[MAX_POINTS]; int num_points; // number of points num_points = scanPoints(points); // printf("Before sort: "); // printPoints(x, y, num_points); // for checking sortPoints(points, num_points); printf("After sort: "); printPoints(points, num_points); // for checking printf("Sum of lengths of vertical and horizontal lines = %d ", traceLines(points, num_points)); return 0; } // Read data for points and assign to arrays x and y int scanPoints(point_t pts[]) { int size, i; printf("Enter number of points: "); scanf("%d", &size); printf("Enter x- and y-coordinates of %d points: ", size); for (i=0; i Test Data: http://www.comp.nus.edu.sg/~cs1010/practice/2017s1/Practice-S12P01/testdata/
Objective: Sorting Task statement You are given a list of points on a 2-dimensional plane, each point represented by its x- and y- coordinates, both integers You are to sort the points in ascending order of their x-coordinates, and for those points with the same x-coordinate, sort them in ascending order of their y-coordinates. The points are stored in an array of point_t variables with the following structure definition: typedef struct t int x, y; ) point t You may assume that there are at most 20 points and no two points are identical Do the sorting by calling Selection Sort only once. How do you adapt the Selection Sort code given in the lecture for this problem? After sorting the points, imagine that you trace the points in their order in the sorted array Write a function traceLines) to compute the sum of lengths of those lines that are horizontal or vertical For example, suppose this is the list of 15 sorted points: (1, 2), (1, 3), (2, 1), (2, 4), (3, 2), (3, 3), (3, 4), (5, 3), (5, 6), (6, 2), (6, 5), (7, 2), (10, 4), (11, 4), (12, 2). The diagram below shows the tracing of the points. The vertical and horizontal lines are marked in green. The sum of lengths of horizontal and vertical lines is 13 4 1 2 3 45 6 78 9 10 11 12
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
