Array question - Beginner

This is a discussion on Array question - Beginner within the C++ Programming forums, part of the General Programming Boards category; How would I reorganize an array? Lets say I have an array of an unknown data type that I chose ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Array question - Beginner

    How would I reorganize an array?

    Lets say I have an array of an unknown data type that I chose to remove by simply overwriting that index number by its following members

    So for example, I found an item for removal in BTable[h][0] and there are
    3 things in the array. I need to move BTable[h][1] into BTable[h][0] and BTable[h][2] into BTable[h][1]

    Thanks

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try
    Code:
    BTable [h][0] = BTable[h][1];
    BTable [h][1] = BTable[h][2];
    BTable [h][2] = 0; //Assuming it's int, if not, use appropriate statement.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    If my array is quite large how might I do this in a loop (its array datatype is TYPE <template class TYPE>)

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Code:
    for (unsigned int i = 0; i != 2nd_dimension_size; ++i) {
       if (i == 2nd_dimension_size - 1) {
          // do whatever you want to do to the last index
       }
    
       array[h][i] = array[h][i+1];
    
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Will that work ofr any array size?

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    If the array is 3 elements big and I found what I want to remove at element 3, nothing moves
    if I found it at element 2, then element 3 goes into element 2 leaving element 3 empty

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Quote Originally Posted by INFERNO2K
    Will that work for any array size?
    No. arrays have a limite in size established by the definition of size_t. On 32bit systems this is typically a unsigned long. But... it can be different. You need to check your implementation.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Quote Originally Posted by INFERNO2K
    If the array is 3 elements big and I found what I want to remove at element 3, nothing moves
    if I found it at element 2, then element 3 goes into element 2 leaving element 3 empty
    Have you actually tried the code I gave you?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 03:58 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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