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

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

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

// 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.

"

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

// Assignment 1, Question #1
#include <iostream.h>
#include<conio.h>


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 <strong class="highlight">array</strong>
} // 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

#include <iostream.h>
#include<conio.h>

void main()
{

int n,size;
clrscr();

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

int *array = new int;

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

for(int m=0;m<size-1;m++)
{
for(int n=m+1;n<size;n++)
{
if(array[m]>array[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..

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.