GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Feb-2006, 09:08
MM4o MM4o is offline
New Member
 
Join Date: Feb 2006
Posts: 2
MM4o is on a distinguished road
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:
CPP / C++ / C 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;
}
  #2  
Old 24-Feb-2006, 08:55
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Problem with sorting algorithm


With a quick look, it appears that your while loop is, at least at first, killing you. It is not breaking up the string into words as expected, but keeps appending the entire string to the array.

If you want a possibly easier way to at least break up the words (but they will not be sorted, you will still need to do that) take a look at the strtok() function example in the MSDN.
  #3  
Old 24-Feb-2006, 10:21
MM4o MM4o is offline
New Member
 
Join Date: Feb 2006
Posts: 2
MM4o is on a distinguished road

Re: Problem with sorting algorithm


Thanks very much TurboPT. I think that will solve my problem.
  #4  
Old 24-Feb-2006, 10:40
davis
 
Posts: n/a

Re: Problem with sorting algorithm


Quote:
Originally Posted by MM4o
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.

I hope that I can say a few things about your code without offending you. There are a number of "style" issues that you should really correct with your programming style, IMO.

Somewhere around 2200 years ago, whitespace was added to make sentences clearer and easier to read. It is a good tradition and I recommend it both for writing communications and coding. Programmers who tend to jam everything together in statements are not using whitespace for its intended purpose: Make code clearer and easier to read.

CPP / C++ / C Code:
#include <string>
#include <iostream>
using namespace std;

#define MAX_STRING_SPACE        1000
#define MAX_NUM_STRINGS         250
#define MAX_STRING_SIZE         300

This isn't C++, it is C with some C++ features being used and mixed with C. By choosing to use C++ you can greatly reduce your programming difficulty.

CPP / C++ / C Code:
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) )

...use a space after every comma. Also, the preferred pointer syntax in C++ is for the asterisk to cuddle the type name:

CPP / C++ / C Code:
int bmsearch(char* text, char* pat, int* no, int* pos)

In C, the convention is to cuddle the variable name. Whichever you choose, do it consistently and with a space after each comma. Okay?

Try to declare variables one per line. Multiple variables on the same line are more difficult to maintain over time.

CPP / C++ / C Code:
{
    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) )

I recommend separating your variable declarations from your for conditional blocks. It makes code easier to work on and debug by allowing you to easily comment out a block by adding /* and */ in the blank line locations.

Do you realize that you don't have a single comment in "bmsearch" ...not even one that describes what bmsearch is supposed to do?

Your variable names are not very expressive. Granted, most of us can figure out what "len" is, but "ptct" and "compcount" ...what are they? If you are asking for help from other programmers, then you help them first by making your code readable from the outset. It won't kill you to spell out whatever ptct is and why it should be initialized to 1 less than length.

Another really good habbit to get into is to always use braces around conditional blocks. Here is an example:

CPP / C++ / C Code:

// BAD    for( i=0;i<256;i++ )table[i]=len;
// BAD    for( i=0;i<len;i++ )table[pat[i]]=len-(i+1);

    for( i=0; i < 256; i++ )
    {
        table[i] = len;
    }
    for( i=0; i < len; i++ )
    {
         table[pat[i]] = len - (i+1);
    }

...not only does it make the code more readable, it is much easier to add something inside of it to check the state of a variable. Also, if something needs to be added to the code, multi-line blocks are already "ready" for it.

CPP / C++ / C Code:
ptct+=(table[text[ptct-count]]-count);

...lines like this are difficult to read and understand from a glance.
CPP / C++ / C Code:
ptct += (table[ text[ptct-count] ] - count);

...is a little better, isn't it? Ideally, something that reduces the complexity of code should be considered.

There are a lot of less than ideal choices made in your C-code. For example, you're using cout to output and gets to input? Why not simply use cin?

Another example of not-very-good-design is that you're doing a lot of your "algorithm" work inside of your main (which should be declared to return an int).

I don't know who your professor is, but I'm guessing that after reaching tenure reading this level of code, he probably is immune to it.

Here's a good idea:

Separate your work into discrete functions and do the work inside of the functions. Make each function that you use perform a simple task so that the complexity of the code is greatly reduced. Use as many functions as you need to break down the job into small "bite-sized" bits of work. Worry less about looping a lot and coding inefficiencies and worry more about making each little function easy to read and understand what happens. C is a low-level language. You have to build little modules of functionality to accomplish larger jobs. Trying to do too much in one function complicates the coding and the debugging. Chain the work together inside of main so that main initializes the variables and then, as needed, passes them in to the functions that perform work on the variables. Create your functions based on the variables that they need in order to do the work.


If you can succeed at doing what I just wrote, your programs will greatly improve and others will be much more inclined to help out when you have a problem. Also, you can point them to a nice, composite little function and say "this isn't working" and it will be clearer and easier to understand where you are in the work-chain.

Unless you expect the user to input a very large amount of text, or eventually read some file, then processing power is probably not a major issue. Breaking down the work will simplify your life, too.


:davis:
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03
String sorting problem Lost Boy C Programming Language 5 10-Apr-2005 19:49
STL (algorithm): for_each problem yosep C++ Forum 1 03-Nov-2003 12:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.