[英]C program not functioning as expected when trying to iterate over multidimensional array to fill the rest of the array with calculated table values
在下面的代碼中,所有變量( startV
, endV
, volumeStep
, tempStep
)都是用戶之前在代碼中預先定義的雙精度數。 變量rowNum
是一個整數。 程序似乎在達到Table[j][i] = (nR*(Table[i][0]))/(Table[0][j]);
時中斷Table[j][i] = (nR*(Table[i][0]))/(Table[0][j]);
並嘗試使用數組中已有的行和列來計算和分配值。 我無法弄清楚出了什么問題。 感謝提前的幫助:)哦,是的,數組表初始化為Table[rowNum][9];
for (int i = 1;i<=8;i++) //Fills the Volume COLUMNS axis of the table [0][v]
{
Table[0][i] = startV + (volumeStep*(i-1));
}
for (int i = 1;i<=rowNum;i++) //Fills the Temperature ROWS axis of the table [t][0]
{
Table[i][0] = startT + (tempStep*(i-1));
}
nR = moles*8.314;
for (int i = 1; i<=rowNum;i++)
{
for (int j = 1; j<=8; j++)
{
Table[j][i] = (nR*(Table[i][0]))/(Table[0][j]);
printf("%lf", Table[i][j]);
}
}
例如,c中的數組從0
到N - 1
索引
for (int i = 1 ; i <= rowNum ; i++)
應該
for (int i = 0 ; i < rowNum ; i++)
所以當你到達i == rowNum
你的程序會調用未定義的行為
或許你應該將Table
聲明為
double Table[rowNum + 1][9];
哦是的,數組表初始化為Table [rowNum] [9];
這意味着:
Table[j][i] = (nR*(Table[i][0]))/(Table[0][j]);
是錯的。 你的意思是Table[j][i]
,你將i
從1
循環到rowNum
,你的j
從1
循環到8
。
此外,如果您的第一個數組具有rowNum
元素,則只能循環到rowNum - 1
(包括最后一個元素)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.