CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Table search algorithm
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

Table search algorithm
PostPosted: Thu Jun 10, 2004 7:09 pm     Reply with quote

I am writing some code to deal with table searchings and lookup.

This routine returns the position a given number is at some table.. based in this number it will lookup another. Example:

10, 20, 30, 40, 50, 60, 70, 80, 90, 100 // size 10

if value=55 then it would return 5

It is returning 0 and I can't find why, in simulator it is fine, but in real hardware id does not.

*pointer points to a array[8] inside a struct and &struct.array[0] tells the first address, sizeof(struct.array) tells the size (8 in this case).

Code:
table_search(*pointer, size, value) {
    int8 count=0;
    while((value>*pointer)&&(--size)) { count++; pointer++; }
    return count;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 10, 2004 11:02 pm     Reply with quote

You need to post a complete test program, that we can drop into
MPLAB and test. It should be a small program, but you should post
all #use statements, #fuse statement, etc.
Guest








PostPosted: Fri Jun 11, 2004 1:50 am     Reply with quote

Maybe it is a typo but in your example you put a value instead of address.
table_search(*pointer, size, value) is incorrect, use
table_search(pointer, size, value)
In CCS C if you declare table_search(char *pointer, size, value)
and you call it as table_search(&table,.....)
then it puts the lower byte of the address of the table. That is usually greater than 55.
Ttelmah
Guest







Re: Table search algorithm
PostPosted: Fri Jun 11, 2004 4:23 am     Reply with quote

future wrote:
I am writing some code to deal with table searchings and lookup.

This routine returns the position a given number is at some table.. based in this number it will lookup another. Example:

10, 20, 30, 40, 50, 60, 70, 80, 90, 100 // size 10

if value=55 then it would return 5

It is returning 0 and I can't find why, in simulator it is fine, but in real hardware id does not.

*pointer points to a array[8] inside a struct and &struct.array[0] tells the first address, sizeof(struct.array) tells the size (8 in this case).

Code:
table_search(*pointer, size, value) {
    int8 count=0;
    while((value>*pointer)&&(--size)) { count++; pointer++; }
    return count;
}

Series of comments.
First your declarations should include 'types'. Your declaration does not say what 'pointer' is a pointer _to_. C needs this, to know how to handle arithmetic on the pointer.
Second, use 'return(count)', rather than 'return count'. CCS sometimes has problems with the second type of declaration.
Generally, learn to be explicit in all declarations in CCS C. Though there is a 'default' in C, that a function returns a default sized integer if not declared, it is generally safer throughout, to declare everything. The code seems to work across more compiler versions with less problems if this is done.
Code:

int8 table_search(char *pointer,int8 size,char value) {
    int8 count=0;
    while((value>*pointer)&&(--size)) { count++; pointer++; }
    return(count);
}

Remember also, that the 'table' will need to be in RAM, and not a constant table in ROM for this to work.

Best Wishes
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Fri Jun 11, 2004 6:59 am     Reply with quote

Ttelmah thank you for the explanations.

It is very strange because this routine is called 2 times and only one works in hardware... I think this one will be hard to debug ;(

In SIM I watched all variables, pointer, size, value... all Ok in both calls.

Tables are arrays within structs, two 8 position arrays... pointers to them are 245 and 253.
Ttelmah
Guest







PostPosted: Fri Jun 11, 2004 7:46 am     Reply with quote

future wrote:
Ttelmah thank you for the explanations.

It is very strange because this routine is called 2 times and only one works in hardware... I think this one will be hard to debug ;(

In SIM I watched all variables, pointer, size, value... all Ok in both calls.

Tables are arrays within structs, two 8 position arrays... pointers to them are 245 and 253.

Now that description really is 'strange'. Seriously, I'd be looking at something else. For instance, is it possible that an interrupt may be occurring inside the second call?. Is it possible that the contents of the comparison array itself is being corrupted by something else?.
I'd look to adding a massive 'debug' at the entry to the call. Dump the memory area addressed by the pointer to RS232, or copy it to EEPROM, and then look at this after running the chip. Also copy the comparison value, and the size being passed....

Best Wishes
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Fri Jun 11, 2004 5:06 pm     Reply with quote

Ttelmah, I found the problem... and the cause was me Embarassed

I Forgot that one item in the struct was int16, so everything after it was offset by 1 byte and the value of the first item in table was high... that's why it was always returning 0.
Ttelmah
Guest







PostPosted: Sat Jun 12, 2004 2:21 am     Reply with quote

future wrote:
Ttelmah, I found the problem... and the cause was me Embarassed

I Forgot that one item in the struct was int16, so everything after it was offset by 1 byte and the value of the first item in table was high... that's why it was always returning 0.

I know the feeling all too well...
At least you have 'cracked' it now.

Best Wishes
wgalaugher



Joined: 10 Jan 2011
Posts: 18

View user's profile Send private message

table search algorithm
PostPosted: Wed Jun 15, 2011 9:25 am     Reply with quote

I was looking for this topic and ran across this thread. Its quite old and thin on details.

I need some help to define the structure of the table.

I am writing some code to deal with table searchings and lookup.

This routine returns the position a given number is at some table.. based in this number it will lookup another. Example:

10, 20, 30, 40, 50, 60, 70, 80, 90, 100 // size 10

if value=55 then it would return 5.

It is returning 0 and I can't find why, in simulator it is fine, but in real hardware id does not.

*pointer points to a array[8] inside a struct and &struct.array[0] tells the first address, sizeof(struct.array) tells the size (8 in this case).
Code:

table_search(*pointer, size, value) {
    int8 count=0;
    while((value>*pointer)&&(--size)) { count++; pointer++; }
    return count;
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jun 15, 2011 1:37 pm     Reply with quote

When you copy code, then make sure to read the whole thread.
Code:
table_search(*pointer, size, value) {
As mentioned in a comment above, this line is wrong.

Always post your compiler version.
Always post a complete test program.
wgalaugher



Joined: 10 Jan 2011
Posts: 18

View user's profile Send private message

Table search algorithm
PostPosted: Wed Jun 15, 2011 3:12 pm     Reply with quote

I got that part. Thanks.

What I'm looking for is the structure of the data array. The guy who posted the original did not provide all the code and that was back in 2004.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jun 15, 2011 4:00 pm     Reply with quote

Only now I see that most of your post is a literal quote of the topic start. It helps a lot when you mark the quote so it is recognizable as a quote, now it is not clear what your question is. Use the 'quote' buttons to mark text as a quote next time.

The original topic poster used an array inside a struct, but for the working of the search function this is irrelevant, the search function expects an array and doesn't care how many layers you wrap around it. Something like the following will do the trick:
Code:
#include <18F458.H>
#fuses HS,NOWDT,NOLVP
#use delay(clock=16000000)


int8 table_search(char *pointer, int8 size, char value)
{
   int8 count=0;
   while((value>*pointer)&&(--size)) { count++; pointer++; }
   return count;
}

void main()
{
  char Array[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};    // size 10
  int8 Position;

  Position = table_search(Array, sizeof(Array), 55);

  for(;;);
}
Tested in MPLAB simulation and found: Position = 5
wgalaugher



Joined: 10 Jan 2011
Posts: 18

View user's profile Send private message

Table search algorithm
PostPosted: Wed Jun 15, 2011 4:12 pm     Reply with quote

Thank you

I'm a little new to the forum, but I learn fast. Sorry for the confusion!

Its amazing when you see how its done, how simple it is. I danced all around the target with all kinds of attempts that were close. But close doesn't cut it in coding.

I was trying to replicate a lookup function in pbasic. This will do.

Thanks again

Wayne
wgalaugher



Joined: 10 Jan 2011
Posts: 18

View user's profile Send private message

PostPosted: Wed Jun 15, 2011 11:20 pm     Reply with quote

I got the code working as advertized.

When I filled the array with the following:
Code:

char array [] = (1800,2001,3500,4001,7000,7351,10100,10201,14000,14351,18068,18169,21000,21451,24890,24991,28000,29701);

it returns a position of 1 for a value of 7001 or any other value between the min or max of the array.

I think I'm over an int value of 256. I've changed all the variables to unsigned int16 but no joy.

Is this doable

Wayne
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jun 16, 2011 7:38 am     Reply with quote

wgalaugher wrote:
I think I'm over an int value of 256.
Yep.

Post your actual code. The line you have posted now does not compile because the array is enclosed in '()' instead of '{}'.

Quote:
I've changed all the variables to unsigned int16 but no joy.
Did you also chance the char* pointer in the declaration of the search function? Otherwise it will treat the array as 8 bit integers...

A small but important thing to overlook is the 'sizeof' operator that I used in the example program. It will return the size of the array in bytes. As long as your array contains byte size elements it is an easy method to count the number of elements in the array. Now that you changed to int16 it doesn't work anymore as expected. See the improved version below.

Code:
#include <18F458.H>
#fuses HS,NOWDT,NOLVP
#use delay(clock=16000000)

#define NR_ELEMENTS(x) (sizeof(x)/sizeof(x[0]))

int8 table_search(int16 *pointer, int8 size, char value)     //   <<---  note the int16 *
{
   int8 count=0;
   while((value>*pointer)&&(--size)) { count++; pointer++; }
   return count;
}

void main()
{
  int16 Array[] = {1800,2001,3500,4001,7000,7351,10100,10201,14000,14351,18068,18169,21000,21451,24890,24991,28000,29701};
  int8 Position;

  Position = table_search(Array,NR_ELEMENTS(Array), 21002);    //   changed the automatic array calculation to be more flexible for other data types

  for(;;);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group