一个简单的C 数组问题

[复制链接]
查看11 | 回复1 | 2011-4-15 12:41:20 | 显示全部楼层 |阅读模式
#includeiostream
#includectime
using namespace std;
int*A;
int n;
int main()
{
cout\"please input n !\"endl;
cinn;
for(int i=0;in;i)
{
A=rand()%2;

cout\"A=\"Aendl;

}
}
这个应该怎么改呀,使数组的长度可以为任意的正整数??
回复

使用道具 举报

千问 | 2011-4-15 12:41:20 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">一维动态数组分配像这样: 分配一个有N个单元的 int 类型数组
int *f=new int[N]
N值是可以通过输入的,下面是例子求平均数,不难理解
#includeiostream
using namespace std;
void main()
{
int N;
float s=0;
cout\"请输入数组长度:\";
cinN;

int *f=new int[N];//动态分配数组

cout\"请输入数组的数值:\";
for(int i=0;iN;i)
{

cinf;
s =f;
//输入数组中的数值,并把它们加起来
}
couts/Nendl; //总和除N 得平均数
}











参考资料:<span class=\"gray\"><a href=\"http://zhidao.baidu.com/question/151937853.html?an=0defds123si=6defds123wtp=wk\" target=\"_blank\">http://zhidao.baidu.com/question/151937853.html?an=0defds123si=6defds123wtp=wk</a></span>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行