#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int **fill_matrix(int **a,int n){
	a=new int *[n];
	for(int i=0;i<n;i++)
		a[i]=new int[n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			a[i][j]=rand()%10 +1;
		}
	}
	return a;
}

//Function to get cofactor of mat[p][q] in temp[][]. n os current
//dimention of mat[][]
void getCofactor(int mat[N][N], int temp[N][N], int p, int q, int n){
	int i=0,j=0;
	//Looping for each element of the matrix
	for(int row=0;row<n;row++){
		for(int col=0;row<n;row++){
			//Copying into temporary matrix only those 
			//elements which are not in given row ant column
			if(row != p && col != q){
				temp[i][j++]=mat[row][col];

				//Row is filled, so increase row index
				//and reset col index
				if(j==n-1){
					j=0;
					i++;
				}
			}
		}
	}
}

// Recursive function for finding determinant of matrix.
//n is current dimention of mat[][].
int determinantOfMatrix(int mat[N][N], int n){
	int D=0; //Initialize result

	//Base case: if matrix contains single element
	if(n==1)
		return mat[0][0];
	int temp[N][N]; //To store sign multiplier

	//Iterate for each element of first row
	for(int f=0;f<n;f++){
		//Getting Cofactor of mat[0][f]
		getCofactor(mat, temp, 0, f, n);
		D+=sign*mat[0][f]*determinantOfMatrix(temp, n-1);

		//terms are to be added with alternate sign
		sign = -sign;
	}
	return D;
}



int main(){

int mat[n][n];

int n;

cout<<"Give integer n for square matrix dimentions: ";
cin>>n;
cout<<endl;

mat=fill_matrix(mat, n);

int det=determinantOfMatrix(mat, n);

cout<<det;




return 0;
}
