C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 12th, 2005, 01:55 PM
Grakstar Grakstar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 1 Grakstar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 55 sec
Reputation Power: 0
Help With staticHTable Producing Wrong Output!

I have done these sets of code but for some reason its not give then correct ouput. I enter a word and its definition and then it gives me the hash index as well as displaying the word and its definition. this keeps looping so i can enter more words in which is correct. when i type exit and then the "enter search word" appears in type in that word i just entered and it says word not found which is incorrect. its supposed to find it and display the definition. I am testing this using HTableTest.cc. I run the programe by doing g++ staticHTable.cc HTableTest.cc and then ./a.out. Could I have some help in this:

Code:
\\staticHTable.cc
#include <iostream>
#include "staticHTable.h"
#include <string>
using namespace std;

staticHTable::staticHTable ()
{
  int i;

  for (i=0; i < TABLE_SIZE; i++)
  {  
      sHTable[i].word = "";
      sHTable[i].definition = "";

  }
}

staticHTable::~staticHTable ()
{
}

int staticHTable::hash(const string& w)
{
  int i, sum;
  
  for (sum=0, i=0; ((w[i] != '\0') && (i < w.length())); i++)
    sum += (int)w[i];
  return sum % TABLE_SIZE;
}

int staticHTable::rehash(const string& w)
{
  int sum,i;

  for (sum=0, i=0; ((w[i] != '\0') && (i <w.length())); i++)
    sum += (int)w[i];

  return sum % 19;
}

void staticHTable::put(const string& w, const string& d)
{
  int i, home, inc;
  
  i = hash(w);
  cout<< "Hash table index for word is "
      << i << " and word is :" << w << endl;
  cout<< "Definition is :" << d << endl;

  
  if ((sHTable[i].word != "") && (sHTable[i].word != w))
    {
      home = i;  
      do
	{
          inc = rehash(w);
	  i = i + inc; 
	  if (i >= TABLE_SIZE)  
	    {
	      i = i - TABLE_SIZE; 
	    }
	} while ((sHTable[i].word != "") && (sHTable[i].word != w)
                && (home != i));
        if (sHTable[i].word == "")
        {
           sHTable[i].word = w;
           sHTable[i].definition = d;
           cout<< "inserted new key into hash table location "<<i
	  << endl;
        }
	else if (sHTable[i].word == w)
             {
	       sHTable[i].definition += "\n" + d;
             }
             else
                 cout<< "can't insert key into hash table - table is full\n";
    }
  else 
    sHTable[i].definition += "\n" + d;
}

const string staticHTable::get(const string& w)
{
  int i, home, k;
  
  i = hash(w);
  if ((sHTable[i].word != "") && (sHTable[i].word != w))
  {
     k = rehash(w);
     home = i;
     do
     {
         i = i + k;
         if  (i >= TABLE_SIZE)
	 {
	     i = i - TABLE_SIZE;
	 }
     } while ((sHTable[i].word != "") && (sHTable[i].word != w) 
              && (home != i));
     if (sHTable[i].word == w)
       return sHTable[i].definition; 
   }
  else if (sHTable[i].word == w)
       { return sHTable[i].definition;
       }
  return "";
}

Code:
\\ HTableTest.cc
#include <iostream>
#include "staticHTable.h"
using namespace std;

int main()
{
    string word, def;
    int i;
    staticHTable sTable;
    
    cout << "Enter word and definition (end by typing in exit): ";
    cin >> word,def;
    getline(cin, def);
    cout << endl;
    while (word != "exit")
    {
        sTable.put(word, def);
        cout << "Enter word and definition (end by typing in exit): ";
        cin >> word,def;
        getline(cin, def);
        cout << endl;
    } 
    
    cout << "Enter search word: ";
    cin >> word;
    if ((word = sTable.get(word)) !="")
    {
      cout << "Found word and definition is: " << def << endl;
    }
    else
    {
       cout << endl << "word not in table\n";
    }
    
    return 0;
}  

Code:
//staticHTable.h
#ifndef STATIC_TABLE
#define STATIC_TABLE

#include <string>

const int TABLE_SIZE = 23;

class staticHTable
{
   public:
      staticHTable();		// constructor ? create empty table
      ~staticHTable();		// destructor
   
      int hash(const string& w);  
      int rehash(const string& w);

     /* enter data into table.
     if the word is already present in the list, update the existing 
     definition by concatenating the current one */
     void put (const string& w,const string& d);
	        
     /* return definition associated with key 'w'
        return null string if 'w' is not present in the table */	
     const string get(const string& w);
	    
   private:
      struct
      {
	  string word;
	  string definition;
      } sHTable[TABLE_SIZE];
};
#endif

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help With staticHTable Producing Wrong Output!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT