954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic array

for a assignment i need to create an array dynamically

so then late i have to sort the array

but the problem is i an new in this cousrse and i dontknow how to creat an array dynamically

please help

any help will be a good start for me

\thx

d1e9v85
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

my friend said that i might have to use
new[]
and delete[]
but the problem is i havent studied them yet

d1e9v85
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
for a assignment i need to create an array dynamically so then late i have to sort the array but the problem is i an new in this cousrse and i dontknow how to creat an array dynamically please help any help will be a good start for me \thx


A dynamic array is just a pointer to memory returned by malloc() or new[]. You make one like this where T is some type and n is a positive integer.

// In C++
T *array = new T[n];

/* In C */
T *array = malloc( n * sizeof( T ) );

Then you have to deallocate the memory when you're done.

// In C++
delete [] array;

/* In C */
free( array );

Let's say I want a dynamic array of integers in C++. I would do this.

#include <iostream>

int main()
{
  using namespace std;

  int size = 10;
  int *array = new int[size];

  // Put something in the array
  for ( int i = 0; i < size; i++ )
    array[i] = i;

  // Make sure it got putted right :)
  for ( int i = 0; i < size; i++ )
    cout << array[i] << ' ';
  cout << '\n';

  delete [] array;

  return 0;
}

It's pretty simple, really. Besides the allocation and deallocation, using dynamic arrays is a lot like regular arrays. But if you have any problems at all, just meander over here and the smarty pants here at Daniweb can help you out. :)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 
// Assignment 1, Question #1
#include <iostream> 
#include<cstdlib>
#include <ctime>
using namespace std;
 
 
void Sort( int *array , int size );
void print( int *array , int size );
 
int main ()
{ 
int arraysize;
cout << "Please enter the size of your integer array: " << endl;
cin >> arraysize;
 
int *array = new int[arraysize];
 
 
for ( int i = 0; i< arraysize; i++ )
*(array + i ) = rand() % 101;
 
Sort( array, arraysize );
 
print( array, arraysize ); 
return 0;
}
void Sort( int *array, int size )
{
for ( int next = 1; next < size; next++ ) 
 
{ 
int insert = array[ next ]; // store the value in the current element 
 
int moveItem = next; // initialize location to place element 
 
// search for the location in which to put the current element 
while ( ( moveItem > 0 ) && ( array[ moveItem - 1 ] > insert ) ) 
{ 
// shift element one slot to the right 
array[ moveItem ] = array[ moveItem - 1 ]; 
moveItem--; 
} // end while 
 
array[ moveItem ] = insert; // place inserted element into the array
} // end for 
 
void print( int *array, int size )
{
//Original print out of array
cout << "The unsorted arangement of the array is:\n\n";
for ( int j = 0; j < size; ++j )
cout << array[ j ] << "\n" ;
cout << endl;
 
//Print out of listing 
cout << "The aranged array is:\n\n";
for ( i = 0; i < size; ++i )
cout << array[ i ] << "\n" ;
cout << endl; 
}



what am i doing wrong??
the compiler gives me 2 errors

"
: error C2601: 'print' : local function definitions are illegal

: fatal error C1004: unexpected end of file found

Error executing cl.exe.

"

d1e9v85
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

You're missing a closing brace '}' at the end of main().

Here:

void print( int *array, int size )
{
//Original print out of array
cout << "The unsorted arangement of the array is:\n\n";
for ( int j = 0; j < size; ++j )
cout << array[ j ] << "\n" ;
cout << endl;
 
//Print out of listing of cities in alphabetical order
cout << "The aranged array is:\n\n";
for ( i = 0; i < size; ++i )


You forgot to declare 'i' by placing 'int' before it.

I just tried running your program now, and it works pretty well - except for this:

for ( int i = 0; i< arraysize; i++ )
*(array + i ) = rand() % 101;
 
Sort( array, arraysize );
 
print( array, arraysize );

Do you see what you're doing wrong? You only made print() with one array as a parameter, and you try to print out 2 arrays here:

void print( int *array, int size )
{
//Original print out of array
cout << "The unsorted arangement of the array is:\n\n";
for ( int j = 0; j < size; ++j )
cout << array[ j ] << "\n" ;
cout << endl;
 
//Print out of listing of cities in alphabetical order
cout << "The aranged array is:\n\n";
for ( /* 888 */ int i = 0; i < size; ++i )
cout << array[ i ] << "\n" ;
cout << endl; 
}

So although your sort function works, you're either going to have to:Modify print() to accept to arrays as paramters, and print them out.
Modify print() to only print out one array, and then call print() twice.
Both would work, although I'd probably go with the first solution (faster).

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

// Assignment 1, Question #1
#include
#include


void sort(int *array , int size);
void print(int *array , int size);

void main ()
{
int arraysize;
cout << "Please enter the size of your integer array: " << endl;
cin >> arraysize;

int *array = new int[arraysize];

cout<<"Enter the values";
for ( int i = 0; i< arraysize; i++ )
cin>>*(array+i);

sort(array, arraysize );

print( array, arraysize );
getch();
}

void sort( int *array, int size )
{
for ( int next = 1; next < size; next++ )

{
int insert = array[ next ]; // store the value in the current element

int moveItem = next; // initialize location to place element

// search for the location in which to put the current element
while ( ( moveItem > 0 ) && ( array[ moveItem - 1 ] > insert ) )
{
// shift element one slot to the right
array[ moveItem ] = array[ moveItem - 1 ];
moveItem--;
} // end while

array[ moveItem ] = insert; // place inserted element into the array
} // end for
}
void print( int *array, int size )
{
int i;
//Original print out of array
cout << "The unsorted arangement of the array is:\n\n";
for ( int j = 0; j < size; ++j )
cout << array[ j ] << "\n" ;
cout << endl;

//Print out of listing
cout << "The aranged array is:\n\n";
for ( i = 0; i < size; ++i )
cout << array[ i ] << "\n" ;
cout << endl;

}

This is perfect program............ Enjoy.............This will run perfectly

shylesh_kerala
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

#include
#include

void main()
{

int n,size;
clrscr();

cout<<"Enter the length of array : ";
cin>>size;

int *array = new int[size];

cout<<"Enter elements : "<<"\n";
// Put something in the array
for ( int i = 0; i < size; i++ )
cin>>array[i];

for(int m=0;marray[n])
{
int temp;
temp=array[m];
array[m]=array[n];
array[n]=temp;
}
}
}


cout<<"Sorted Array"<<"\n";
// Make sure it got putted right :)
for ( int j= 0; j < size; j++ )
cout << array[j] << ' ';
cout << '\n';

delete [] array;
getch();
}

This is program to allocate & deallocate memory and sorting that array.............................................. 100% working..

shylesh_kerala
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Please use code tags and standard code when posting. Also please don't resurrect dead threads. You must have seen the warning before you posted.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You