Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

arrays / hash indexing?

arrays / hash indexing?

arrays / hash indexing?

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

RE: arrays / hash indexing?

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!



As always, I hope that helped!

Beware: Studies have shown that research causes cancer in lab rats.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close