[問題] 動態配置亂數大小的二維陣列
一開始犯了新手常犯的錯誤T_T,使用變數常數去設定陣列大小
int row,clu,clu_t;
srand(time(NULL));
row=(rand()%10)+1;
clu=(rand()%10)+1;
clu_t=(rand()%10)+1;
int a[row][clu],b[clu][clu_t],mul[row][clu_t];
因為我想要亂數產生二維陣列,而且其大小也是亂數產生的
雖然明知道這樣可能有問題,但是還是想試試XD後來很神奇的
發現在dev c++下竟然可以compiler,而且做相乘還是正確的
不過這樣的寫法在visual c下就不給過了,所以想請問如果我使用
動態配置(如下)
int **p,**q,row,clu,clu_t;
srand(time(NULL));
row=rand()%10+1;
clu=rand()%10+1;
clu_t= rand()%10+1;
cout<<"A矩陣的列為"<<row<<"行為"<<clu<<endl;
cout<<"B矩陣的列為"<<clu<<"行為"<<clu_t<<endl;
cout<<"***A矩陣為***"<<endl;
p=new int *[row];
for(int i=0;i<row;i++)
{
p[i]=new int [row];
for(int j=0;j<clu;j++)
{
p[i][j]=rand()%10;
cout<<p[i][j]<<" ";
}
cout<<endl;
}
cout<<"***B矩陣為***"<<endl;
q=new int *[clu];
for(int i=0;i<clu;i++)
{
q[i]=new int [clu];
for(int j=0;j<clu_t;j++)
{
q[i][j]=rand()%10;
cout<<q[i][j]<<" ";
}
cout<<endl;
}
若我要對其做矩陣相乘的運算,還要再new一塊記憶體空間嗎?
謝謝大家的指教,程式新手真的是看一本書不夠,要看很多本
也要問很多人才能進步阿T_T
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.194.107.153
推
04/23 01:10, , 1F
04/23 01:10, 1F
推
04/23 01:12, , 2F
04/23 01:12, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章