0

This is the C code for determinant of matrix, But it gives compilation errors.

Code is:

#include<stdio.h>
#include<math.h>
int m;

float determinant(float b[][]);

int main(void)
{
int i,j,m;
printf("enter a no: ");
scanf("%d",&m);
//printf("%d",m);
float arr[m][m];
for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {
        scanf("%f",&arr[i][j]);
        //printf("%f",arr[i][j]);
    }
}


for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {
        printf("%f ",arr[i][j]);
    }

    printf("\n");
}



float det = determinant(arr);

printf("Determinant= %f ", det);




}

float determinant(float b[][])
{
int i,j;
int p;
float sum = 0;
float c[m][m];

for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {
        printf("%f ",b[i][j]);
    }

    printf("\n");
}



if(m==2)
{
    printf("Determinant for m=2");
    sum = b[0][0]*b[1][1] - b[0][1]*b[1][0];
    return sum;
}

for(p=0;p<m;p++)
{
    int h = 0,k = 0;
    for(i=1;i<m;i++)
    {
        for( j=0;j<m;j++)
        {
            if(j==p)
                continue;
            c[h][k] = b[i][j];
            k++;
            if(k == m-1)
            {   
                h++;
                k = 0;
            }
        }
    }

    m=m-1;
    sum = sum + b[0][p]*pow(-1,p) * determinant(c);

}




return sum;
}

And the Compilation Errors are:

det.c:5:25: error: array type has incomplete element type
det.c: In function ‘main’:
det.c:36:2: error: type of formal parameter 1 is incomplete
det.c: At top level:
det.c:45:25: error: array type has incomplete element type
det.c: In function ‘determinant’:
det.c:91:3: error: type of formal parameter 1 is incomplete
det.c:99: confused by earlier errors, bailing out
Preprocessed source stored into /tmp/cc1Kp9KD.out file, please attach this to your bug report.

I think the error is in the passing of 2-D Array. when I passing it as a pointer then it gives warnings but no errors but it does not give the right result as in always gives determinant as Zero. So I guess the array is not being passed only and when I print it in the function determinant it doesn't print also. Please help as I am stuck because of this in my project.

1

5 Answers 5

0

in your code,

scanf("%d",&m);
//printf("%d",m);
float arr[m][m];

here arr is a 2D array with static memory allocation so you can not read m at run time and declare arr like this.
so if you are want to define the array dynamically then use dynamic memory allocation methods like malloc() in C.

1
  • You answer is wrong. Read about variable length arrays.
    – haccks
    Mar 13, 2014 at 16:44
0

When you declare the prototype of a function as

int foo(int arr[], int n);

then compiler interprets it as

int foo(int (*arr), int n);  // and that's why you can omit the first dimension!

i.e, your function is expecting first argument is of type int *. Similarly, when the parameter is a multidimensional array as

int foo(int arr[][col], int n); // Only first dimension can be omitted. You need to specify the second dimension.   

then the compiler interprets it as

int foo(int (*arr)[col], int n);   

i.e, your function is expecting first argument is of type int (*)[col] (a pointer to int array).
Since when passed to a function (in most cases) array names decay to pointer to its first element, in your case arr will be decayed to pointer to its first element, i.e, first row. Hence its type will become float (*)[m]. Its is compatible to your function parameter if you will declare it as

float determinant(int m, float b[][m]);     

and the call should be like

float det = determinant(m, arr);
0

You can declare array dynamically like this in C99 (variable length arrays, pointed out by haccks), but not in the earlier version:

float arr[m][m];

So, if it troubles you then instead declare a pointer and malloc memory for it:

float* arr = malloc(sizeof(float)*m*m);

Also, the definition won't work (in either case):

float determinant(float b[][]);

you need to define the columns in the array that you pass to the function.

If you declare and allocate the pointer as I have shown then you can just pass a pointer in your function:

float determinant(float *b, int size); //here size is your row dimension, in this case equal to m

And inside the function, access your elements like:

*(b + size*i + j) = value // equivalent to b[i][j];
1
  • Yes you can. This is called variable length array.
    – haccks
    Mar 13, 2014 at 16:45
0

Declare your array with explicit bounds float b[m][m]; the compiler doesn't understand empty bounds in float b[][] (empty bounds are OK only for 1-D arrays, for reasons explained in the other answers).

So your determinant function should look like this:

float determinant(int m, float b[m][m])
{
    ...
}

There are other ways to make your code work, but I think this way is closest to what you already have.

1
  • I tried this but doesnt work, and gives one more error as 'm ' undeclared? can u tell other solution Mar 14, 2014 at 2:20
0

I agree you can't move with this syntax of array initialization, u could have used

int *p=(int*)calloc(n*n,sizeof(float));

And then access your elements as :-

*(p+j+n*i);//for p[i][j] element

Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.