Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need with arrays

Status
Not open for further replies.

DonFredi

MIS
Dec 2, 2005
4
0
0
US
Hi all!
I am trying to implement the Boyer Moore Horspool algorithm and having trouble with the following function:

void shiftTable(char* pat, int num){
int i;
num= strlen(pat);
for(i=0;i<10;i++)
table=num;
for(int j=0; j<pNum; j++)
table[pat[j]]=num-(1+j);// int table[10]
}
what I am trying is to put num-1-j into table using the position of pat[j] as index i.e, for pat="BOY", table[0] should have 2(num-1-j);

Any help would be appreciated. Thanks in advance
 
DonFredi,

Just some observations:
1. table[] looks to be defined somewhere else as an int array of size 10.
2. the term [pat[j]] may be greater than 10. This will cause trouble because you will have overstepped the boundaries of table[].
3. pat[] should be defined somewhere that it is sized to pNum or greater.
4. pat[] should be limited to just one character. If it is a string table[pat[j]] will cause an error.

What, if any, errors are you getting and are they compile or run time errors?


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Thanks HyperEngineer!!!
Corrections: pattern always contains num < 10;
int table[10];
num is the number of char in pattern

void shiftTable(char* pattern, int num){
int i;
num= strlen(pattern);
for(i=0;i<10;i++)
table=num;
for(int j=0; j<num; j++)
table[pattern[j]]=num-(1+j);
}
This code compiles fine, except the program hangs, when that function is called. Hope that shed some light
 
Did you step through the function in the debugger 1 line at a time to see where it hangs?
 
yea, it at table[pattern[j]]= num-1-j.
I think the problem is that pattern[j] return an index of type char and table is of type int.
Any suggestions on other ways of accomplishing this?
 
What chars does pattern contain? If any of those chars have an ascii value higher than 9 you'll be stepping outside the boundaries of the table array.
 
Also, why do you pass num to the function if it immediately gets set to something else by strlen()?
 
DonFredie,

Your problem is table[pattern[j]]. That's because pattern[j] is a string not a char that can be converted to an integer. If you used just the first char of the string then you will have the problem cpjust pointed out. If the char has an integer value of greater than 10, then you will overstep the bounds of table[]. Some how you will have to translate pattern[j] to an integer between 0 and 9 ( that would be your 10 elements of table[] ).

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top