Thread: Data Structures

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Data Structures

    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;
    }

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Well, you're only changing the x values of the structures... And not the y values.
    Try making hold a struct Points and then swap the structures. Kind of like this...
    Code:
    hold = ptA[item];
    ptA[item] = ptA[i];
    ptA[i] = hold;
    You can use = assignment with structs of same type, but not == comparison.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Rouss
    You can use = assignment with structs of same type
    Yes, you can. But remember, if your structure contains pointers, all it does is assign one pointer to the other. It won't duplicate anything you have allocated for said pointer. Thus, if you're trying to clone a structure, you won't really make a copy of data pointed to, rather just another "link" to the same object.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's a good generalized data structures book?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-16-2006, 01:01 PM
  2. i need advice about data structures
    By sawer in forum C Programming
    Replies: 2
    Last Post: 04-22-2006, 03:40 AM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. array of structures, data from file
    By nomi in forum C Programming
    Replies: 5
    Last Post: 01-11-2004, 01:42 PM
  5. Array Data Structures
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 06:52 PM