[問題] array的迭代迴圈
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEV C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
#include <stdio.h>
#include <stdlib.h>
#define max 40 /* number of grid points */
main()
{
double pin[max][max]={0};
int i, j, iter;
double pout[max][max]={0};
FILE *output; /* save data in laplace.dat */
int a;
a=0;
output = fopen("laplace.txt","w");
for(i=0; i<max; i++)
{
pin[i][3] = 100.0; /* p[i][3] = 100 V */
}
printf("%f %f\n",pin[0][3],pout[0][3]);
for(iter=0; iter<3; iter++) /* iterations */
{
for(i=0; i<max; i++) /* x-direction */
{
for(j=0; j<max; j++) /* y-direction */
{
if(a=0)
{pout[i][j] = pin[i][j];}
if(a=1)
{pin[i][j] = pout[i][j];}
}
}
printf("%f %f\n",pin[0][3],pout[0][3]);
if(a=0)
{a=1;}
if(a=1)
{a=0;}
}
system("PAUSE");
return 0;
}
我用pin[40][40]和pout[40][40]兩個array用for loop迭代
執行3次 用一個判斷式 令a=0 每個迴圈如果a=0就用pin迭代出pout
然後把a變成1
當跑下個迴圈時 遇到另一個判斷式:如果a=1 就用pout迭代得出pin
再把a變成0
我做了兩個printf去測試這個程式 第一個在迴圈前 第二個在迴圈內
我去印pin[0][3]和pout[0][3] 發現迴圈前印出來的值就是我預設的值
迴圈內的值卻全部變成0
另外我也有檢查a a在迴圈中竟然根本不會改變其值 和我預期的
一次0 一次1...完全不一樣
這是怎麼回事?
要如何讓我的迴圈迭代正確?
(PS:我想讓一個array經過迴圈不斷用舊的array迭代成新的array
模擬一個物理量隨時間演變 才用pin和pout互相跌代的方法)
餵入的資料(Input):
預期的正確結果(Expected Output):
100.000000 0.000000
100.000000 100.000000
100.000000 100.000000
100.000000 100.000000
錯誤結果(Wrong Output):
100.000000 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 58.114.190.180
推
01/24 20:45, , 1F
01/24 20:45, 1F
→
01/24 20:46, , 2F
01/24 20:46, 2F
→
01/24 20:47, , 3F
01/24 20:47, 3F
→
01/24 20:47, , 4F
01/24 20:47, 4F
→
01/24 20:48, , 5F
01/24 20:48, 5F
→
01/24 20:58, , 6F
01/24 20:58, 6F
→
01/24 21:02, , 7F
01/24 21:02, 7F
→
01/24 21:08, , 8F
01/24 21:08, 8F
→
01/24 21:46, , 9F
01/24 21:46, 9F
→
02/01 01:53, , 10F
02/01 01:53, 10F