使用动态数组

1、定义一个指针类型

如:typedef   double*   DoubleArrayPtr;

2、声明一个指针变量

如: DoubleArrayPtr   a;

3、调用new

a =  new   double[ array_size ];

4:、动态数组的长度在方括号中给出

5、像普通数组那样使用

6、调用delete [ ]

如:delete  [ ]   a;

 

// 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)
	///创建动态数组
	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 ;
	 //选择排序
	///将数组降序排序
	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;
			}
		}
}


 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值