Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to resize an array

Status
Not open for further replies.

pigsie

Programmer
Dec 19, 2000
307
GB
Hi

How can i resize an array is it possible? Do i need to make a new array with a differemt size and copy the old array into the new one ? thanks
 
Well,you can create a dynamic array and gives it any size that you want while the program is running.
Here is an example of it.

#include <iostream.h>
void main()
{
float *array;
int size;
cout<<&quot;\t\tStoring data in an array.&quot;<<endl<<endl;
cout<<&quot;How long will be your array ?&quot;<<endl;
cin>>size;
array = new float[size]; // allocating memory to the array
for( int i = 0; i < size; i++ )
{
cout<<&quot;array[&quot;<<i<<&quot;] = &quot;;
cin>>array;
}
cout<<&quot;This new array have been created !&quot;<<endl;
cout<<&quot;array[&quot;<<size<<&quot;] = {&quot;;
for( i = 0; i < size; i++ )
{
cout<<array;
if( i < size - 1 )
cout<<&quot;,&quot;;
}
cout<<&quot;} &quot;;
delete array; // freeing memory
}
 
Also look into CArray... It has an Add method allowing it to be dynamic.

Matt
 
Thanks for the replies:

what is CArray by the way and where do I find it?
 
It is part of the microsoft foundation classes. MSDN help files have it.

CArray<type,type> my_array;


Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top