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. :)