Thread: Remove elements from array

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Remove elements from array

    Ive got an array lets call it even[];

    now in even i have values but also some Zeroes. Now all id like to do is remove those Zeroes from the array.

    Can someone give me a push into the right direction how to achieve this.

    I was thinking along the lines of

    while(even[k] != MAX)
    if(even[k] != 0);
    {
    evenclean[k] = even[k]
    k++;
    }

    could this work??

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Of course you mean:
    Code:
    while(k != MAX)
    {
          if(even[k] != 0)
          {       
                evenclean[i] = even[k] ;
                i++;
                
          }
          k++;
    }
    Yes, why not?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    USE CODE TAGS
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by maxorator
    USE CODE TAGS
    Do you have that saved as a template at this stage?

    http://cboard.cprogramming.com/searc...earchid=536448

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    No, I'm writing it out every time...
    Also, I have to edit it every time, because when you originally post, the upper-case words will be made lower-case with the first character upper-case and that's not done when editing the post.

    Also, posting this thing is a good advertise to my cat (see my signature).
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    ... right. That pre-screen is annoying! That's a good pic of the leg with the foreshortening. max3.jpg I thin it was.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The site is loading too slowly.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It loads quite quickly for me. Maybe it's slow for foreign connections...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you used a std::vector instead of an array, you could #include <algorithm> and write:
    Code:
    even.erase(
        std::remove_if(even.begin(), even.end(),
            std::bind2nd(std::equal_to<int>(), 0)),
        even.end());
    At least assuming that you are dealing with ints. Of course, with an array you can still write:
    Code:
    // assuming even has not decayed into a pointer
    int* even_end = even + sizeof(even) / sizeof(int);
    even_end = std::remove_if(even, even_end, std::bind2nd(std::equal_to<int>(), 0));
    Then you just use the range from even to even_end-1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  3. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 08:05 AM
  4. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 01:46 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM