1

This code below is the code for finding the determinant for 3x3 matrix (this code is intended for nxn matrix, but for the sample, I used 3x3), using recursive The result is working fine, but I wonder what errors in this code make this must be the printf("\n") before calling the sub-function (itself) or else it will return the error 0xc0000fd (stack overflow).

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define size 3


void trimarray(int**rrayrc,int**rrayout, int dim,int cuti,int cutj)
{
    int i, j;
    int ti = 0,tj;

    for(i = 0; i<dim; i++)
    {
        tj = 0;
        for(j = 0; j< dim; j++)
        {
            if(!((i==cuti)||(j==cutj)))
            {
                    rrayout[ti][tj] = rrayrc[i][j];
            }
            if(j!=cutj) {tj++;}
        }
        if(i!=cuti) {ti++;}
    }

}
void initializearray(int** rray,int dim)
{
    int i, j;
    for(i = 0; i<dim; i++)
    {
        for(j = 0; j<dim; j++)
        {
            rray[i][j] = 0;
        }
    }
}
int det(int** rray, int dim)
{
    int i,j;
    int cut[dim-1][dim-1];
    int* cutp[i];
    int mul = 1,sum=0;
    if(dim >1)
    {
        for(i = 0; i<dim-1; i++)
        {
            cutp[i] = cut[i];
        }
        initializearray(cutp,dim-1);
        for(i = 0; i<dim; i++)
        {
            printf("\n",dim); //<< Without this the program won't work
            trimarray(rray,cutp,dim,0,i);
            sum+=det(cutp,dim-1)*mul*rray[0][i];
            mul = 0-mul;
        }
        return sum;

    }
    else
    {
        return rray[0][0];
    }
}
int main()
{
    int test[size][size] = {2,-3,-2,-6,3,3,-2,-3,-2};
    int* testpntr[size];
    int i,deter;
    for(i = 0; i<size; i++)
    {
        testpntr[i] = test[i];
    }
    deter = det(testpntr,size);
    printf("[%d]",deter);
    getch();

    return 0;
}

The answers will be dearly appreciated.

1 Answer 1

2

int* cutp[i]; is undefined behavior since i is uninitialized at this stage. You have no idea what is the size of cutp array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.