Thread: Checking to see if an individual element in an array is filled

  1. #1
    Registered User
    Join Date
    Nov 2007

    Checking to see if an individual element in an array is filled

    Hi everyone,

    I am writing the insert function for a hashtable and I am having one issue. When going to insert a new integer I have to check to see if a bin is full or not.
    To do this I am doing,

    while(array[hashinsert] !=0 )

    However the compiler reads this as, if that bin does not have a 0 in it. How do I make it so it checks to see if it is already occupied.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You need another array of booleans, say
    if ( ! inuse[hashinsert] )

    Then set it to true when you update array[hashinsert]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    OR, you have to pick a value that you never plan to use such as -1 and use that to mean that the spot is empty, which would work if you were say only ever intending to store positive numbers.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2007
    Ok, thanks i, that is what I is working perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. How to return the last element of an array?
    By BuezaWebDev in forum C Programming
    Replies: 20
    Last Post: 03-26-2005, 10:57 AM
  4. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 07:27 PM
  5. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM