Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

cannot get the hash function in c to work

Posted on 2004-10-08
7
222 Views
Last Modified: 2010-04-17
Hi experts, I'm doing an assignment on hash tables and require some advise

I have the following structs:

typedef struct bucket *ListPtr;
typedef struct conListNode *ListNodePtr;
typedef struct ipaddr
{
  int digit1, digit2, digit3, digit4;
}IPAddress;
typedef struct bucket
{
  int           size;
  ListPtr       listPtr;
}Bucket;
typedef struct conListNode
{
  IPAddress     ipAddress;
  int           portNumber;
  ListNodePtr   next;
}ConListNode;
typedef ListPtr BucketPtr;
typedef struct connctnList
{
  int         tableSize_N;
  int         bucketThreshold_m;
  int         primeNo1_k1;
  int         primeNo2_k2;
  int         primeNo3_k3;
  int         primeNo4_k4;
  int         primeNo5_k5;
  BucketPtr   *bucketPtr;
}connctnList;

To initialise the table, I have the following function:

int makeConnectionList(connctnList *conList)
{
  int counter;
  conList = malloc(sizeof(connctnList));
  if(conList == NULL)  {    return FAILURE;  }

  conList->tableSize_N = TABLE_SIZE;
  conList->bucketThreshold_m = INITIALISE;
  conList->primeNo1_k1 = K1;
  conList->primeNo2_k2 = K2;
  conList->primeNo3_k3 = K3;
  conList->primeNo4_k4 = K4;
  conList->primeNo5_k5 = K5;

  conList->bucketPtr = malloc(sizeof(Bucket) * conList->tableSize_N);

  if (conList->bucketPtr == NULL)  {    return FAILURE; }

  for (counter = 0; counter < conList->tableSize_N; counter++)
  {
    conList->bucketPtr[counter] = malloc(sizeof(Bucket));
    if(conList->bucketPtr[counter] == NULL)    {
      return FAILURE;
    }
    else    {
      conList->bucketPtr[counter]->listPtr = NULL;
    }
  }
  return SUCCESS;

This function to call the hash function to calculate position within array is:

int addConnection(connctnList *conList, IPAddress address, int port)
{
  int hashValue = INITIALISE;
  hashValue = hashCalculation (conList, address, port);
  printf("%d\n", hashValue);
  return 0;
}

And this is the hash function:

int hashCalculation(connctnList *conList, IPAddress address, int port)
{
   int hashValue = INITIALISE;

  hashValue = ((conList->primeNo1_k1 * address.digit1) + \
              (conList->primeNo2_k2 * address.digit2) + \
              (conList->primeNo3_k3 * address.digit3) + \
              (conList->primeNo4_k4 * address.digit4) + \
              (conList->primeNo5_k5 * port)) % \
               conList->tableSize_N;
  return hashValue;
}

Can someone please explain to me why the hashValue printed out is rubbish.

Thanks a million from a very confused person.
0
Comment
Question by:kwy
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:pratap_r
ID: 12260227
can you post the code from where you are calling makeConnectionList ??

as far as i can tell your connection object is not passed properly

try changing to
int makeConnectionList(connctnList **b)
{
connctnList *conList;
conList = (connctnList*)malloc(sizeof(connctnList));
*b=conList;
......
.....
}

main()
{
      connctnList *conList;
      makeConnectionList(&conList);
      ipaddr ip;
      ip.digit1=127;ip.digit2=0;ip.digit3=0;ip.digit4=1;
      addConnection(conList,ip,100);
}

using a table size of 100 i got the hash as 61 --> seems to work!!?

enjoy!
pratap
0
 
LVL 11

Expert Comment

by:pratap_r
ID: 12260241
oh and i used 2,3,5,7,11 as values for K1, k2, k3, k4, k5 since i dint know what values you were using.. but i dont see any problem with that
0
 

Author Comment

by:kwy
ID: 12263371
Hi pratap

Because this is an assignment, I cannot change the function parameters.  Please see below the extracts from main where the functions are called, your advise is much appreciated :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "connctnList_HT.h"
#include "connctnList.h"

int main (int argc, char **argv)
{
  connctnList conList;

  IPAddress ipAddress1;
  IPAddress ipAddress2;
  -------------etc-----------------
 if (ParseLine(line, &tempCommand, tempIPAddress, tempPortOrIP) == FALSE)
    {      printf("\nError on Line Number %d in file %s", lineNum, argv[FILENAME]);    }
 else
 {
      if (tempCommand == 'C')
      {
        if (makeConnectionList(&conList) == FALSE)
        {         FatalError("Unable to create list !!");        }
      }
      else if (tempCommand == 'A')
      {
        sscanf (tempPortOrIP, "%d", &port);

        if (ParseIP(tempIPAddress, &ipAddress1) == FALSE)
        {     printf("Unable to create IPAddress structure");        }
        if (addConnection(&conList, ipAddress1, port) == FALSE)
        {     FatalError("Unable to add node !!");       }
      }
     -----etc

Thanks again.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:suryaxchange
ID: 12265264
In the following declararation,
  int makeConnectionList(connctnList *conList)
connctnList *conList is wrong.

Use connctnList **conList, because you are allocating memory for conList in the function i.e., you are modifying the pointer so, you need to send the pointer to this pointer same as you send address of integer to modify the value of that integer.

Try this you will get succeeded
Surya.
0
 
LVL 11

Accepted Solution

by:
pratap_r earned 500 total points
ID: 12265449
yup thats exactly what you need to do. that is what i had mentioned in my reply .. change the * to ** in input parameter :-)

in your main function you will have to change the declaration of connctnList conList; to connctnList *conList; because you are allocating memory in your make connection function. but when you declare connctnList conList; your memory is already allocated! if you are not allowed to change your parameter list then you should remove the malloc inside your makeconnection function..

your new make connection will be
int makeConnectionList(connctnList *conList)
{
  int counter;
  if(conList == NULL)  {    return FAILURE;  }
  conList->tableSize_N = TABLE_SIZE;
.....
...
}
 
nothing else needs to be changed... :-)  rest of the code is fine. the malloc was basically allocating a memory but your main's conlist was not getting it properly...

enjoy!
Pratap
0
 

Author Comment

by:kwy
ID: 12265515
Thanks guys for your help, especially you Pratap.
The program is now working beautifully!
kwy
0
 
LVL 11

Expert Comment

by:pratap_r
ID: 12265604
with pleasure :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question