作者obelisk0114 (追風箏的孩子)
看板C_and_CPP
標題[問題] 賦值給動態一維陣列
時間Wed Oct 12 23:41:10 2011
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++2008
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
兩個長度n儲存整數的array a[i],b[i]
返回c[i]=a[i]*b[i],i=0,1...,n-1
餵入的資料(Input):
由鍵盤輸入n值,再分別輸入array a[i],b[i]
預期的正確結果(Expected Output):
返回c[i]=a[i]*b[i],i=0,1...,n-1
錯誤結果(Wrong Output):
鍵盤輸入的a[i],b[i]好像沒成功存入
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
int main()
{
int *a=NULL,*b=NULL,*c=NULL;
int m,i;
cout<<"Please input the dimension:";
cin>>m;
a=new int[m];
b=new int[m];
c=new int[m];
cout<<"Please input the vector:";
for(i=0;i<m;i++) cin>>a[i];
cout<<"Please input the other vector:";
for(i=0;i<m;i++) cin>>b[i];
for(i=0;i<m;i++)
c[i]=a[i]*b[i];
cout<<c[i];
return 0;
}
補充說明(Supplement):
--
肝不好 ▁▁ ● ◤
肝若好
人生是黑白的 ▏ ◤
考卷是空白的
▏ ◤
、 ﹐
● ●b 囧 ▎ ●> ● ◤
▌ ﹍﹍ 0 ▊囧>
幹...
▲ ■┘ ■ ▎ ■ █◤ ▌ ㄏ▋ ︶■
〈﹀ ∥ ▁▁∥ ▎ ﹀〉◤
▋ ▊ 〈\
ψcockroach727
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.245.27
→ james732:你最後一行 cout<<c[i]; 是不是應該用個迴圈包起來? 10/12 23:42
→ firejox:...................................................... 10/12 23:42
→ james732:for (i=0;i<m;i++) cout << c[i]; 10/12 23:42
→ loveme00835:把 i 放在 for init stmt 就不會有這問題了... 10/12 23:44
→ loveme00835:沒用空白行隔開, 把迴圈縮成一行, 都容易讓人糊塗 10/12 23:45
→ obelisk0114:成功了,感謝各位幫忙 10/12 23:47