Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12920 Members
Join this group to post and comment.

# Write a program to evaluate a determinant of any order n*n.

Write a program to evaluate a determinant of any order n*n.
Input would be the value of n and the determinant itself.The output is the value of the determinant.
😁
niraj.kumar • Nov 20, 2008
Dont get you can you please provide me an example wat actually you are asking for, as far as I know in program to evaluate the complexity you have 2 make changes in the code.Please correct me if I am wrong
You can be given any determinant as input say maximum order 10 x 10.
You have to write the program to evaluate this determinant.

Example:
Input will be
Order:4
Actual determinant:
8 1 0 0
16 18 8 1
0 5 16 18
0 0 0 5

Output will be value : 8640
I think no one tried it out. Here is my code Pleasse check this one out and if you find a better one tell me.
```//Program to evaluate determinant of any order
#include
#include
void print(int a[10][10],int m,int n);
int eval(int det[10][10],int n);
void main()
{
int a[10][10],i,j,n;
clrscr();
cout<<"Enter the order of the matrix : ";
cin>>n;
cout<<"Enter the elements of the matrix : \n";
for(i=0;i😁
```
Below is my piece of code for same.

```#include "stdio.h"

#define MAX_MATRIX_ORDER 10

void print_matrix(int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int order) {
int i = 0, j = 0;
for(i = 0; i < order; i++) {
for(j = 0; j < order; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

long calculate_determinant(int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int order) {
int submatrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER] = { 0 };
int determinant = 0;
int sign = 1;
int i = 0, j = 0, k = 0;

if(order == 1) {
determinant = matrix[0][0];
} else {
for(i = 0; i < order; i++) {
for(j = 0; j < (order - 1); j++) {
for(k = 0; k < (order - 1); k++) {
if(k < i) {
submatrix[j][k] = matrix[j + 1][k];
} else {
submatrix[j][k] = matrix[j + 1][k + 1];
}
}
}
determinant += sign * matrix[0][i] * calculate_determinant(submatrix, order -1);
sign = -sign;
}
}

return determinant;
}

int main() {
int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER] = { 0 };
int order = 0;
long determinant = 0;
int i = 0, j = 0;

scanf("%d", &order);
if(order > MAX_MATRIX_ORDER) {
printf("Matrix order too high\n");
return 0;
} else if(order < 1) {
printf("Invalid matrix order\n");
return 0;
}

for(i = 0; i < order; i++) {
for(j = 0; j < order; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("Matrix:\n");
print_matrix(matrix, order);

determinant = calculate_determinant(matrix, order);
printf("Determinant: %d\n", determinant);

return 0;
}
```
Compile the file with above code and run as:
a.exe < input.txt > output.txt [on windows]
a.out < input.txt > output.txt [on linux]

Here,
a.exe or a.out are the executables generated after compilation

input.txt is the file containing input, e.g., if the matrix of order 4 is:
8 1 0 0
16 18 8 1
0 5 16 18
0 0 0 5
Then input.txt will be
4
8 1 0 0
16 18 8 1
0 5 16 18
0 0 0 5

output.txt is the output file containing result, for given example output.txt will contain
Matrix:
8 1 0 0
16 18 8 1
0 5 16 18
0 0 0 5
Determinant: 8640

Let me know if any item need clarification.