Use Dynamic Array

Source: Internet
Author: User

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;
}
}
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.