开发者

Calculate Reverse Matrix?

Why should i get crash with this , where did i wrong !? :(

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

const volatile max=15;

int    read(float[][max],float[][max]);
void   compute1(float[][max],float[][max],float[][max],int,int,int,float);
float  compute2(float[][max],int);
void   display(float[][max],float[][max],int,float);

int    main(){

    float  num[max][max],g[max][max],v[max][max],a[max][max];
    int dn,u;
    float det;
    int register i,j;

    dn=read(num,a);
    det=compute2(num,dn);

    for(i=0;i<dn;i++)
        for(j=0;j<dn;j++){
            compute1(a,g,v,dn,i,j,det);
        }

    display(a,v,dn,det);
    getch();

    return 0;
}

//****************************************************************************
int read(float num[][max],float a[][max]){

    int dn;
    clrscr();
    int register i,j;

    printf("\nenter degree of matrix:");

    scanf("%d",&dn);
    clrscr();

    for(i=0;i<dn;i++){
        printf("\n\n\nenter arguments of row[%d]:\n\n",i);

        for(j=0;j<dn;j++){
            scanf("%f",*(num+i)+j);

            *(*(a+i)+j)=*(*(num+i)+j);
        }
    }
    return dn;
}


//****************************************************************************
void   display(float c[][max],float inv[][max],int dn,float det){
    int register i,j;
    clrscr();

    printf("\n\n\n\n\n\t\t\t    --ORIGINAL MATRIX--\n");

    for(i=0;i<dn;i++){
        printf("\n\t\t");

        for(j=0;j<dn;j++)
        printf("%10.3f",c[i][j]);
    }

    printf("\n\n\n\t\t\t      --INVERSE MATRIX--\n");

    for(i=0;i<dn;i++){
        printf("\n\t\t");

        for(j=0;j<dn;j++)
        printf("%10.6f  ",inv[i][j]);
    }

    printf("\n\n\n\t\tعؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤ؟");
    printf("\n\t\t³ determinan of matrix= %19.7f  ³ ",det);
    printf("\n\t\tہؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤظ");
}


//****************************************************************************
void compute1(float g[][max],float v[][max],float inv1[][max],int dn,int r,int c,float e){
    int col=0,row=0,add=r+c,y;
    float y1=1;

    if(add%2)
        y1=-1;

    int register i,j;

    for(i=0;i<dn-1;i++){
        if(i==r)
            row=1;

        col=0;

        for(j=0;j<dn-1;j++){
            if(j==c)
                col=1;

            v[i][j]=g[i+row][j+col];
        }
    }

    inv1[c][r]=y1*compute2(v,dn-1)/e;
}


//****************************************************************************
float compute2(float c[]开发者_如何学Python[max],int s){
    float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
    int register i,j;

    while(y>0){
        for(i=1;i<k;i++){
            if(c[y][y]!=0)
                h[i-1]=c[y-i][y]/c[y][y];
            else{
                for(j=0;j<s;j++)
                    c[y][j]+=c[y-i][j];
                h[i-1]=c[y-i][y]/c[y][y];
            }
        }

    y--;
    k--;

    for(i=0;i<k;i++)
        for(j=s-1;j>=0;j--)
            c[y-i][j]=c[y-i][j]-h[i]*c[y+1][j];
    }

    for(i=0;i<s;i++)
        h1*=c[i][i];

    return h1;
}


There's quite a number of problems with your code. In compute2 you're allocating memory for.. a row?

   float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;

You're allocating space for just one float though. That could be a possible source of crashes.

You never actually deallocate the memory either, too.


You need to use debugging and logging tools rather than eyeballing the code. We don't even know what your crash is.

However here is one suspicious place

    if(c[y][y]!=0)
        h[i-1]=c[y-i][y]/c[y][y];
    else{
        for(j=0;j<s;j++)
            c[y][j]+=c[y-i][j];
        h[i-1]=c[y-i][y]/c[y][y];
    }

In both branches of the if...else you are dividing by c[y][y]. You know when it enters the else that is zero. Unless the for loop changes it you will have divide by zero. So I suggest you test it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜