Click to See Complete Forum and Search --> : Problem with sort algorithm


MM4o
February 22nd, 2006, 08:56 AM
I have a problem with the sorting algorithm which is at the edn of the code.
Can someone help me !!!

#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

Paul McKenzie
February 23rd, 2006, 11:30 AM
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

JeffB
February 23rd, 2006, 12:15 PM
I've moved this to the proper forum

JeffB

Bob Davis
February 23rd, 2006, 01:34 PM
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.

Paul McKenzie
February 23rd, 2006, 02:08 PM
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:

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