how to expand arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 13
Reputation: Dha_King is an unknown quantity at this point 
Solved Threads: 0
Dha_King Dha_King is offline Offline
Newbie Poster

Re: get length of a dynamic array

 
0
  #1
Sep 30th, 2007
Hello sorry if i hijack this thread for my own purpose, first English isent my first language, my apologies for the misspelling and all.

I also have problem with dynamic arrays, our stupied teacher made the assingment harder then he hade to. I'll try to explain the assingment.

First we are supposed to use dynamic arrays, like

  1. int *list = new int;

And thats fine its nothing special about that, but he whants us to write a funktion that expands(s*) the dynamic array one integer at the time

  1.  
  2. void expandera_lista( int *&list , int n)
  3. {
  4. int *templist = new int[n];
  5. for(int i=0; i<=n;i++)
  6. {
  7. templist[i] = list[i];
  8. }
  9. delete[] list;
  10.  
  11. list = new int[n+1];
  12.  
  13. for(int i=0; i<=n;i++)
  14. {
  15. list[i] = templist[i];
  16. }
  17. delete[] templist;
  18.  
  19.  
  20. }

I think this code will work.

the problem is that i have to controll the size ( n ) of the array to make room for the next integer. I have tried

  1.  
  2. int n = (sizeof(*list)/sizeof(list[0]))

and that wont work it returns 1 att the time.

Does any one have a simpel ansver to my problem?
Last edited by Ancient Dragon; Sep 30th, 2007 at 6:54 pm. Reason: add line numbers to code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: get length of a dynamic array

 
0
  #2
Sep 30th, 2007
It would be better starting a new thread for this. It is also good netiquette.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: get length of a dynamic array

 
0
  #3
Sep 30th, 2007
I dunno. It's always good to resurect 2 year old threads!
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,469
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: how to expand arrays

 
0
  #4
Sep 30th, 2007
>>Hello sorry if i hijack this thread for my own purpose
Please don't do it again. I split your thread this time for you for free

Line 5 is incorrect. Arrays are numbered from 0 to, but not including, the value of N. You should use the < operator, not <= operator.

To solve your problem I think just pass the second parameter by reference and let expandera_lista() increment it. That way the calling function will always have an updated copy of the size of the array.

Your function is also doing too much copying. You only have to copy once.
  1. void expandera_lista( int *&list , int& n)
  2. {
  3. int *templist = new int[n+1];
  4. for(int i=0; i<n;i++)
  5. {
  6. templist[i] = list[i];
  7. }
  8. delete[] list;
  9.  
  10. list = templist;
  11. ++n;
  12. }
Last edited by Ancient Dragon; Sep 30th, 2007 at 7:02 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 13
Reputation: Dha_King is an unknown quantity at this point 
Solved Threads: 0
Dha_King Dha_King is offline Offline
Newbie Poster

Re: how to expand arrays

 
0
  #5
Oct 1st, 2007
Thank you ancient dragon for your answer, and your code exampel =). My problem is a bit diffrent that what u describe, i'll cut and paste the code so u can have a resonable chans to understand my problem. l read trough my last post and i dident give u a fair chans to understand my problem.

well the code dosent work as it is, but u get the general idea how its suppose to work. My problem is how to get the size of my array when its a dynamic array, the way i have tried dont work, it returns 1 allt the time.

ps our teacher still is an idiot=) ds or mybe its me.

  1.  
  2. // Laboration1.3.cpp : Defines the entry point for the console application.
  3. //DYNAMISKA FÄLT
  4.  
  5. #include "stdafx.h"
  6.  
  7.  
  8.  
  9. void fylla_lista(int *&list, int n, int tal)
  10. {
  11.  
  12.  
  13. list[n+1]=tal;
  14.  
  15.  
  16. }
  17. void expandera_lista( int *&list , int n)
  18. {
  19. int *templist = new int[n];
  20. for(int i=0; i<=n;i++)
  21. {
  22. templist[i] = list[i];
  23. }
  24. delete[] list;
  25.  
  26. list = new int[n+1];
  27.  
  28. for(int i=0; i<=n;i++)
  29. {
  30. list[i] = templist[i];
  31. }
  32. delete[] templist;
  33.  
  34.  
  35. }
  36. void printa_lista ( int *list , int n)
  37. {
  38.  
  39. for(int i=0; i<=n;i++)
  40. std::cout<<list[i]<<std::endl;
  41.  
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. int _tmain(int argc, _TCHAR* argv[])
  51. {
  52. char vanta;
  53. int *list = new int[1];
  54.  
  55.  
  56. int tal=0, n=0;
  57.  
  58.  
  59. std::cout<<"Skriv in ett tal at gangen och avsluta med CTRL-Z\n\n";
  60.  
  61. while(!std::cin.eof())
  62. {
  63.  
  64. std::cin>>tal;
  65. list[i++]=tal;
  66.  
  67. **the problem**
  68.  
  69. *n = (sizeof(*list)/sizeof(list[0])) ;*
  70.  
  71. expandera_lista(list,n);
  72. fylla_lista(list, n, tal);
  73.  
  74. }
  75.  
  76. printa_lista(list,n);
  77.  
  78. delete[] list;
  79.  
  80. std::cin>>vanta; // the DOSpromt vill stay activ
  81. return 0;
  82. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,750
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: how to expand arrays

 
0
  #6
Oct 1st, 2007
>My problem is how to get the size of my array when its a dynamic array
You allocated the array, you should know the size. In other words, there's no portable way to find the size of allocated memory given nothing but a pointer to it. You need to remember the size yourself and pass it around along with the pointer.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,469
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: how to expand arrays

 
0
  #7
Oct 1st, 2007
use a vector instead of a simple c-style array and you won't have that problem. c++ vector class has a method that returns the current size of the array.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC