#include <iostream.h>
#include <conio.h>
#include <math.h>

void cutsquare(float **& newsquare, float ** oldsquare, int oldsize, int r, int c)
  {
    int trow, tcol;
    newsquare = new float*[oldsize-1];
    for (int i = 0; i < oldsize-1; i++) {newsquare[i] = new float[oldsize-1];}

    for (int x=0; x<oldsize-1; x++)
      for (int y=0; y<oldsize-1; y++)
        {
          if (x<r) {trow=x;} else {trow=x+1;}
          if (y<c) {tcol=y;} else {tcol=y+1;}
          newsquare[x][y] = oldsquare[trow][tcol];
        }
  }

void deletesquare(float ** todelete, int size)
  {
    for (int i=0; i < size;  i++) {delete[] todelete[i];}
    delete[] todelete;
  }

long double det(float ** matrix, int matsize)
  {
    long double sum = 0.0;
    if (matsize==2)
      {return matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];}
    else
      {
        for (int col = 0; col < matsize; col++) {
          float ** newsquare;
          cutsquare(newsquare, matrix, matsize, 0, col);
          sum = sum + pow(-1,0+col)*matrix[0][col]*det(newsquare,matsize-1);
          deletesquare(newsquare,matsize-1);
        }
      }
    return sum;
  }

void main()
  {
    int size; float **matrix;
    cout << "How big is the matrix: "; cin >> size;
    clrscr(); cout << size << "x" << size;

    matrix = new float*[size];
    for (int i = 0; i < size; i++) {matrix[i] = new float[size];}

    for(int x = 0; x < size; x++)
      for (int y = 0; y < size; y++)
        {
          gotoxy(6,1); cout << "|  Enter number for " << x+1 << "," << y+1 << ":";
          gotoxy(y * 5 + 6, x*2+3);
          cin >> matrix[x][y];
        }

    cout << endl << "The determinant is:  " << det(matrix,size);

    for (int i = 0; i < size;  i++) {delete[] matrix[i];}
    delete[] matrix;
  }
