CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: dynamic array resizing

  1. #1
    Join Date
    Oct 2014
    Posts
    9

    dynamic array resizing

    I have to write a program where the user will input integer numbers. How many numbers they enter is unknown, therefor you should use a repetition structure for the input. When the user is done, they will enter -1 to exit.

    Create a dynamic array if the size=2( the initial size must be 2)
    Repeat until user enters -1.

    I have to do this without using vectors.

    This is what i have, I cannot figure out what to put in main. I was thinking of a do-while?

    This is my first time in c++
    Any hints?


    Code:
    #include <iostream>
    using namespace std;
    void resize(int *[], int);
    int main()
    {
    	int *listDyn;
    	int size=2;
    	listDyn=new int[size];
    
    	
    		for (int i=0; i<size; i++)
    		{
    			cout << "Enter as many integers as you'd like or enter -1 to exit" ;
    			cin >> listDyn[i]; 
    		}
    
    
    void resize(int *listDyn, int size)
    {
    	int *listDynNew=new int[size*2];
    	for (int i=0; i<size; i++)
    		listDynNew[i]=listDyn[i];
    	size++;
    	listDyn=listDynNew;
    	delete[] listDynNew;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,189

    Re: dynamic array resizing

    Your for loop will only allow 2 numbers to be entered!

    Your resize function also has issues. Why are using size * 2? As listDyn and size are passed by value, any new value assigned to them during the function isn't passed back to the calling code.

    I have to do this without using vectors.
    Shame! Boo!

    I was thinking of a do-while?
    Your loop in main() needs to loop until either a -1 is entered or an error occurs on the input stream.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: dynamic array resizing

    Honestly, your assignment doesn't make sense, even if you can't use vector.

    You're creating a dynamic array in main, but then you just call resize() which basically boils down to doing nothing. Yes, you're allocating memory in resize, but then you deallocate the memory at the end of resize. Basically a lot of wheel-spinning with nothing being accomplished. Are you sure you're supposed to deallocate the memory you just allocated, or is that a bug?
    Code:
    listDyn=listDynNew;
    delete[] listDynNew;
    ?

    Also, you have a memory leak, since the initial allocation of listDyn is never deallocated.

    Regards,

    Paul McKenzie

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,597

    Re: dynamic array resizing

    You should use a while loop that accepts an integer and terminates when that integer is -1. You can't input directly to the array is it probably isn't going to be big enough.

    A dynamic array works by allocating more memory than the current array, copying the current array into the new memory and deleting the old. Of course, the pointer to the start of the array will change each time you do this. You'll need a function to add your int to the array, resizing it if necessary.

  5. #5
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: dynamic array resizing

    I can advise you to read Data Programming in C++,

    this will provide you a good understanding for how to manage the memory during run time of your program..


    you can read also read Ivor horton book " VC++ Programming Published By Wrox"

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,189

    Re: dynamic array resizing

    I can advise you to read Data Programming in C++,
    Can you provide more details please as I can't find a book on Amazon with this exact title.

    Ivor horton book " VC++ Programming Published By Wrox"
    Again in Amazon there does not appear to be a book by Ivor Horton with this title. The latest ones by him are
    Beginning Modern c++
    http://www.amazon.co.uk/Beginning-C-...horton+c%2B%2B

    beginning Visual c++ 2013
    http://www.amazon.co.uk/Ivor-Hortons...horton+c%2B%2B

    Neither of which I have seen so can't comment upon them.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)