Member • Nov 17, 2008
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.
😁
Member • Nov 17, 2008
Member • Nov 20, 2008
Member • Nov 20, 2008
Member • Nov 24, 2008
//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; } }😁
Member • May 17, 2009
#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: