deleting only the first number in an array

This is a discussion on deleting only the first number in an array within the C++ Programming forums, part of the General Programming Boards category; i know this may seem trivial, bt everytime i program i realize how much i really don't know...anyway to the ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    deleting only the first number in an array

    i know this may seem trivial, bt everytime i program i realize how much i really don't know...anyway to the point.
    I have to write a stack program for my data structures class. i have most of the stuff done, but i need to be able to delete only the first most object in the array.
    ie.
    an array:

    2
    3
    5
    6
    7

    i want only to delete the 2.
    --I think my stack is a bit better vertically

    thanks for any help.
    Last edited by xxmetalheadxx; 02-25-2006 at 12:58 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Are you sure that's what you need to do? A stack usually requires that you remove the top before you remove things from the bottom, so you would have to remove 7, 6, 5 and 3 before you removed 2.

    If you do need to do it, you could move each element down one spot in a loop, or create a new array and copy all but the first element over.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    I've never heard of that being done. Your best bet would be to allocate a new array one element smaller than your last, assign the old array to it, starting with the second element, and deleting the entire old array.

    Code:
    #include <iostream>
    
    const char ARRAYSIZE = 10;
    
    int main() {
        int *Arry1, *Arry2;
        
        Arry1 = new int[ARRAYSIZE];
        
        std::cout << "Array 1:" << std::endl;
        for(int i = 0; i < ARRAYSIZE; i++) {
           Arry1[i] = i * 5;
           std::cout << Arry1[i] << ' ';       
        }
        
        Arry2 = new int[ARRAYSIZE-1];
        
        std::cout << "\n\nArray 2:" << std::endl;
        for(int i = 0; i < ARRAYSIZE-1; i++) {
           Arry2[i] = Arry1[i+1];
           std::cout << Arry2[i] << ' ';
        }
           
        delete[] Arry1;
        delete[] Arry2;
    
        return 0;
    }
    *Note: I'm assuming that by "delete an element" you mean physically remove from memory. I should hope a person taking a data structures class should know how to overwrite an element.
    Last edited by SlyMaelstrom; 02-25-2006 at 01:06 AM.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    After you edited your post it is unclear how your data is stored in the array. If the 2 is at the end, then just leave it there and change the size you saved (or if you are using a sentinel, put the sentinel there). If the 2 is at the beginning of the array, then see my previous post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 06:37 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

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