CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2006
    Posts
    1

    Exclamation Problem with sort algorithm

    I have a problem with the sorting algorithm which is at the edn of the code.
    Can someone help me !!!

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    #define MAX_STRING_SPACE        1000
    #define MAX_NUM_STRINGS         250
    #define MAX_STRING_SIZE         300
    
    int bmsearch(char *text,char *pat,int *no,int *pos)
    {
    	int i,table[256];
    	int len=strlen(pat);
    	*no=0;
    	int compcount=0;
    	for(i=0;i<256;i++)table[i]=len;
    	for(i=0;i<len;i++)table[pat[i]]=len-(i+1);
    	int ptct=len-1;
    	while(ptct<strlen(text))
    	{
    		int count=0;
    		while(count<len)
    		{
    			if(text[ptct-count]!=pat[len-1-count]){compcount++;break;}
    			else count++;
    		}
    		if(count==len)
    		{
    			(*no)++;
    			*(pos+(*no)-1)=(ptct-count+1);
    			ptct+=len;
    		}
    		else
    		{ptct+=(table[text[ptct-count]]-count);}
    	}
    	return compcount;
    }
    
    main()
    {
    	char text[300],pat[100];
    	int no,count,pos[20],i;
    	cout<<"\nEnter the text string:";
    	gets(text);
    	cout<<"Enter the searched pattern:";
    	gets(pat);
    	count=bmsearch(text,pat,&no,pos);
    	cout<<"\n\nPattern string occoured "<<no<<" times.";
    	cout<<"\n"<<count<<" comparisons required";
    	cout<<"\nPositions of occourence:\n";
    	for(i=0;i<no;i++)
    		cout<<pos[i]<<"\n";	
    	cout<<"\nThe index of the words"<<endl;
    	for (int j=0;j<300;j++)
    	{
    		if (j==0) cout<<j<<" - ";
    		if(text[j]==' '||text[j]=='.'||text[j]==','||text[j]=='!'&&(j+2)<strlen(text))
    			if (text[j+1]==' '||text[j+1]=='.'||text[j+1]==','||text[j]=='!'&&(j+2)<strlen(text))
    			{cout<<"\n"<<j+3<<" - ";j++;}
    			else 
    				cout<<"\n"<<j+2<<" - ";
    		else
    			if (j<strlen(text))
    				cout<<text[j];
    	}
    // Till here everything is ok. But i cannot make this sorting algorithm work	
            char string_space[MAX_STRING_SPACE];
            char *strings[MAX_NUM_STRINGS];
    
            /* Read the strings. */
            char *next_space = string_space;
            int inloc = 0;
            while(text) {
                    /* Find the length of the string and see if it fits. */
    			int length = strlen(text) + 1;
    			if(next_space + length >= string_space + MAX_STRING_SPACE)
    				break;
    			if(inloc >= MAX_NUM_STRINGS)
    				break;
    
    			/* Place the string into the structure. */
    			strings[inloc++] = next_space;
    			strcpy(next_space, text);
    			next_space += length;
    		}
    
    		cout<<"\n--------------------------------------------------\n";
    
    		/* Perform the sort.  Outer loop goes through destination of the minimum string. */
    		int strloc;
    		for(strloc = 0; strloc < inloc - 1; ++strloc) 
    		{
    			/* Scan the remaining strings for ones smaller. */
    			int scan;
    			for(scan = strloc + 1; scan < inloc; ++scan) 
    			{
    				if(strcmp(strings[strloc], strings[scan]) > 0) 
    				{
    					/* Exchange the strings. */
    					char *tmp = strings[strloc];
    					strings[strloc] = strings[scan];
    					strings[scan] = tmp;
    				}
    			}
    		}
    
    		/* Print 'em. */
    		for(strloc = 0; strloc < inloc; ++strloc) 
    		{
    			cout<<strings[strloc]<<endl;
    		}
    	
    	
    
    	   return 0;
    }
    *Edit* : Added code tag
    Last edited by JeffB; February 23rd, 2006 at 01:15 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with sort algorithm

    1) Please use code tags when posting code. Your code is unreadable

    2) This is not a compiler bug, it is a bug in your program. This forum concerns bugs in the compiler. Please post this in the Non-Visual C++ forum.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Problem with sort algorithm

    I've moved this to the proper forum

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  4. #4
    Join Date
    Jan 2001
    Posts
    588

    Re: Problem with sort algorithm

    Since you're including <iostream> and <string> (even though you didn't use the string class at all in your example), I'm assuming you are working in C++ and have a standard library. In this case, you should use std::string and std::sort to do what you're trying to do. Don't reinvent the wheel.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with sort algorithm

    Quote Originally Posted by MM4o
    I have a problem with the sorting algorithm which is at the end of the code.
    To add to what Bob has stated, what I highlighted is a major part of the problem -- why is it in main() to begin with? Why isn't it a separate function, so it can be easily tested with known input?

    First, write it as a separate function. Then test the function with hard-coded input (don't read from a file or keyboard, and don't fool around with a lot of that other stuff you wrote). Just call the function with known values, and test it. Then debug why it doesn't work with known input values. Once you have it working, then you move the fixed function in with the rest of your application.

    That is the way you are supposed to develop code, and that is to write each module and test each one, not write everything in main() and not know why a piece of code in the middle of a 100 line main() program doesn't work.
    For example:
    Code:
    void SortMyStuff( /* appropriate parameters */ )
    {
       // All this does is attempt to sort, according to your criteria, the input
       // that is passed in. 
    }
    
    int main()
    {
         char *TestInput[] = (" Test1 ", "Test2", "Test3");
         SortMyStuff( TestInput,  /*other parameters*/ );
    
         // output the results to see if it worked.
    }
    Something like that.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured