| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1311 人关注过本帖
标题:[求助]关于数组初始化的烦恼~~
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
关于动态开辟 二维空间, 你可以去看我在我的博客里写的一篇文章:
http://blog.bc-cn.net/user1/121/archives/2006/2172.shtml

下面给一个动态开辟一维空间的例子:

#include <iostream>
using namespace std;

int main()
{
int n;
int * p; // to receive the addresse, where a dynamic array allocated
// so that you can work with this dynamic array
// pay attention: here is one dimension array
cout<<"Please enter the number of elements, that you want an one dimension array hat: ";
if(cin>>n)
{
p = new int[n];
// now you have allocated the space for your one dimension array
// p is here the first address of this array
// you can write someting there
// and make some changement
// or display them.

// for example I write something in this array
for(int i = 0; i<n; i++)
{
p[i] = i;
}

// and maybe some statements will be here
// --- some code ---

// and now some code to change this array
// for example inverse
for(int i = 0; i<(n>>1); i++)
{
int temp = p[i];
p[i] = p[n-i-1];
p[n-i-1] = temp;
}

// now you maybe want show this array
for(int i = 0; i<n; i++)
{
cout<<p[i]<<" ";
}
}

// you have done everything with your program, now you want leave.
// Caution: Never forget to free the dynamic allocation
delete [] p;

system("pause");
return 0;
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-11-12 22:58
qijiang
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-11-12
收藏
得分:0 
...
if(cin>>n)
...
for(int i = 0; i<(n>>1); i++)
...
这俩个是什么意思?cin>>n怎么返回布尔型吗?i<(n>>1)这个也不懂啊
2006-11-13 14:10
上大阿Ben
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-11-11
收藏
得分:0 

谢谢大家,我得到了一个不错的答案并且上机试验过了,效果不错:
//动态构造并初始化二维数组"array"
void build(void)
{
double **array,size;
label:cout<<"请输入行列式的阶\n";
cin>>size;
if(size>=1&&(int)size==size)
{
array = (double**)malloc(sizeo(doublef*)*size);
for( int i=0; i<size; i++ )
{
array[i] = (double*)malloc(sizeof(double)*size);
}
for( int a=0;a<size;a++)
for(int b=0;b<size;b++)
{
cout<<"请输入第"<<a+1<<"行第"<<b+1<<"列的数\n";
cin>>array[a][b];
}
}
else
{
cout<<"\a输入错误,请重新输入\n";
goto label;
}
}
但是其中两句有些不明白:
array = (double**)malloc(sizeo(doublef*)*size);
array[i] = (double*)malloc(sizeof(double)*size);
哪位高手能解释得详细一些呢?
还有,为什么第二句要用循环语句而第一句不用呢?
麻烦解释一下我真得很想弄明白谢谢!!

2006-11-19 15:18
dick_zq007
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-12-5
收藏
得分:0 

这两句应该是申请内存单元的命令吧,用来申请一个动态的空间。

第一句应该是给怎个数组申请空间吧,第二句就应该是给每个成员申请空间。

我的理解是这样的,还请高手指教!!


-------------------不再浪费一秒钟---------------------
2006-12-06 12:53
快速回复:[求助]关于数组初始化的烦恼~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018212 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved