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

arrays / hash indexing?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HI
I am slowly but painfully trying to become a c programmer, I will get ther ein the end I have an assignment to do which Ido not understand the assignment involves hash index which up till now I havnt heard of I have no experience of random files nor the struct function, which I belive can be used here but Im not that far into my studies so I cant use it to date I have done basic c, arrays and jst started the geginning of pointers
I will post my question at the bottom
I obviously dont want anyone to code it for me but would be grateful if some one can break in down so as I can understand it better and give me a few tips perhaps little snipets of code that could be useful

the question
"Hashing" is a method of sorting data in an aray so that it can be retrieved in an efficient manner. Unlike a "Binary search", hashing allows the data to be stored in any order.

This is done using a hashing algorythm to generate a "HASH INDEX" from given a data item (or "KEY"). "The hash index" is then used to select the paeticular array element that will store the data (similar infact to random files).

write a program that uses two lists to store the data. One list should be student names and another a list of the course they are enrolled upon at the college. Using the students name as the key, the following algorythm will add together the ASCII values of each non-blank character in the name and then use the MOD operator to convert the total into a range of 0 to Maxnum - 1, where MaxNum is the size of th array

eeg using a value of 24 for Max num

JOHN DOE = 'J' + 'O' + 'H' + 'N' + 'D' + 'O' + 'E';

74 + 79 + 72 + 78 + 68 + 79 + 69 = 519

Many thanks
 
Well assuming that each student will give you a different value, I think your hash functino would be:

int hash(char *name)
{
/* Add up *name somehow */
int i = 1;
int sumOfName = name[0];
/* Set the "\n" to whatever terminatest the name... */
/* "\n" is a newline, or the enter key */
while (name != "\n")
{
sumOfName = name + sumOfName;
}

return (sumOfName % 24);
/* This will return a number from 0 to 23 */
}

int main()
{
char student[100]; /* Could probably be shorter */
int position; /* position in the array */
int courseNumber[24]; /*For the courses*/

student = {"Mike B\n"}; /* I'm not sure if that'll work */
position = hash(student);
courseNumber[position] = 100; /* This is how you use the hashed number */
return (0);
}

This is the idea, I have not compiled and and doubt it will work like this, but it is an example. Post any more questions you have, and good luck!

MWB>


} As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top