Ashutosh_shukla
Ashutosh_shukla
Branch Unspecified
18 Nov 2008

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

niraj.kumar

Branch Unspecified
20 Nov 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
Ashutosh_shukla

Ashutosh_shukla

Branch Unspecified
21 Nov 2008
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
Ashutosh_shukla

Ashutosh_shukla

Branch Unspecified
24 Nov 2008
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<iostream.h>
#include<conio.h>
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<n;i++)
  for(j=0;j<n;j++)
  {
   cout<<"a["<<i+1<<"]["<<j+1<<"]=";
   cin>>a[i][j];
  }
 cout<<"The matrix is : \n";
 print(a,n,n);
 cout<<"The value of determinant is = "<<eval(a,n);
 getch();
}
void print(int a[10][10],int m,int n)
{
 int i,j;
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   cout<<a[i][j]<<"\t";
  cout<<"\n";
 }
}
int eval(int det[10][10],int n)
{
 if(n==1)
  return det[0][0];
 else
 {
  int a[10][10],i,j,k,p,q,t=1,val=0;
  for(i=0;i<n;i++)
  {
   p=0;
   for(j=1;j<n;j++)
   {
    q=0;
    for(k=0;k<n;k++)
    {
     if(k!=i)
     {
      a[p][q]=det[j][k];
      q++;
     }
    }
    p++;
   }
   val=val+t*det[0][i]*eval(a,n-1);
   t=-t;
  }
  return val;
 }
}
😁
pradeep_agrawal

pradeep_agrawal

Branch Unspecified
17 May 2009
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.

-Pradeep

Share this content on your social channels -

Only logged in users can reply.