c++,定义数组,并通过键盘输入数组的值,怎么做??
来源:百度知道 编辑:UC知道 时间:2024/06/09 13:38:55
下面说明动态分配数组。
#include <iostream>
using namespace std;
int main()
{
int *a;
int i,n;
cout<<"please enter array size:" <<endl ;
cin >> n; // 数组大小
a = (int *) malloc(sizeof(int) * n); // 分配
cout<<"please enter " << n << " numbers:" <<endl ;
for (i=0;i<n;i++)
{
cin >> a[i]; // 键盘输入 n 个数
}
for (i=0;i<n;i++) cout << a[i] << " ";
cout << endl;
return 0;
}
输入例子:
please enter array size:
5
please enter 5 numbers:
12 34 56 888 999
int* p= new int[5];
循环
cin>>p[n];