change array size

This is a discussion on change array size within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a real beginner, and I am having trouble, can someone please help. I have to complete some ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    change array size

    Hi,

    I am a real beginner, and I am having trouble, can someone please help. I have to complete some code to change the size of an array to a new size (newS) that is specified by the argument in program that tests it. The program that I have to code uses a template and I'm a bit unsure how to go about this. I think I need to use 'new' but not sure how.

    I have to complete the code for public function void grow(int);
    template <class BType, class IType>
    void Array<BType, IType>::grow(int newS)
    {
    // I need to code this, everthing else is already done
    }


    The program for testing this is
    int main(void)
    {
    int i;
    Array<int> A(5);
    ofile.open("A11.out");
    for (i = 0; i < 5; ++i)
    A[i] = i;
    printIt(A, "Original");
    A.grow(10);
    for (i = 5; i < 10; ++i)
    A[i] = 2 * i;
    printIt(A, "Increased");
    A.grow(4);
    printIt(A, "Decreased");
    ofile.close();
    return 0;
    }

    Thanks

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you're right about using the "new" operator ......

    the trick to it is to declare the original array dynamicaly, meaning on the "heap" or (so called "free store")

    Code:
     int Size = 20;
    
     int * IntArray[Size] = new int[Size];
    i believe after wards you can change the size of an array dynamicaly depending on the user's input.

    hope that helps...

    ps. you can also try "linked lists"

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Smile i guess you can do so:

    void Array<BType, IType>::grow(int newS)
    {
    //assume thatyou have an pointor in Array<BType,IType>:
    //Type* old,
    Type *tmp=new Type[newS];
    //copy the elements to the new one that on the heap
    ......
    delete old;
    old =tmp;
    }

    i'm a beginner too.

    hope that helps...

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    more specific question

    I have got a little further....This is the code I have used, however I don't know how to retain the original values - do I somehow have to copy the original array first? I tried to do this but it didn't work so I must not be doing it right, any ideas???

    template <class BType, class IType>
    void Array<BType, IType>::grow(int newS)
    {
    hibnd = newS - 1;
    arrayData = new BType[newS];

    }

    output.....

    Original:0,1,2,3,4.

    Increased:4325492,4325436,0,0,0,10,12,14,16,18.

    Decreased:4325492,4325436,0,0.

  5. #5
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    A tip that helps me.
    Write the code simple. then convert it into a template.

    Advance tip:
    Dynamic arrays are great!! I could not live without them. But the have a large cost when they get big. So I suggest you research linked list. and use them the same way as an array.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: i guess you can do so:

    Originally posted by atandix
    Code:
    void Array<BType, IType>::grow(int newS) 
    { 
      //assume thatyou have an pointor in Array<BType,IType>: 
      //Type* old,
      Type *tmp=new Type[newS];
      //copy the elements to the new one that on the heap
      ......
      delete old;
      old =tmp;
    } 
    

    i'm a beginner too.

    hope that helps...
    I believe you should have put brackets inbetween your delete and old. So the new line would look like this:

    delete [] old;

    Remember when calling delete, you put the empty brackets for deleting single AND multi-dimensional arrays.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >do I somehow have to copy the original array first?

    Create a pointer to the original array, then use 'new' to create the new array. Next copy the data from the old array to the new array. Finally use 'delete' to free the old array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the size of an array of structs
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2008, 06:29 AM
  2. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 08:23 PM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 10:20 PM
  4. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  5. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM

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