C++ program to find determinant of a 4×4 matrix.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <stdio.h> #include <stdlib.h> #include <time.h> #define d 4 //4x4 double det (double A[d][d], int N) { double c, r=1; for(int i = 0; i < N; i++) { for(int k = i+1; k < N; k++) { c = A[k][i] / A[i][i]; for(int j = i; j < N; j++) A[k][j]= A[k][j] - c*A[i][j]; } } for (int i = 0; i < N; i++) r *=A[i][i]; return r; } int main() { double M[d][d]; for(int i=0; i<d; i++) { for(int j=0; j<d; j++) { M[i][j] = rand() % 20; } } printf("Det(M) = %f\n",det(M,d)); return 0; } |
This code calculates the determinant of a square matrix M
of size d x d
. The function det()
takes a 2D array A
and an integer N
as input and returns the determinant of the matrix as a double. The determinant is calculated using Gaussian elimination.
The determinant is a scalar value that can be computed from the elements of a square matrix. It is a measure of the “nonsingularity” of the matrix and has a number of useful properties. For example, it can be used to determine whether a system of linear equations has a unique solution. The determinant is denoted by the symbol “det” or “|A|”.
To calculate the determinant, the function first performs Gaussian elimination on the matrix. This is a process of manipulating the rows and columns of the matrix to put it into an “upper triangular” form. The determinant is then calculated by taking the product of the diagonal elements of the upper triangular matrix.
The function main()
initializes a 4×4 matrix M
with random values between 0 and 19, and then calls the det()
function to calculate the determinant of M
. The result is printed to the console.
Output:
Here is a detailed explanation of the code:
First, the code includes the necessary headers:
1 2 3 4 5 | #include <stdio.h> #include <stdlib.h> #include <time.h> |
stdio.h
is the C Standard Input/Output library and is used for input and output operations in the code. stdlib.h
is the C Standard Library and is used for various utility functions such as memory allocation and random number generation. time.h
is the C Time library and is used to seed the random number generator.
Next, the code defines a constant d
with the value 4
:
1 2 3 | #define d 4 //4x4 |
This constant is used to specify the size of the matrix M
, which is a d x d
matrix.
The code then defines the function det()
which takes a 2D array A
and an integer N
as input and returns the determinant of the matrix as a double:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | double det (double A[d][d], int N) { double c, r=1; for(int i = 0; i < N; i++) { for(int k = i+1; k < N; k++) { c = A[k][i] / A[i][i]; for(int j = i; j < N; j++) A[k][j]= A[k][j] - c*A[i][j]; } } for (int i = 0; i < N; i++) r *=A[i][i]; return r; } |
This function performs Gaussian elimination on the matrix A
to put it into upper triangular form. It does this by manipulating the rows and columns of the matrix as follows:
- Loop over the rows of the matrix from top to bottom (
for(int i = 0; i < N; i++)
). - For each row
i
, loop over the rows below it (for(int k = i+1; k < N; k++)
). - Calculate a “multiplier”
c
equal to the element in the current rowk
divided by the element in the same column in the current rowi
. - Loop over the columns in the current row
k
(for(int j = i; j < N; j++)
) and update each element by subtracting the corresponding element in the current rowi
multiplied by the multiplierc
.
After the matrix is in upper triangular form, the determinant is calculated by taking the product of the diagonal elements of the matrix (for (int i = 0; i < N; i++) r *=A[i][i];
). The determinant is returned as a double.
Finally, the code defines the main()
function which initializes a 4×4 matrix M
with random values between 0 and 19 using a loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | int main() { double M[d][d]; for(int i=0; i<d; i++) { for(int j=0; j<d; j++) { M[i][j] = rand() % 20; } } printf("Det(M) = %f\n",det(M,d)); return 0; } |
The printf()
function is used to print the string “Det(M) = ” followed by the value of the determinant of M
, which is calculated by calling the det()
function with M
and d
as arguments.
Finally, the main()
function returns 0
to indicate that the program terminated successfully.