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

This is a discussion on Checking to see if an individual element in an array is filled within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am writing the insert function for a hashtable and I am having one issue. When going to ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    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.

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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
    Posts
    2
    Ok, thanks i, that is what I did...it is working perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02: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, 09:57 AM
  4. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 06:27 PM
  5. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21