I want to use data structures to create like a 2D array (x,y) and then sort them by looking at x. The code is below but when I sort it, it sorts only the x's.. the corresponding y value doesn't stick. I know I am CHANGING the value of x's so there's no way y would also rearrange. How do I change this so that the both the x and y are sorted. Can I swap pointer locations like a pointer to the structure which will maintain the coressponding y values with x?
Code:
#include <stdio.h>
#include <string.h>
int main() {
int i, item, hold;
struct Points {
int x;
int y;
};
struct Points ptA[4];
for (i=0; i<4; i++) {
fflush(stdin);
printf("Enter #%d point: ", i+1);
scanf("(%d,%d)", &ptA[i].x, &ptA[i].y);
}
printf("\n");
for (item=0; item<4-1; ++item)
for (i=item+1; i<4; ++i) {
if (ptA[i].x<ptA[item].x) {
hold = ptA[item].x;
ptA[item].x=ptA[i].x;
ptA[i].x=hold;
}
}
printf("sorted:\n");
for (i=0; i<4; i++)
printf("(%d,%d)\n", ptA[i].x, ptA[i].y);
printf("\n");
return 0;
}