C Program to Compute Determinant of a 3 x 3 Matrix

The program receives a 3 x 3 matrix and computes the determinant and prints the results. The user provides the values for the matrix.

Advertisements

We compiled the program using Dev-C++ 5.0 compiler, but you can use a different compiler such as Turbo C++ 3.0.

You must be familiar with the concept of the matrix and its determinant to understand this example. See the problem definition section for a brief introduction.

Learn following C programming concepts before you learn the example.

Problem Definition

The determinant of a 3 x3 matrix is easy to calculate. Before we dive into the code, let’s understand the procedure to calculate the determinant of a 3 x 3 matrix.

Suppose you are given a 3 x3 matrix like one given below.

Advertisements
3 x 3 Matrix
3 x 3 Matrix

The next step is to find the co-factor of A_{11}, A_{21} and A_{31}.

\begin{aligned}& A_{11} \hspace{1ex} = \hspace{1ex} a_1 (b_2 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - c_2 \hspace{1ex} \cdot b_3 )\\ \\
&A_{21} \hspace{1ex} = \hspace{1ex} -a_2(b_1 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - c_1 \hspace{1ex} \cdot b_3) \\ \\
&A_{31} \hspace{1ex} = \hspace{1ex} a_3(b_1 \hspace{1ex} \cdot \hspace{1ex} c_2 \hspace{1ex} - c_1 \hspace{1ex} \cdot \hspace{1ex} b_2)
\end{aligned}

Now the value of determinant for 3 \times 3 matrix is

\begin{aligned}&Determinant \hspace{1ex} = \hspace{1ex} a_1(b_2 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - \hspace{1ex} c_2 \hspace{1ex} \cdot \hspace{1ex}  b_3) \hspace{1ex} \\ \\ &\hspace{1ex}- a_2(b_1 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - \hspace{1ex} c_1 \hspace{1ex} \cdot \hspace{1ex} b_3) \\ \\ &\hspace{1ex} + \hspace{1ex} a_3( b_1 \hspace{1ex} \cdot \hspace{1ex} c_2 \hspace{1ex} -\hspace{1ex} c_1 \hspace{1ex} \cdot \hspace{1ex} b_2)\end{aligned}

Program Code – Determinant of 3 x 3 Matrix

/*Program to compute the determinant of a matrix */

#include <stdio.h>

#include <stdlib.h>

int main()
{

    int a[3][3],i,j,m,n,detval;

    int r1,r2,r3;

    r1 = 0;

    r2 = 0;

    r3 = 0;

    detval = 0;

    printf("Enter Elements of Matrix Row wise:\n");

    for(i=0;i<3;i++)
    {

        for(j=0;j<3;j++)
        {

            scanf("%d",&a[i][j]);

        }

    }

    r1 = a[0][0] * ((a[1][1] * a[2][2]) 
    - (a[2][1] * a[1][2]));

    r2 = a[0][1] * ((a[1][0] * a[2][2]) 
    - (a[2][0] * a[1][2]));

    r3 = a[0][2] * ((a[1][0] * a[2][1]) 
    - (a[2][0] * a[1][1]));

    detval= r1 - r2 + r3;

    printf("Determinant=%d\n",detval);

    system("PAUSE");

    return 0;

}

Output

When the program executes, it ask for input values for the matrix. You must enter the values row-wise. Upon entering the values, it computes the determinant and prints the output to the console.

You can enter negative values too, the compiler will consider that as an negative integer value.The output value as you can see is a negative integer value of -11.

You can verify the answer by doing the math on a paper and compare the results with program output. Try solving for different values for matrix.

Enter Elements of Matrix Row Wise:
1 4 5
1 3 1
2 3 1
Determinant=-11
Press any key to continue . . . _

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.