Skip to main content

To Calculate Determinant of a Matrix Using Recursion in C++.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
 int determinant(int[5][5],int);// FUNCTIONS
 void read(int[5][5],int);
 void print(int[5][5],int);     // PROTOYPES
 int a[5][5],l,n;
 int result;
 l1:clrscr();
 cout<<"ENTER ORDER OF MATRIX(MAX. OF 4X4):";
 cin>>l>>n;
//TESTING CONDITION
if(l!=n)
  {                   
	cout<<"SORRY!!!!!\nONLY SQUARE MATRIX";
	goto l1;
  }
 read(a,n);
 result = determinant(a,n);
 print(a,n);
 cout<<"THE DETERMINANT OF THE ABOVE MATRIX IS:"<<result;
 getch();
}
void read(int b[5][5],int m)     //FUNCTION FOR READING MATRIX
{
 cout<<"ENTER "<<m*m<<" ELEMENTS OF MATRIX(ROW-WISE):";
 for(int i=0;i<m;i++)
  for(int j=0;j<m;j++)
	cin>>b[i][j];
}
//FUNCTION FOR PRINTING MATRIX
void print(int b[5][5],int m)
{
 clrscr(); 
 cout<<"MATRIX IS :";
 for(int i=0;i<m;i++)
 {
  cout<<"";
  for(int j=0;j<m;j++)
	cout<<"	"<<b[i][j];
 }
}
//FUNCTION TO CALCULATE DETERMINANT
int determinant(int b[5][5],int m)   
{
 int i,j,sum = 0,c[5][5];
 if(m==2)
  {           /BASE CONDITION
	sum = b[0][0]*b[1][1] - b[0][1]*b[1][0];
	return sum;
  }
 for(int p=0;p<m;p++)
 {
  int h = 0,k = 0;
  for(i=1;i<m;i++)
  {
	for( j=0;j<m;j++)
	{
	 if(j==p)
	  continue;
	 c[h][k] = b[i][j];
	 k++;
	 if(k == m-1)
	  {
		 h++;
		 k = 0;
	  }
	}
  }
  sum = sum + b[0][p]*pow(-1,p)*determinant(c,m-1);
 }
 return sum;
}

Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }