C++ Coding with 2D Array
<p>I am trying to learn C++ and trying to write a code for a simple hash table like following structure:</p>

<pre><code>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
</code></pre>

<p>means Array of Dynamic Arrays. Now, below is my code:</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;conio.h&gt;

using namespace std;

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

class HashTable
{
   public:

    HashTable()
    {    
         for(int i = 0 ; i &lt; 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 &lt; num ; i++)
              temp [i] = arrays[key][i] ;
              temp[num+1] = value ;
              arrays[key] = new int[num + 1] ;
              for(int i = 0 ; i &lt; 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 &lt; 10 ; i++ ){
int j = ele[i] ;

cout &lt;&lt; j &lt;&lt; " K ";

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


 getch();
   return 0;
}
</code></pre>

<p>which uses put method to put values in the custom HashTable.</p>

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