0

I am trying to learn C++ and trying to write a code for a simple hash table like following structure:

array[0][0] array[0][1] array[0][2]
key 1        value 1      value 2

array[1][0] array[1][1] 
key 2        value 3     

array[2][0] array[2][1] array[2][2]
key 3        value 4      value 5

means Array of Dynamic Arrays. Now, below is my code:

#include<iostream>
#include<conio.h>

using namespace std;

 int ele [10] ;
 int** arrays = new int*[10] ;

class HashTable
{
   public:

    HashTable()
    {    
         for(int i = 0 ; i < 10 ; i++)
         ele[i] = - 1 ;     // element array stores the number of elements in column of each row
    }

    void put(int key, int value){
         if(ele[key] == -1){
         arrays[key] = new int[1];
         arrays[key][0] = value ;  // initialize 2nd dimention
         ele[key] = 0 ; 
         }
         else{
              int num = ele[key] ;
              int temp[num + 1] ;
              for(int i = 0 ; i < num ; i++)
              temp [i] = arrays[key][i] ;
              temp[num+1] = value ;
              arrays[key] = new int[num + 1] ;
              for(int i = 0 ; i < num+1 ; i++)       // take all the elements in an temporary array and store it back
              arrays[key][i] = temp [i] ;
              ele[key] = num + 1 ;
              }

         }

};

main()
{

HashTable object;

 object.put(0 , 100);
 object.put(1 , 200);
 object.put(3 , 300);
 object.put(3 , 3000);
 object.put(3 , 30000);
 object.put(5 , 500);
 object.put(5 , 5000);
 object.put(5 , 50000);
 object.put(5 , 50); 


for (int i = 0 ; i < 10 ; i++ ){
int j = ele[i] ;

cout << j << " K ";

if(j != -1){
for (int k = -1 ; k < j ; k++ )    // print the values of corresponding keys
cout << arrays[i][k] << "  ";
}
}


 getch();
   return 0;
}

which uses put method to put values in the custom HashTable.

Can anybody help me why above code gives wrong output value while trying to retrieve values of the corresponding key.

5
  • I suugest that you get a good book and read a chapter on arrays ... 5 times at least. So many things are wrong in your code, that I don't know where to start. PS. This is NOT a hash table, not even near. Commented Nov 8, 2012 at 13:41
  • @aleguna, a little bit pointer will be helpful. Commented Nov 8, 2012 at 13:42
  • "why above code gives wrong output value while trying to retrieve values": what does it output? Commented Nov 8, 2012 at 13:43
  • I got stuck on above code. I will rewrite it after solving the issues. Commented Nov 8, 2012 at 13:44
  • @Raedwald It should output the value 100 for i = 0 in main, values 200, 300 for i = 1 . Commented Nov 8, 2012 at 13:45

1 Answer 1

2

This can't work:

for (int k = -1 ; k < j ; k++ )
    cout << arrays[i][k] << "  ";

You cannot have arrays with negative indexes. When you enter the loop, you have k = -1 so you try to access arrays[i][-1], which won't work.

Other recommendations:

  • The objects arrays and ele should be members of your class and be declared inside it (as private members)
  • On the line where you write arrays[key] = new int[num + 1], you forget to call delete on the previous object. This will cause a memory leak.
  • You should print your arrays using a method inside your function (like the put method) and then just call it using object.print() (if you named it print)
  • Testing if(j != -1) before the loop for (int k = -1; k < j; k++) is redundant: if j == -1 you will never go into the loop anyway.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.