DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2006
    Posts
    1

    Exclamation Problem with sorting algorithm

    I had this assingment from my profesor in COS:
    The program has to scan an input text, select every word (separated with white space or a punctuation characters); tofind all occurrences of a particular word(address of the word is the number of its first character); to create the index( word with attached list of addresses); and to sort words (with index alphabetically.
    So far i have managed to solve every problem exept the sorting of words. Can someone help me fixing the bugs in the sorting algorithm.
    Here is the code:
    PHP 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==0cout<<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_spacetext);
                
    next_space += length;
            }

            
    cout<<"\n--------------------------------------------------\n";

            
    /* Perform the sort.  Outer loop goes through destination of the minimum string. */
            
    int strloc;
            for(
    strloc 0strloc inloc 1; ++strloc
            {
                
    /* Scan the remaining strings for ones smaller. */
                
    int scan;
                for(
    scan strloc 1scan 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 0strloc inloc; ++strloc
            {
                
    cout<<strings[strloc]<<endl;
            }
        
        

           return 
    0;


  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    I think you need to use strcpy not assignment operations. Also, tmp is assigned the address of existing stuff not a true place holder -- that *looks* like a bug but I did not check it deeply.

Similar Threads

  1. a very weird problem (AGAIN!!!)
    By Michael in forum ASP.NET
    Replies: 14
    Last Post: 02-24-2006, 08:54 AM
  2. Josephus Problem, problem.
    By aesoprock00 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 06:18 PM
  3. heap sorting problem
    By java? in forum Java
    Replies: 1
    Last Post: 11-28-2005, 08:23 PM
  4. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 06:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links