Home > front end >  Why my code is not running for finding the determinant?
Why my code is not running for finding the determinant?

Time:02-25

I want to find the determinant of a 3*3 matrix but am not sure why is it not giving a good answer?

#include <stdio.h>
    int main()
    {
        int A[3][3];
        for (int i = 1; i <= 3; i  )
        {
            for (int j = 1; j <= 3; j  )
            {
                scanf("%d", &A[i][j]);
            }
        }
        int determinant = 0;
        determinant = (A[1][1] * A[2][2] * A[3][3])   (A[2][1] * A[3][2] * A[1][3])   (A[1][2] * A[2][3] * A[3][1]) - (A[1][3] * A[2][2] * A[3][1]) - (A[3][2] * A[2][3] * A[1][1]) - (A[2][1] * A[1][2] * A[3][3]);
        printf("Determinant of the matrix:%d", determinant);
        return 0;
    }

CodePudding user response:

The bound of array is from 0 to N-1. So you change bound your array.

#include <stdio.h>
    int main()
    {
        int A[3][3];
        for (int i = 0; i < 3; i  )
        {
            for (int j = 0; j < 3; j  )
            {
                scanf("%d", &A[i][j]);
            }
        }
        int determinant = 0;
        determinant = (A[1][1] * A[2][2] * A[0][0])   (A[2][1] * A[0][2] * A[1][0])   (A[1][2] * A[2][0] * A[0][1]) - (A[1][0] * A[2][2] * A[0][1]) - (A[0][2] * A[2][0] * A[1][1]) - (A[2][1] * A[1][2] * A[0][0]);
        printf("Determinant of the matrix:%d", determinant);
        return 0;
    }

CodePudding user response:

Your code will result in: "stack smashing detected" error

Usually, the compiler generates the stack smashing detected error in response to its defense mechanism against buffer overflows.

A buffer​ overflow occurs when the user input exceeds the buffer capacity. The following C code can cause the buffer to overflow if the user enters more than ten characters. In such a ​case, the compiler will throw the stack smashing detected error.

reference: https://www.educative.io/edpresso/what-is-the-stack-smashing-detected-error

  • Related