Thread: bus error

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    29

    bus error

    what causes the bus error in the hashtable implementation file?

    Code:
    #include "Hashtable.h"
    
    // Enter your implementation here
    Hashtable::Hashtable(int m_that){
    m = m_that;
    //if (!T.empty()){
    for(int j=0;j<m;j++)
    T[j] = -1;
    //}
    }
    
    // The primary hashing formula
    int Hashtable::h1(int k){
      return k%m;
    }
    
    // The secondary hashing formula
    int Hashtable::h2(int k){
      return 1+(k % (m-1));
    }
    
    // The overall (combined) hashing formula
    int Hashtable::h(int k,int i){
    
      int index;
      while(i<m){
      index = (h1(k) + i*h2(k))%m;
      if(T[index] != -1)		  //occupied slot
      i++;
      if(T[index] == -1)		  //empty slot
      //if(index == m)
      //T[index-1] = k;
      //else
      T[index] = k ;  // Insert value 'k' into index = h1(k) in vector T
      return index;		//insertion ok, double hashing, call double hash
      }
      
      for(int j=0;j<T.size();j++){
      if (T[j] == k)
      return -2;  // If 'k' is already exist in the table
      if(i==m)				// else if vector T is full, all -1 slots filled up
      return -1;             //this method returns -1. No change to vector T
      }  
      }
    	
    int Hashtable::Hash_Insert(int k){
      int i=0;
      return h(k,i);  	
      }
    			  
    int Hashtable::Hash_Search(int k){
    
    if(!T.empty()){
    for(int j=0;j<T.size();j++){
    if (T[j] == k)
    return j;   // Returns the index of item 'k' in vector T
    if (T[j] != k)
    return -1;  // Returns -1 if item 'k' is not found
    }
    }
    }

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Your search is broken to start with ...

    Code:
    int Hashtable::Hash_Search(int k)
    {
        if (!T.empty())
        {
            for(int j=0;j<T.size();j++)
            {
                if (T[j] == k)
                    return j;   // Returns the index of item 'k' in vector T
                if (T[j] != k)
                    return -1;  // Returns -1 if item 'k' is not found
            }
        }
    }
    Should be

    Code:
    int Hashtable::Hash_Search(int k)
    {
        if (!T.empty())
        {
            for(int j=0;j<T.size();j++)
            {
                if (T[j] == k)
                    return j;   // Returns the index of item 'k' in vector T
            }
        }
    
        return -1;  // Returns -1 if item 'k' is not found
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    29
    yeah u were right. thanks for pointing out tt error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM