1. Define a pointer type
For example, typedef double * DoubleArrayPtr;
2. Declare a pointer variable
For example, DoubleArrayPtr;
3. Call new
A = new double [array_size];
4: the length of the dynamic array is given in square brackets.
5. Use it like a normal Array
6. Call delete []
For example, delete [];
[Cpp]
// String. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <cstdlib>
# Include <cstddef> ///******
Typedef int * IntArrayPtr; // (1)
Void fill_array (int a [], int size );
Void sort (int a [], int size );
Int main (int argc, char * argv [])
{
Using namespace std;
Cout <"This program sorts numbers from highest to lowest. \ n ";
Int array_size;
Cout <"How many numbers will be sorted? \ N ";
Cin> array_size;
IntArrayPtr a; // (2)
/// Create a dynamic array
A = new int [array_size]; // (3)
Fill_array (a, array_size );
Sort (a, array_size );
Cout <"In sorted order the numbers are: \ n ";
For (int index = 0; index <array_size; index ++)
{
Cout <a [index] <"";
}
Cout <endl;
Delete [] a; // (5)
Return 0;
}
Void fill_array (int a [], int size)
{
Using namespace std;
Cout <"Enter" <size <"integers. \ n ";
For (int index = 0; index <size; index ++)
Cin> a [index];
}
Void sort (int a [], int size)
{
Int I, j;
// Select sorting
/// Sort arrays in descending order
For (I = 0; I <size-1; I ++)
For (j = I + 1; j <size; j ++)
{
If (a [I] <a [j])
{
Int temp = a [I];
A [I] = a [j];
A [j] = temp;
}
}
}