作者rockken ()
看板C_and_CPP
標題[問題] 關於抓取數列
時間Tue Mar 10 16:23:19 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Dev-C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
NO 只有基本的 include <stdio.h>
問題(Question):
抓陣列取值有時會出錯
餵入的資料(Input):
預期的正確結果(Expected Output):
如圖。陣列值都是一。
錯誤結果(Wrong Output):
可是當N 變大時等於60, 他會出現 2292658 這種怪數字。
可是 N=99 又沒問題。 不知道為什麼,求大師解救。
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#define N 60
int main(){
int score1[N],score2[N];
int i,j,s=0,ds=0;
// 輸入兩個長度100的數列
for (i=1;i<=N;i++){
score1[i]=1,score2[i]=1 ;
printf("%6d\n",score1[i]);
printf("%6d\n",score2[i]);
printf("%6s\n","--");
}
int ks=3;
int kk= N/ks;
printf("N/k=%d\n",kk);
printf("%6s\n","--");
int k,s0[kk],s2=0;
for (j=0;j<kk;j++){
s0[j+1]=0 ;
for(k=1;k<=ks;k++)
{
s0[j+1]= s0[j+1]+ score1[j*ks+k]*score2[j*ks+k];
printf("%d\n",j*ks+k);
printf("%d\n",score1[j*ks+k]);
printf("%d\n",score2[j*ks+k]);
printf("%6s\n","--");
}
//printf("s0=%d\n", s0[j+1]);
//s2=s2+ s0[j];
//printf("%d\n",score1[j]);
//printf("%6d\n",score2[i]);
}
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.174.242.175
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1425975802.A.B5C.html
→ wenyonba: 陣列長度為N,Index是從0~N-1喔,不是1~N 03/10 16:48
→ KawasumiMai: i=0;i<n 應該是寫陣列的習慣了= = 03/10 16:58
→ rockken: 謝謝兩位,太久沒寫C 。 以為跟R 一樣 S[0] 沒有定義 03/10 17:17
→ rockken: 以解決 3Q 03/10 17:17