Thread: Alphabetical sorting function

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    Alphabetical sorting function

    This is a function i've been working on but can't seem to get it to work (very new @ c). What it's suppose to do is read the word i enter, determine where it's suppose to go in the array and insert it accordingly. That means when all the words have been inserted into the (2 dimensional) array, they should be in alphabetical order.

    I got the program to work with integers, and i'm in the process of converting it, so i t can sort words instead. However now that it deals with 2 dimensional arrays, i'm on new grounds and aren't very sure of myself.

    This is what it should do;

    The first function determines wether the word i entered is 'smaller' or 'bigger' than the words in the current array. It doesn't this by comparing it to all the words that is in the array. If it finds that the current word is the 'largest' among words, it sticks it at the end, if not, it returns the element address, and that is the 'insertion point' (where to insert the new word).

    The second function, uses the insertion point, and shifts the array accordingly, so that the new word is in the rightful place in the array. I've looked over this function, but i don't understand it as well as the first.

    The main function is pretty easy to understand i think.

    Thanks for the help.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_WORDS (10)
    #define MAX_CHARS (100)
    
    int insertionPoint(char inputs[], char value, int currentLength)
    {
    	int i;
    	for(i = 0; i < currentLength; i++)
    	{
    		if( strcmp(value, inputs[i]) == -1 )
    			return i;		
    		
    	return currentLength;
    }
    
    void moveup(char inputs[], int currentLength, int startPoint)
    {
    	int i;
    	int numberOfElementsToMove;
    	numberOfElementsToMove = currentLength - startPoint; // iM - iI (i from main loop - i returned from insertIndex)
    	for(i = 0; i < numberOfElementsToMove; i++)
    	{
    		int index = currentLength - i;
    		inputs[index] = inputs[index-1];
    	}
    }
    
    int main()
    {
    	char inputs[MAX_WORDS][MAX_CHARS];
    	int i;
    	
    	for(i = 0; i < MAX_WORDS; i++)
    	{
    		char temp[100];
    		int insertIndex;
    		scanf("%s", temp);
    
    		insertIndex = insertionPoint(inputs,temp,i);
    		moveup(inputs,i,insertIndex);
    		strcpy( inputs[insertIndex], temp );
    	}
    	
    	return 0;
    }
    I'm working on it now at the moment also, if i manage to fix anything, i will post it.
    Last edited by typer; 05-20-2006 at 12:19 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int insertionPoint(char inputs[], char value, int currentLength)
    To pass the array you have in main, you would need

    int insertionPoint(char inputs[][MAX_CHARS], char value, int currentLength)

    ditto for your moveup function.

    Also in moveup
    inputs[index] = inputs[index-1];
    Use strcpy() to copy the string from one index to another.

    > strcmp(value, input[i]) == -1
    Use < 0, not == -1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    7
    There still seems to be a few errors i can't pick out. I don't even understand what half of them are on about. Non-void? Lacks a cast? startPoint undeclared?

    (how the hell can startPoint be undeclared when it is? What the hell? @__@, *pulls hair out*)

    I've tried;

    if( strcmp( char value, const char inputs[i]) < 0 )

    but errors still come out on the same line. The cast thing disapears though.

    These are the errors:
    g++.exe -x c++ -c J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c -o J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.o -Wall -fpermissive
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c: In function `int insertionPoint(char (*)[100], char, int)':
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:11: warning: passing `char' to argument 1 of `strcmp(const char *, const char *)' lacks a cast
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:18: parse error before `{'
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:21: `startPoint' undeclared (first use this function)
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:21: (Each undeclared identifier is reported only once
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:21: for each function it appears in.)
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:27: warning: control reaches end of non-void function `insertionPoint(char (*)[100], char, int)'
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c: In function `int main()':
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:40: warning: passing `char *' to argument 2 of `insertionPoint(char (*)[100], char, int)' lacks a cast
    J:\Work\UNISTU~1\SEMEST~1\HES1300\COMPUT~1\GCC\ASS IGN~1\mooza.c:41: warning: implicit declaration of function `int moveup(...)'
    Failure


    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_WORDS (10)
    #define MAX_CHARS (100)
    
    int insertionPoint(char inputs[][MAX_CHARS], char value, int currentLength)
    {
    	int i;
    	for(i = 0; i < currentLength; i++)
    	{
    		if( strcmp( value, inputs[i] ) < 0 ) // line 11
    			return i;		
    		
    	return currentLength;
    }
    
    void moveup(char inputs[][MAX_CHARS], int currentLength, int startPoint)
    { // line 18
    	int i;
    	int numberOfElementsToMove;
    	numberOfElementsToMove = currentLength - startPoint; // line 21
    	for(i = 0; i < numberOfElementsToMove; i++) // Loop shifts stored words up(?)
    	{
    		int index = currentLength - i;
    		strcmp( inputs[index], inputs[index-1] );
    	}
    } // line 27
    
    int main()
    {
    	char inputs[MAX_WORDS][MAX_CHARS];
    	int i;
    	
    	for(i = 0; i < MAX_WORDS; i++)
    	{
    		char temp[100];
    		int insertIndex;
    		scanf("%s", temp);
    
    		insertIndex = insertionPoint( inputs, temp, i ); // line 40
    		moveup( inputs, i, insertIndex ); // line 41
    		strcpy( inputs[insertIndex], temp );
    	}
    	
    	return 0;
    }
    Last edited by typer; 05-20-2006 at 01:00 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh you mean there's more....

    Well value needs to be a char array as well

    And your insertpoint function is missing a closing brace for the for loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    7
    Oh god it works...Thanks a trillion. Now the degrading of my eyes for staring at this code for 5 hours won't go to waste.

    --- edit ---

    This isn't my day ;(

    The printArray function doesn't cause any errors, but it won't work for some odd reason. I'm sure it should ;(

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_WORDS (10)
    #define MAX_CHARS (100)
    
    void printArray( const char a[][MAX_CHARS] )
    {
    	int i;
    	int j;
    	
    	for( i = 0; i <= MAX_WORDS; i++)
    	{
    		for( j = 0; j <= MAX_CHARS; j++)
    		{
    			printf("%s", a[i][j]);
    		}
    		
    	printf("\n");
    	}
    }
    
    int insertionPoint(char inputs[][MAX_CHARS], char value[MAX_CHARS], int currentLength)
    {
    	int i;
    	for(i = 0; i < currentLength; i++)
    	{
    		if( strcmp( value, inputs[i] ) < 0 )
    			return i;		
    	}
    	return currentLength;
    }
    
    void moveup(char inputs[][MAX_CHARS], int currentLength, int startPoint)
    { // line 18
    	int i;
    	int numberOfElementsToMove;
    	numberOfElementsToMove = currentLength - startPoint;
    	for(i = 0; i < numberOfElementsToMove; i++) // Loop shifts stored words up(?)
    	{
    		int index = currentLength - i;
    		strcmp( inputs[index], inputs[index-1] );
    	}
    }
    
    int main()
    {
    	char inputs[MAX_WORDS][MAX_CHARS];
    	int i;
    	
    	for(i = 0; i < MAX_WORDS; i++)
    	{
    		char temp[100];
    		int insertIndex;
    		scanf("%s", temp);
    
    		insertIndex = insertionPoint( inputs, temp, i );
    		moveup( inputs, i, insertIndex );
    		strcpy( inputs[insertIndex], temp );
    	}
    	
    	printf("\n");
    	
    	printArray( inputs );
    	
    	return 0;
    }
    Last edited by typer; 05-20-2006 at 02:04 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for( i = 0; i <= MAX_WORDS; i++)
    Use the same for loop as you do in main
    This one steps off the end of the array.

    > printf("%s", a[i][j]);
    The array reference is a single char, so the correct format is %c

    But the easier way is simply
    Code:
    for(i = 0; i < MAX_WORDS; i++) {
      printf( "%s\n", a[i] );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    7
    The printArray thingy i used comes out with a weird result. So i used yours instead, works perfect. Thanks, now i can finally move on with my assignment.

    Oh, this was a boo boo < strcmp( inputs[index], inputs[index-1] ); >, was suppose to be strcpy

    Thanks C god.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM