[英]exc_bad_access , xcode, C
I'm still new with C language and i don't really have background knowledge in it. 我还是C语言的新手,但是我真的没有背景知识。 i was using fabs function and then i got the EXC_BAD_ACCESS. 我使用fabs函数,然后得到EXC_BAD_ACCESS。 can anyone help me ? 谁能帮我 ?
#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);
}
The for loop that starts with for(i=1;1<=node;i++)
is most likely not what you want. 以for(i=1;1<=node;i++)
开头的for循环很可能不是您想要的。 You probably want for(i=1;i<=node;i++)
. 您可能想要for(i=1;i<=node;i++)
。 i
looks like 1
, but they're certainly not the same! i
看起来像1
,但它们肯定不一样!
i
is increasing beyond the bounds of resid
and that's why you're getting EXC_BAD_ACCESS. i
越来越超越边界resid
,这就是为什么你得到EXC_BAD_ACCESS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.