Dynamic array help needed
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: Dynamic array help needed

  1. #1
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Dynamic array help needed

    hello once again,

    atm i am working with some dynamic memory using "new" and i bit confused what the task asks me :S

    3. Write a program that will dynamically allocate an array of dimension N where N is a value entered by the user.
    • The user should be prompted to enter values form the keyboard to fill the array.

    Code:
    #include <iostream>
    
    int main()
    {
    	int* pDym = NULL;
    	int n = 0;
    	std::cout<< "Enter the size of the array: ";
    	std::cin>> n;
    	pDym = new int[n];
    
    
    	for(int i = 0; i<n; i++)
    	{
    		std::cin>> pDym[i];
    	}
    
    	for(int i = 0; i<n; i++)
    	{
    		std::cout<< " " << pDym[i] << " ";	
    	}
            
            delete [] pDym;
    }

    so the part above is done, it works like a charm but now what the work is asking


    • If the user continues to enter values that would go beyond the end of the array then the program should respond by dynamically allocating a new array of size 2N and populating it with the values already in the original array. The user may then continue to enter values. If they go beyond the end of the new array then again a new array should be allocated (you chose what size that new array should be).

    but my code will not allow them to go past the limit anyway because my for loop will end once the array is full, any ideas what should i do???

    i am thinking of just adding a switch for user to choose if he wants to expand the array :S
    Last edited by Mariusmssj; January 19th, 2010 at 12:58 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Dynamic array help needed

    Quote Originally Posted by Mariusmssj View Post
    but my code will not allow them to go past the limit anyway because my for loop will end once the array is full, any ideas what should i do???
    So how does the user say "I'm finished entering values"?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Dynamic array help needed

    Quote Originally Posted by Paul McKenzie View Post
    So how does the user say "I'm finished entering values"?

    Regards,

    Paul McKenzie
    he chooses how many he wants to enter at the beginning, but yeah i do see your point. Any advice because i really can't think of anything :S

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Dynamic array help needed

    The actual resizing of the array is fairly trivial. The confusing part, then, is knowing when the user is done entering values.

    I would suggest emailing your teacher with the question. Odds are, it'll be something like "stop when a -1 is read" or something.

  5. #5
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Dynamic array help needed

    Quote Originally Posted by Mariusmssj View Post
    Any advice because i really can't think of anything :S
    You could use a loop like this
    Code:
    #include <iostream>
    
    int main() {
        int tmp; 
        while ( std::cin >> tmp ) {
            
        }
    }
    the loop will terminate if the user inputs EOF ( Ctrl+D on unix or Ctrl+Z on windows ) or any non int input.
    Kurt

  6. #6
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Dynamic array help needed

    Quote Originally Posted by Lindley View Post
    The actual resizing of the array is fairly trivial. The confusing part, then, is knowing when the user is done entering values.

    I would suggest emailing your teacher with the question. Odds are, it'll be something like "stop when a -1 is read" or something.
    i did email him because this is a really odd question

    Quote Originally Posted by ZuK View Post
    You could use a loop like this
    Code:
    #include <iostream>
    
    int main() {
        int tmp; 
        while ( std::cin >> tmp ) {
            
        }
    }

    the loop will terminate if the user inputs EOF ( Ctrl+D on unix or Ctrl+Z on windows ) or any non int input.
    Kurt
    hmm i can't use anything which i haven't been taught and i defo having been taught that
    Last edited by Mariusmssj; January 19th, 2010 at 01:58 PM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Dynamic array help needed

    Quote Originally Posted by Mariusmssj View Post
    hmm i can't use anything which i haven't been taught and i defo having been taught that
    Here's the problem: We don't know what you were taught, what you can or cannot use, etc. So take the answers and responses given to you as they come.

    Anyway, not using what you were not taught is a restrictive, if not stupid way of teaching programming. So the teacher expects you to remember every single word mentioned in the classroom, every single function ever used, etc. and whatever was never spoken, read about, etc.. can't be used? Expect to go nuts trying to keep everything straight.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 19th, 2010 at 02:42 PM.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Dynamic array help needed

    There's also the fact that there's no possible way the teacher could tell you about every single function. Sooner or later, you'll be expected to just break out Google and figure things out on your own.

  9. #9
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Dynamic array help needed

    Quote Originally Posted by Lindley View Post
    There's also the fact that there's no possible way the teacher could tell you about every single function. Sooner or later, you'll be expected to just break out Google and figure things out on your own.
    atm he wants to use only stuff he taught xD

  10. #10
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Dynamic array help needed

    Well it could be your job to be creative on this. There are a number of ways it could be done. The user enters random numbers and then they are added to the array which the user had originally commanded the size for. Perhaps you need one more outer loop such as a do..while. When finished with the filling of original data ask the user if they want to add more data. If yes, repeat the whole process and ask for a new size or a number of elements to add to the original array. It's kind of a silly programming exercise with no real world value anyway so I wouldn't worry about trying to be realistic. It's just designed to force you to think about how you would resize a dynamic array. As long as the program meets the requirements it shouldn't matter what you decide to ask the user or how you structure the additional functionality. You just have to prove that you are capable of creating and resizing a dynamic array.

    C++ is too dynamic to sit around waiting for the instructor to guide you in every possible way. What was earlier suggested was fairly simple and I don't see you could be faulted for using information from a book or website to complete the exercise. You're in college now aren't you? You are an adult and you are expected to use your mind creatively and to its fullest extent. If the teacher punishes you for that then you need to have a conversation with the dean. I had an exercise once that deal with stream IO. I was having some problems, needed guidance, and the teacher clearly didn't have a clue how to help or what guidance to give me. You really do need to read all you can with regards to the IO streams and learn some things on your own.
    Last edited by kempofighter; January 19th, 2010 at 03:14 PM.

  11. #11
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Dynamic array help needed

    he emailed me back:

    The user may choose to ask for an array of say 10 elements. One way you could do this is to let the user now keep entering numbers until they enter a terminating value say -999. So the can keep entering numbers until they enter -999 to stop. Or you could ask them after each number - do you want to enter another number?

    so i shall try to work by this idea of his

    i think i am doing it wrong

    Code:
    #include <iostream>
    
    int main()
    {
    	int* pDym = NULL;
    	int n = 0;
    	int n2 = 0;
    	int i = 0;
    	std::cout<< "Enter the size of the array: ";
    	std::cin>> n;
    	pDym = new int[n];
    
    
    	while(pDym[i]!= -999)
    	{
    		std::cin>> pDym[i];
    		i++;
    	}
    
    	for(int i = 0; i<n; i++)
    	{
    		std::cout<< " " << pDym[i] << " ";	
    	}
    
    	delete [] pDym;
    
    }
    any ideas???
    Last edited by Mariusmssj; January 20th, 2010 at 03:26 AM.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Dynamic array help needed

    Quote Originally Posted by Mariusmssj View Post
    he emailed me back:

    The user may choose to ask for an array of say 10 elements. One way you could do this is to let the user now keep entering numbers until they enter a terminating value say -999. So the can keep entering numbers until they enter -999 to stop. Or you could ask them after each number - do you want to enter another number?

    so i shall try to work by this idea of his
    The code to do this is a little more sophisticated than just allocating an array of n elements.

    You need to declare a variable that keeps track of the maximum size of the array, and another that keeps track of the number of elements that have been entered. If entering a new number reaches the maximum size of the array, you expand the array to the new size, and set the maximum size to the new size and stick the number into the array.

    I think you would be better served to first write the code that expands the array to a certain size. That is the important part of this assignment you haven't coded yet, and you can't avoid it. Might as well get that done first before thinking about anything else.

    That means that you have to create a brand new array of the new size, copy the elements of the old array to the old array, delete the old array, and assign the new array to the pointer you were using (in your case, pDym).

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Dynamic array help needed

    Quote Originally Posted by Paul McKenzie View Post
    The code to do this is a little more sophisticated than just allocating an array of n elements.

    You need to declare a variable that keeps track of the maximum size of the array, and another that keeps track of the number of elements that have been entered. If entering a new number reaches the maximum size of the array, you expand the array to the new size, and set the maximum size to the new size and stick the number into the array.

    I think you would be better served to first write the code that expands the array to a certain size. That is the important part of this assignment you haven't coded yet, and you can't avoid it. Might as well get that done first before thinking about anything else.

    That means that you have to create a brand new array of the new size, copy the elements of the old array to the old array, delete the old array, and assign the new array to the pointer you were using (in your case, pDym).

    Regards,

    Paul McKenzie
    aa well i do understand what you said but i am having problems with making it happen. Now the new n2 array, do a i need a new pointer for it?? bit confused i must say

    Code:
    #include <iostream>
    
    int main()
    {
    	int* pDym = NULL;
    	int n = 0;
    	int n2 = 0;
    	int i = 0;
    	int input = 0;
    	std::cout<< "Enter the size of the array: ";
    	std::cin>> n;
    	pDym = new int[n];
    
    
    	while(input != -999)
    	{
    		std::cin>> input;
    		pDym[i] = input;
    		i++;
    		if( n == i )
    		{
    			n2 = n;
    			n2++;
    			new int[n2];
    			for(j = 0; j<i; j++)
    			{
    
    
    	}
    
    	for(int j = 0; j<i; j++)
    	{
    		std::cout<< " " << pDym[j] << " ";	
    	}
    
    	delete [] pDym;
    
    }

  14. #14
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Dynamic array help needed

    Quote Originally Posted by Mariusmssj View Post
    aa well i do understand what you said but i am having problems with making it happen. Now the new n2 array, do a i need a new pointer for it?? bit confused i must say
    You would do better if you wrote a function to do this, and not try to stick everything into a for loop.
    Code:
    void resizeArray(int *& oldArray, int oldSize, int newSize)
    {
        int * newArray  = new int [newSize];
        //  copy oldArray contents to newArray
        // delete oldArray
        oldArray = newArray;
    }
    
    int main()
    {
         int *pDym = new int [10];
        //... test
        resizeArray(pDym, 10, 20);  // resize to 20
        //...
        delete [] pDym;
    }
    Once you have that working, then you have a function that resizes the array.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Dynamic array help needed

    Quote Originally Posted by Paul McKenzie View Post
    You would do better if you wrote a function to do this, and not try to stick everything into a for loop.

    Once you have that working, then you have a function that resizes the array.

    Regards,

    Paul McKenzie
    this is my 1st attempt and it's failing how do you pass the pointer array to a function?? because that's what went wrong for me i think :S

    Code:
    #include <iostream>
    
    void resize_array(int *oldarray[], int oldSize, int newSize)
    
    int main()
    {
    	
    	int n = 0;
    	int n2 = 0;
    	int i = 0;
    	std::cout<< "Enter the size of the array: ";
    	std::cin>> n;
    	int *pDym = new int[n];
    
    	while(pDym[i]!= -999)
    	{
    		std::cin>> pDym[i];
    		i++;
    	
    	if( n == i)
    	{
    		std::cin>> n2;
    		resize_array(&pDym, n, n2);
    	}
    	}
    
    }
    
    void resize_array (int *oldArray[], int oldSize, int newSize)
    {
    	int *pNewArray = new int[newSize];
    	for(int i = 0; i<oldSize; i++)
    	{
    		pNewArray[i] = *oldArray[i];
    	}
    	delete [] *oldArray;
    
    	for(int i = 0; i<oldSize; i++)
    	{
    	*oldArray[i] = *pNewArray[i];
    	}
    }
    Last edited by Mariusmssj; January 20th, 2010 at 11:35 AM.

Page 1 of 3 123 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center