Can someone please tell me how I would wrap my array. Lets say that it is of size 10. What I want it to do is count the number of letters in the name then mod it into the array (This is working properly). If a name has the same number of letters then it just looks at the next spot. So I need my array to wrap around meaning that if the 10th spot is filled then it would look at the 1st spot in the array. I know that you can do this by using the mod function but am not sure how.Here is my HashTable

template <class T>
hashTable<T>::hashTable(int Size = 10)
{
	value_arr = new T[Size];
	key_arr = new string[Size];
	status_arr = new int[Size];
	HTableSize = Size;
	// hashIndex + 1 % Size                       //Wrap around
	for (int i = 0; i < Size; i++)
		status_arr[i] = 0;
}

Here is my Hash Function

template <class T>
int hashTable<T>::hashFunction(string key)
{
	return (key.length() - 1) % HTableSize;			
	
}

Here is my add function

template <class T>
void hashTable<T>::add(string key, const T &value)
{
	//while (exists() != true)

	//int hashIndex;
	hashIndex = hashFunction(key);					
	
	while (status_arr[hashIndex] == 1)
	{
		hashIndex ++;
	}
	if (status_arr[hashIndex] !=1)
	{
		key_arr[hashIndex] = key;
		value_arr[hashIndex] = value;
		status_arr[hashIndex] = 1;
	}
		
}

Thanks for the help!

This might help:

#include <iostream>

using namespace std;

int main()
{
    const int size = 5;
    int a[] = {1, 2, 3, 4, 5};
    int k = 0;

    for (int x = 0; x < 13; ++x)
    {
        cout << x << ": " << a[k++ % size] << '\n';
    }
}

I think a test and assignment is clearer and safer than doing a remainder operation, even though you lose l33t points by doing it the boring way. ;)

#include <iostream>

using namespace std;

int main()
{
    const int size = 5;
    int a[] = {1, 2, 3, 4, 5};
    int k = 0;

    for (int x = 0; x < 13; ++x)
    {
        cout << x << ": " << a[k++] << '\n';

        if (k == size) k = 0;
    }
}

You get your initial index but you'r stuck in a loop until you find a non-one! But you never wrap your index!

while (status_arr[hashIndex] == 1)
	{
//		hashIndex ++;
                                hashIndex = (1+hashIndex) % HTableSize;
	}

What happens if no non-ones are found? Infinite loop?

Also right now, you can only leave that loop on a non-one. But your code checks to see if its non-one to do the conditional?

Thanks wildgoose that is what I needed.

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.