sorting an array of structures

This is a discussion on sorting an array of structures within the C Programming forums, part of the General Programming Boards category; *** THE TITLE SHOULD SAY SORTING A STRUCTURE NOT AN ARRAY!!! *** ok this is the last part i need ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    sorting an array of structures

    ***THE TITLE SHOULD SAY SORTING A STRUCTURE NOT AN ARRAY!!!***


    ok this is the last part i need finished for this program and its causing quite a headache.

    basically im storing data in a structure and it needs to be sorted. i have figured out the general outline from information ive found on here and what ive been able to work on.

    when i compile the code i am getting these errors:

    Code:
    64: parse error before `temp'
    66: incompatible types in assignment
    and of course here is the code for the program:

    Code:
    #include <stdio.h>
    
    /* Define structure */
    struct info
    {
    	long int accntNum;
    	char name[25];
    	float balance;
    };
    
    /* Begin main */
    main()
    {
    	/* Declare variables */
    	struct info client[25];
    	int temp = 0;
    	int x, y, numClient;
    	
    	/* Display opening message */
    	printf ("Welcome to Client Account Information Services\n\n");
    	
    	/* Prompt for how many clients will have info entered */
    	printf ("Enter number of clients to be used: ");
    	scanf ("%i", &numClient);
    	
    	/* Prompt for user to enter each clients info */
    	for (x = 0; x < numClient; x++)
    	{
    		printf ("\nEnter account number: ", x + 1);
    		scanf ("%ld", &client[x].accntNum);
    		
    		fflush(stdin); /* Remove extraneous characters */
    		printf ("Enter last name: ", x + 1);
    		gets (client[x].name);
    		fflush(stdin); /* Remove extraneous characters */
    		
    		printf ("Enter balance: ", x + 1);
    		scanf ("%f", &client[x].balance);
    	} /* end for */
    	
    	/* Sort array in ascending order by account number */
    	for (y = 1; y < x - 1; y++)
    	{
    		for (x = 1; x < numClient; x++)
    		{
    			if (client[x].accntNum > client[x + 1].accntNum)
    			{
    				info temp = client[x];
    				client[x] = client[x + 1];
    				client[x + 1] =  temp;
    			} /* end if */
    		} /* end for */
    	} /* end for */
    
    	/* Display each clients information */
    	printf ("\nACCOUNT         LAST NAME         BALANCE");
    	for (x = 0; x < numClient; x++)
    	{
    		printf ("\n%7d %17s %15.2f", client[x].accntNum, client[x].name, client[x].balance);
    	} /* end for */
    	
    	printf ("\n");
    	return 0;
    	
    } /* end main */
    basically just need to figure out what is not correct when i am calling the 'info temp = client[x]; line or just before it.

    all help is appreciated!

    (i also realize this similar question has been asked recently. i have viewed those posts but it has only got me stuck at this point. i have a feeling its something simple i am not noticing, but sometimes we all need a fresh set of eyes to look at things)

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i dont know much about structures but just do this
    Code:
    struct info temp=client[x];
    it should work fine

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As stated above, that makes everything simpler.

    You'll need to start your sorting at subscript 0, not 1.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    hmm ok now it compiles, but its not sorting.

    any ideas?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    basically its not sorting anything and my third line is coming up with garbage information.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    when i ran the same code its getting sorted according to the account number.ok now i see its working fine for 2 accounts.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    its acting kind of strange now for me. it just worked with 5 accounts, but wont work for 2, 3, or 4 accounts.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    yep just ran 2 accounts, and its not sorting for me. hmmm

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    ahhhhhh got it fixed now!

    thanks for all the help!

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by kisiellll View Post
    ahhhhhh got it fixed now!

    thanks for all the help!
    what did you do.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not sure of your sorting algo. The x+1 looks quite wrong. You should be comparing x with y subscripts, in the array. Array[x+1] will be 1 subscript too high when x is at the top of the array (one less than max Array Size).

    This is a known good sorter for small arrays.

    Code:
    struct client temp;
    
    for(i = 0; i < MaxArray-1; i++)  {
       for(j = i + 1; j < MaxArray; j++)  {
          if(client[i].accnum > client[j].accnum)  {
             temp = client[i];
            client[i] = client[j];
            client[j] = temp;
          }
       }
    }
    Last edited by Adak; 04-04-2009 at 10:55 PM.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    ok well i actually never fixed it. i modified some code somewhat like Adak just posted and it looked workable somewhat, but i was still getting junk.

    i just used Adaks example and it seems to be good so far.

    thanks adak

  13. #13
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    by the way never use gets.see the faq section of this site.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your quite welcome. I've been using that code for sorting for a very long time.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by Adak View Post
    I'm not sure of your sorting algo. The x+1 looks quite wrong. You should be comparing x with y subscripts, in the array.
    No. Never say that someone should use that sorting algorithm instead of BubbleSort. BubbleSort is better, largely because it is stable whereas that sorting algorithm is not. [x+1] or [x-1] is simply how you compare an item adjacent to item x.

    The problem was that this
    Code:
    		for (x = 1; x < numClient; x++)
    should have been
    Code:
    		for (x = 0; x < numClient-1; x++)
    .Either that or it should have been [x-1]
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  2. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  3. trying to initialize an array of structures
    By dreamgoat in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 05:33 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21