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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic Suggestion

Status
Not open for further replies.

logic4fun

Programmer
Apr 24, 2003
85
US
Hi guys,
I am again in the world of confusion. I am working with a state engine where it retrieves data states of different persons from a database for a particular interval of time and i will modify them in my "C" program. The question is everytime i need the last state of that particular person when i am dealing the next Interval.

Note: The persons are Random i.e. there is no possibility that they come in a particular order in an interval

the possible solutions i thought are.
* Store the personName and State in the two dimensional array and search for it when ever i change the interval.(Dont KNOW HOW TO IMPLEMENT :-(

* Another idea is to load the last states into a Temporary table of DB2 database and query them..Which will kill lot of time because of the querying..I CAN DO THIS BUT POOR IMPLEMENTATION...

ANY SUGGESTIONS...

Thanks in advance
logic4fun
 
> Which will kill lot of time because of the querying
Whilst this may be true, is it likely to be non-starter without trying it?

What sort of table sizes are you talking about, and how many transactions (or transactions per second) are you likely to be performing?

The C solution may be more optimal, but the cost of implementation is also greater. Rule 1 of this page may apply

> Store the personName and State in the two dimensional array
To keep the name associated with the state, I would use a structure.
 

Easy:

You could store each person retrieved in an array and then do a linear search or binary search on the array to retrieve it (for the latter, you would have to keep the array sorted).

Your framework could be something like this:

#define MAXNAME 50
#define MAXPERSONS 100

struct person {
char name[MAXNAME + 1];
char state[3];
} persons[MAXPERSONS];
int person_count;

to insert:

int person_insert(const char *name, const char *state)
{
struct person p;
strcpy(p.name, name);
strcpy(p.state, state);
if (person_count == MAXPERSON) return -1;
persons[person_count++] = p;
return 0;
}
struct person *person_search(const char *name)
{
int i;
struct person *result = NULL;

for (i = 0; result == NULL && i < person_count; ++i)
if (strcmp(persons.name, name) == 0)
result = &persons;
return result;
}

This is about the slowest method, but may be adequate for your purposes.

Faster options would be using self-ordering data structures such as binary search trees or hash tables, which would take a while to explain here. K&R 2 has examples of both which you could pretty much copy and paste from the book.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top