944,001 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5154
  • C++ RSS
Sep 30th, 2007
0

Re: get length of a dynamic array

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dha_King is offline Offline
13 posts
since Sep 2007
Sep 30th, 2007
0

Re: get length of a dynamic array

It would be better starting a new thread for this. It is also good netiquette.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 30th, 2007
0

Re: get length of a dynamic array

I dunno. It's always good to resurect 2 year old threads!
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Sep 30th, 2007
0

Re: how to expand arrays

>>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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 1st, 2007
0

Re: how to expand arrays

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dha_King is offline Offline
13 posts
since Sep 2007
Oct 1st, 2007
0

Re: how to expand arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 1st, 2007
0

Re: how to expand arrays

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Assistance needed with craps game
Next Thread in C++ Forum Timeline: Determining max and min





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC