源程序:
//3.编写一个程序,该程序建立一个动态数组,为动态数组的元素赋值,显示动态数组的值并删除动态数组。
#include <iostream>
using namespace std;
void main()
{
int i, n, temp = 0;
cout << "输入数组大小:";
cin >> n;
double *array = new double[n]; //用指针,动态申请数组大小
cout << "给每个数组元素赋值:" << endl;
for (i = 0; i < n; i++)
{
cout << "array[" << i << "] = ";
cin >> temp;
*(array + i) = temp;//给数组元素赋值
}
cout << "动态数组个元素的值如下:" << endl;
for (i = 0; i < n; i++)
{
cout << "array[" << i << "] = " << array[i] << endl;//打印数组元素
}
delete[] array;//释放内存
system("pause");
}
运行结果: