Thread: HELP - with my sort

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

    HELP - with my sort

    Im having trouble sorting the name array in my bubble sort. The sort works fine with just the account number and balance, but when i add the name into it it doesnt work. Im sure it has something to do with the fact that name is an array, but i dont know how to fix this problem. Any help would be greatly appreciated.


    Code:
    #include <stdio.h>
    
    main()
    {	
    /*Variables*/
    /*-------------------------------------------------------------------------------*/
    	struct info{char name[30]; int account_num; float balance;
    	 };
    	struct info people[25];
    	int x,a,y,temp=0;
    	
     /*------------------------------------------------------------------------------*/
     	
    /*Heading*/
    /*-------------------------------------------------------------------------------*
    
    
    //*Loop to collect info*/
    
    
    		for(x = 1; x <= 3; ++x)
    		{	 
         		     printf("Enter Name: ");
    		     scanf("%s",people[x].name);
    	             
                         printf("Enter Account Number: ");			 
    		     scanf("%i",&people[x].account_num);
    		     
                         printf("Enter Balance: ");		 
    		     scanf("%f",&people[x].balance);			 
    			 
    		     printf("\n");
    		}
    	
    		for (a=1; a<x-1;++a)
    		{		
    		for (x=1; x<3;++x)
    		{
    		if (people[x+1].account_num>people[x].account_num)
    		{
    			temp = people[x].account_num;
    		 	people[x].account_num = people[x+1].account_num;
                           people[x+1].account_num = temp;					
    			    
    			temp = people[x].name;
    			people[x].name = people[x+1].name;
                           people[x+1].name = temp;
    					 
    				
    			temp = people[x].balance;
    			people[x].balance = people[x+1].balance;
                            people[x+1].balance = temp;
                 
    		}/*end if*/
    		}/*end nested for*/
    		}/*end for loop*/
    
    	/*Loop to print Sorted grades*/
    		for (y=1; y<=3;++y)
    		{	
    		printf("%i %s %.2f \n, ",people[y].account_num,  people[y].name,                           people[y].balance);	
    		}	/*end for*/
    				
    	printf("\n\n");
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, you have to sort strings character by character. It will probably be easier (for everyone) if you write a separate program as an experiment to figure out how to do that, since the stuff that you know works is just clutter.

    Eg, take two different strings:
    Code:
    char one[]="this is a test", two[]="this is too";
    Now try and write a function of this sort:
    Code:
    int alphasort(char *one, char *two);
    That returns one if one should be before two, alphabetically, and zero if it should not. The function will fit perfectly into a bubble sort implimentation.

    Hint: If you don't know about C character ASCII values, look into it. Individual characters already have numerical values that are easily compared ('a' < 'b', etc.)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    15
    Im not trying to sort the characters individually. Is that what you are saying I should do? I just want the name to stay with all of the other info (account_num and balance). Sorry new to C. Do i still need to sort each character individually, since im not sort the characters at all?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by wwwildbill View Post
    Im not trying to sort the characters individually. Is that what you are saying I should do? I just want the name to stay with all of the other info (account_num and balance). Sorry new to C. Do i still need to sort each character individually, since im not sort the characters at all?
    Sorry, I misunderstood you. The real problem is you need to use strcpy(), not an equal sign, to create or change the contents of a string. That also means you need a temporary character array to use in place of "temp" when exchanging the strings, since temp is a single int and you cannot strcpy into it.

    For future reference: this is kind of what I meant by the "clutter" problem; you will probably receive quicker and more helpful advice if you can focus more on your actual problem -- this has absolutely nothing to do with sorting. But to be fair, if you are this new to C, that might be hard to see...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by MK27 View Post
    Sorry, I misunderstood you. The real problem is you need to use strcpy(), not an equal sign, to create or change the contents of a string. That also means you need a temporary character array to use in place of "temp" when exchanging the strings, since temp is a single int and you cannot strcpy into it.
    No he doesn't need to use strcpy at all. The entire struct can already be copied as desired using default assignment. All that is needed is to take advantage of that fact rather than trying to swap the pieces of the struct individually. This is all you need to write for that swap in this case:
    Code:
    info temp = people[x];
    people[x] = people[x+1];
    people[x+1] = temp;
    I would also discourage the opposite of what MK27 did. Posting slightly more code than you want others to focus on allows for better help - we don't get a peephole view. Sure, don't post too much, but this wasn't anyway. More than about 1.5 screen heights is often when it starts getting too much.
    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"

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    No he doesn't need to use strcpy at all. The entire struct can already be copied as desired using default assignment.
    Yep. In that case "temp" should be a struct pointer, as per iMalc's example.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    15
    Thanks guys

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Not a pointer, just an instance of a struct.

    Note that the previous code was also losing resolution by swapping the float using an int.
    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"

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    Not a pointer, just an instance of a struct.
    A ptr will work fine since there is no destruction going on, and given that the two structs being switched were previously allocated.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by MK27 View Post
    A ptr will work fine since there is no destruction going on, and given that the two structs being switched were previously allocated.
    I don't know what you're thinking, but it doesn't make sense. First off I wondered why you thought my example contained a pointer when it doesn't, and now you appear to be suggesting that taking a pointer to those items gets you anywhere towards swapping the entire contents of the two structs. You must be confusing a pointer with something, that's all I can gather. There's nothing to do with construction or destruction here either, it's plain C.

    In any case, I've posted code replaces the entire body of the if-statement, and indications are that the OP has done this and now has working code.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM