exc_bad_access,xcode,C

时间:2013-11-05 03:13:14

标签: c xcode

我还是C语言的新手,我真的没有背景知识。我正在使用fabs功能然后我得到了EXC_BAD_ACCESS。有谁可以帮助我?

#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);

}

1 个答案:

答案 0 :(得分:0)

for(i=1;1<=node;i++)开头的for循环很可能不是您想要的。你可能想要for(i=1;i<=node;i++)i看起来像1,但他们肯定不一样!

i越来越超出resid的界限,这就是您获得EXC_BAD_ACCESS的原因。