Question: The skeleton structure.c which contains a C program that implements a set of structures and functions to operate over these structures. Rather than working alone
The skeleton "structure.c" which contains a C program that implements a set of structures and functions to operate over these structures. Rather than working alone on this lab tutorial, you will work with one other student of this lab, no more and no less, to complete the following tasks:
1. Read the code together and try to explain it to each other.
2. The functions [EditLine(), ReverseLine(), and SortPointList() ]are not working as expected. Review them carefully and try to fix them (Hint call by reference)
3. Edit the function SortPointList() to give the user the option to sort the points based on either the X-coordinate or the Y-coordinate.
4. Add the appropriate comments before each function to improve code readability explain the function purpose and outcome.
#include#include #include /** * add description * */ typedef struct Point { int x; int y; } Point; /** * add description */ typedef struct Line { Point start; Point end; char color[15]; } Line; /** * add description * @param x * @param y * @return */ Point setPoint(int x, int y) { Point p; p.x = x; p.y = y; return p; } Line setLine(Point s, Point e, char lineColor[]) { Line l; l.start = s; l.end = e; strcpy(l.color, lineColor); return l; } /** * add description * * @param p */ void PrintPoint(Point p) { printf("(%d, %d) ", p.x, p.y); } /** * add description * * @param l */ void PrintLine(Line l) { printf("start at: "); PrintPoint(l.start); printf("end at: "); PrintPoint(l.end); printf("color is: %s ", l.color); } /** * add description * * @param l */ void EditLine(Line l) { printf("Please enter the new start point (x, y): "); scanf("%d %d", &l.start.x, &l.start.y); printf("Please enter the new end point (x, y): "); scanf("%d %d", &l.end.x, &l.end.y); printf("Please enter the line color: "); scanf(" %[^ ]s", l.color); } /** * add description * * @param l */ void ReverseLine(Line l) { Point temp; temp.x = l.start.x; temp.y = l.start.y; l.start.x = l.end.x; l.start.y = l.end.y; l.end.x = temp.x; l.end.y = temp.y; } /** * add description * * @param size * @return */ Point *CreatePointList(int size) { Point *ptr = (Point *) malloc(size * sizeof(Point)); return ptr; } /** * add description * @param list * @param size */ void InitializePointsList(Point *list, int size) { int x; int y; for (int i = 0; i < size; i++) { printf("enter data for point # %d: ", i); scanf("%d %d", &x, &y); *(list + i) = setPoint(x, y); } } /** * add description * * @param list * @param size */ void PrintPointList(Point *list, int size) { printf(" Here are the points stored in the list: "); for (int i = 0; i < size; i++) { PrintPoint(*(list + i)); } } void SortPointList(Point *list, int size){ Point temp; for(int i =0; i< size-1; i++){ for(int j = i+1; j (*(list+i)).x ){ temp = setPoint((*(list+i)).x, (*(list+i)).y); *(list+i) = setPoint((*(list+j)).x, (*(list+j)).y); *(list+j) = setPoint(temp.x, temp.y); } } } } int main() { // declare variable start of type Point and use the function setPoint(x,y) to set the x coordinate // and y coordinate of the point start Point start = setPoint(5, 7); // add description ........ Point end = setPoint(3, 9); // declare variable aline of type line and use the function setLine(start, end, color) to set the start point , end // point, and the color of the line Line aline = setLine(start, end, "red"); // call the function PrintLine(Line) that print the information of any variable of type Line printf(" Here is the current Line data: "); PrintLine(aline); // call the function EditLine(Line) which allow the user to change the information of any variable of type Line EditLine(aline); // call the function PrintLine(Line) that print the information of any variable of type Line // Note: after calling EditLine(aline) it is expected that PrintLine will display the new data of aLine printf(" Here is the new Line data: "); PrintLine(aline); // call the function ReverseLine(Line) this function will reverse the start and the end points on any given Line // variable (swap the start and end points) printf(" After reversing the start and end points of the line: "); ReverseLine(aline); // call the function PrintLine(Line) that print the information of any variable of type Line // Note: after calling EditLine(aline) it is expected that PrintLine will display the new data of aLine PrintLine(aline); int num; printf(" Please enter the total number of points you want to create: "); scanf("%d", &num); // Call the function CreatePointList(num) which allocate enough memory to store n number of points in an an array of // Point structures Point *pointlist = CreatePointList(num); // Call the function InitializePointsList(pointList, num) which allow the user to enter the data for each point in // the list of points InitializePointsList(pointlist, num); // Call the function PrintPointList(pointList,num) which will print all the points in the list. PrintPointList(pointlist, num); // Call SortPointList(pointList, num) that sort the list of points based on x or y SortPointList(pointlist, num); // Print the sorted point list PrintPointList(pointlist,num); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
