hey, i wrote a program to create a hash table using an array of numbers, but I do not know how to display the actual table on my screen. Does anyone know how to display my results? So far, I am able to print the numbers that I have in the array. Any help or advice is appreciated. Thanks!


main.cpp

#include <iostream>
#include <cstring>
using namespace std;

int hashfunction(char *, int);
void hashTable (int &, char * );

const int HTsize = 13;

int main()
{
    
    int num[10] = {70, 32, 86, 14, 20, 7, 15, 10, 13, 5};
    
    for (int i = 0; i < 10; i++)
    {
        cout<<num[i]<<" ";
    }
    
    
    cout<<endl;
    
    system("PAUSE");
    return 0;
}


void hashTable(int &index, char *num)
{

    char *hashArray[HTsize];  // same as hashArray[][]
    bool found = false;

    for ( int i = 0; i < HTsize; i++)
    { // search for duplicate

 if(strcmp(num,hashArray[i]) == 0)    // check for same num
     found = true;
 if (found)          // duplicate found
     index = ( index + 1 ) % HTsize;
    
 strcpy(hashArray[index],num);  // locate num in hash with an index
    
    }
    
}

int hashfunction (char *key, int keylength)
{
    int sum = 0;

    for (int i = 0; i < keylength; i++)
 sum += static_cast<int>(key[i]);

    return (sum % HTsize);
}

Recommended Answers

All 8 Replies

I don't see that you use hashTable or hashfunction?

I tried to use hashTable, by doing hashTable(); but it says

6 too few arguments to function `void hashTable(int&, char*)'

Maybe I'm missing something but hashArray has nothing in it. I'm not sure why you are treating your numbers as chars?

the whole thing is confusing me.

Sorry, I am new to hashing. Is there anything I can do to improve my program?

Take a look at (but for your sake do not copy) something like this(scroll down to get the C++ version). I'm not entirely sure of how to best advise you on this. Make sure you are doing your array comparisons with the proper datatype. I get that you were probably piecing stuff together from a lot of sources...

How do I test the output of the sample, so I can see what the hash table looks like for the example? They look like header files. Also, thanks, it is definitely more user friendly than other sources.

How do I test the output of the sample, so I can see what the hash table looks like for the example? They look like header files. Also, thanks, it is definitely more user friendly than other sources.

Well, to be frank, I don't think the sample above actually does anything as of yet. It all depends on your actual hash function, which has to be a rule that says I have 100 mailboxes, some function that I perform on a piece of mail will give me a number between 0 and 99. If there's already a piece of mail in that slot, go to the rulebook and it says go one more box down until I find an empty one. So the design consideration (which you have decided to use a fixed array) is what do I do if there's no more room and I have to conform to my rules. Do I copy to a larger array, how much larger?
But you must decide your rules (or maybe they are laid out for you in the assignment) -- you may decide to put it at index (1000000*value/square root of 123) if it were justified.

Then, you can reuse your function to find the index of a particular value and if not check the next, check the next ad nauseum. So in short(lol), you can just display your array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.