Remove elements from array

This is a discussion on Remove elements from array within the C++ Programming forums, part of the General Programming Boards category; Ive got an array lets call it even[]; now in even i have values but also some Zeroes. Now all ...

  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,231
    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,262
    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,262
    ... 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,231
    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:11 AM
  3. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 07:05 AM
  4. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 12: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

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