Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:21:17+00:00 2026-06-14T02:21:17+00:00

I am trying to learn C++ and trying to write a code for a

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:21:19+00:00Added an answer on June 14, 2026 at 2:21 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn about stack base overflow and write a simple code to
I am trying to learn how to write more dynamic web sites that use
I'm trying to learn Haskell, so I decided to write a simple program to
I've just started to learn Haskell and I am trying to write a simple
Trying to learn Haskell. I am trying to write a simple function to remove
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
I am trying to write a simple program about UDP Connections to learn about
I am trying to write a simple char driver in an attempt to learn
I am trying to learn how to write plugins using SIMBL. I got my
I'm trying to learn C and I'm currently trying to write a basic stack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.