sorting function and while loop.

This is a discussion on sorting function and while loop. within the C Programming forums, part of the General Programming Boards category; Hello, I am having trouble with my sorting function. I am trying to sort 5 strings. After I input all ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    sorting function and while loop.

    Hello,

    I am having trouble with my sorting function. I am trying to sort 5 strings. After I input all 5 strings, the program hangs. When I comment out the while loop, it prints the five strings(though not sorted), so the problem is with the while loop. I can't seem to find what I am doing wrong. Any suggestions?
    Code:
    void string_sort(char * strings[], int num)
    {
    	char *temp;
    	int i, j;
    	int sorted = 0;
    	
    	while(sorted!=1)
    	{
    		sorted = 1;
    		for(i=0; i<num-1; i++)
    			for(j=1; j<num; j++)
    				{
    				if(strcmp(strings[i], strings[j]) > 0)
    					{
    					temp = strings[i];
    					strings[i] = strings[j];
    					strings[j] = temp;
    					sorted = 0;
    					}
    				}
    	}
    }

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    use strcpy() for copying strings.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by swappo View Post
    Hello,

    I am having trouble with my sorting function. I am trying to sort 5 strings. After I input all 5 strings, the program hangs. When I comment out the while loop, it prints the five strings(though not sorted), so the problem is with the while loop. I can't seem to find what I am doing wrong. Any suggestions?
    Code:
    void string_sort(char * strings[], int num)
    {
    	char *temp;
    	int i, j;
    	int sorted = 0;
    	
    	while(sorted!=1)
    	{
    		sorted = 1;
    		for(i=0; i<num-1; i++) {
    			for(j=1; j<num; j++)
    				{
    				if(strcmp(strings[i], strings[j]) > 0)
    					{
    					temp = strings[i];
    					strings[i] = strings[j];
    					strings[j] = temp;
    					sorted = 0;
    					}
    				}
                   }
    	}
    }
    The outermost for loop is mssing its opening / closing parenthesis.
    Is strings[] a 5-numbered array of pointers to strings.

  4. #4
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    The while loop doesn't make any sense to me.
    edit:
    @itcbitc
    I think that's not an error but good practice though to use the brackets.
    Last edited by stevesmithx; 11-26-2008 at 11:37 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    The purpose of the while loop is to keep looping through the for loops until the list is completely sorted. With out the while loop, I would expect the program to terminate after going through the for loops leaving the array of strings partially sorted. When I comment out the while loop, I get partially sorted strings.

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    I couldn't understand how the while loop is being used in this context.
    Could you post your complete code please?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you post the code that fills in the strings and calls the sort function? I suspect stevesmithx is on the right track, but it's hard to say without knowing exactly what the strings look like and how they are stored.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    The string_sort function looks okay to me, actually. I suggest that you post the smallest and simplest compilable code that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Here is the whole program.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    void string_sort(char * strings[], int num);
    
    int main(void)
    {
    	char input[5][100]={'\0'};
    	char ch;
    	char * ptstr[5];
    	int i, j, k;
    	
    	printf("Enter 5 strings\n");
    	for(i=0; i<5; i++)
    		for(j=0; (ch=getchar())!='\n'; j++)
    			{
    			input[i][j]=ch;
    			ptstr[i]=input[i];
    			}
    	printf("\n");
    	string_sort(ptstr, i);
    
    	for(i=0; i<5; i++)
    		{
    		for(j=0; j<100; j++)
    			printf("%c", ptstr[i][j]);
    		printf("\n");
    		}
    	return 0;
    }
    void string_sort(char * strings[], int num)
    {
    	char *temp;
    	int i, j;
    	int sorted = 0;
    	
    	while(sorted!=1)
    	{
    		sorted = 1;
    		for(i=0; i<num-1; i++)
    			{
    			for(j=1; j<num; j++)
    				{
    				if(strcmp(strings[i], strings[j]) > 0)
    					{
    					temp = strings[i];
    					strings[i] = strings[j];
    					strings[j] = temp;
    					sorted = 0;
    					}
    				}
    			}
    	}
    }

  10. #10
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    use strcpy() instead of assigning strings.
    [Didn't you read my first post]
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Considering that you do not even pass a char *string[] to the code, I'd say you're very lucky to not crash...

    You need to pass your strings as a 2D array of char, string[][100], and use strcpy() to move the content around.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by matsp
    You need to pass your strings as a 2D array of char, string[][100], and use strcpy() to move the content around.
    Alternatively, you can actually have an array of pointers to char, e.g., with dynamic memory allocation for each string. The crux of the problem is that your function is designed to work with an array (or rather a pointer to the first element of an array) of pointers, but what you have is a two dimensional array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    [QUOTEuse strcpy() instead of assigning strings.
    [Didn't you read my first post] ][/QUOTE]
    Yes I did read your post and I was planning on trying strcpy() next. First, I wanted to see if I can get it to work the way it is so far.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swappo View Post
    [QUOTEuse strcpy() instead of assigning strings.
    [Didn't you read my first post] ]
    Yes I did read your post and I was planning on trying strcpy() next. First, I wanted to see if I can get it to work the way it is so far.[/QUOTE]

    No, you can't because what you are doing to the variable when you are passing it to the function is wrong.

    You can prove that by printing the strings inside the sort function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    I agree with mats and laserlight.
    You can keep it simple if you use two dimensional arrays throughout.
    Also you can use functions like scanf/fgets to get the string from stdin more easily.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

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

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