0
我對C語言還是一個新東西,我並沒有真正的背景知識。我使用晶圓廠功能,然後我得到了EXC_BAD_ACCESS。誰能幫我 ?exc_bad_access,xcode,C
#include <stdio.h>
#include <math.h>
// declare all the variables needed
int a, b, d, i, j, column, row, node, counter1, counter2, V0;
double volt, C, h, resi, odd, even, alpha, simpson;
double V[1000][1000], resid[1000][1000], dif_V[1000]; //1000 is chosen to get better value
int main(void)
{
// insert code here...
// obtaining values of variables
V0 = 100;
printf("The potential V0 may be taken as 100\n");
printf("Enter the values for dimension\n");
printf("1.Width of capacitor, a=");
scanf("%d", &a);
printf("\n 2.Height of Capacitor, b=");
scanf("%d", &b);
printf("\n 3.Length of middle plate, d=");
scanf("%d", &d);
printf("\n 4.Length of square mesh grid, h=");
scanf("%lf", &h);
printf("\n 5.Value of the relaxation factor, alpha=");
scanf("%lf", &alpha);
column= a/(2*h)+1; //calculate the number of columns
row= b/(2*h)+1; //calculate number of rows
node= d/(2*h)+1; //calculate number of nodes
counter1=1;
counter2=1;
//counter1 and counter2 allows the data to be printed for each 10 scans
//define boundary conditions
//all values are set to zero
for(i=1;i<=column;i++)
{
for(j=1; j<=column;j++)
V[i][j] = 0;
}
//set middle nodes to 100 Volts
for(i=1;i<=node;i++)
V[i][j] = V0;
printf("\n No of scans\t");
printf("Potential at the middle\t\t");
printf("Maximum residual\n");
do
{
//calculation of potential for every node using the 5 points formula
resi=0;
for (i=1;i<=node;i++)
{
for(j=2;j<row;j++)
{
if(i==1)
{
V[i-1][j]=V[i+1][j];
//apply the 'fictitious' nodes formula for nodes at boundary
}
volt=V[i][j];
//stores previous value of voltage in 'volt'
V[i][j]=0.25*(V[i-1][j] + V[i][j-1] + V[i][j+1] + V[i+1][j]);
//apply 5 points formula
resid[i][j]= V[i][j]-volt;
//calculate the residual
V[i][j]= volt + alpha*resid[i][j];
//applying SOR scheme
}
}
for(i=node+1; i<column;i++)
{
for(j=1; j<row;j++)
{
if(j==1)
{
V[i-1][j]=V[i+1][j];
}
volt=V[i][j];
//stores previous value of voltage in 'volt
V[i][j]=0.25*(V[i-1][j] + V[i][j-1] + V[i][j+1] + V[i+1][j]);
//apply 5 points formula
resid[i][j]= V[i][j]-volt;
//calculate the residual
V[i][j]= volt + alpha*resid[i][j];
//applying SOR scheme
}
}
//compare the residuals
//the fabs functions compute the absolute value of a floating point number
for(i=1;1<=node;i++)
{
for(j=2;j<row;j++)
{
if(fabs(resid[i][j])>resi)
{
resi=fabs(resid[i][j]);
}
}
}
for(i=node+1;i<column;i++)
{
for(j=2;j<row;j++)
{
if(fabs(resid[i][j])>resi)
{
resi=fabs(resid[i][j]);
}
}
}
counter1+=1;
counter2+=1;
if(counter2==100)
{
printf("\t %3d\t\t",counter1);
printf("%lf\t\t\t", V[(column-1)/2][(row-1)/2]);
printf("%lf\n", resi);
counter2=0;
}
}
while(resi>0.0001 != counter1>(2*column*row));
printf("Number of scans = %d\n", counter1);
//calculation of dv/dy
for (i=1;i<=node;i++)
dif_V[i]= (4*V[i][2] - 3*V[i][1] - V[i][3])/(2*h);
odd = 0;
even = 0;
for(i=3; i<=node; i=i+2)
odd= odd + dif_V[i];
for(i=2;i<=node;i=i+2)
even= even + dif_V[i];
simpson= (h/3)*(dif_V[1] + dif_V[node] + 4*even +2*odd);
C=(-0.04)*simpson;
printf("The capacitance per unit length = %lf", C);
}
索引以0開頭,而不是1.將所有循環改爲'for(int i = 0; i
rmaddy