/*
Obliczanie wyznacznika
Wprowadzenie tablicy 4x4 i obliczanie dlia nej wyznacznika






















*/
#include <iostream>
#include <iomanip>
#include <limits>

using namespace std; // w starszych kompilatorach trzeba zakomentować ten wiersz

double Det(double T[][4]);
void Drukuj(double T[][4]);

int main()
  {
   double T[4][4];
   while(true)
     {
      for(int y=0;y<4;)
        {
         cout<<"Podaj "<<(y+1)<<" wiersz tablicy: ";
         for(int x=0;x<4;++x) cin>>T[y][x];
         if(cin) ++y;
         else
           {
            cin.clear();
            if(cin.get()=='!') return 0;
            cout<<"Blad wprowadzania"<<endl;
           }
         cin.ignore(INT_MAX,'\n');
        }
      double Dt=Det(T);
      cout<<endl<<"Wprowadzona macierz: "<<endl;
      Drukuj(T);
      cout<<endl<<"Wyznacznik="<<Dt<<endl<<endl;
     }
  }

void Drukuj(double T[][4])
  {
   cout.setf(ios::fixed);
   for(int y=0;y<4;++y)
     {
      for(int x=0;x<4;++x)
        {
         cout<<setw(9)<<setprecision(2)<<T[y][x];
        }
      cout<<endl;
     }
  }

double Det(double T[][2])
  {
   return(T[0][0]*T[1][1]-T[0][1]*T[1][0]);
  }

double Det(double T[][3])
  {
   double Tb[2][2];
   double Suma=0;
   for(int X=0;X<3;++X)
     {
      for(int y=1;y<3;++y)
        {
         for(int x=0,xb=0;x<3;++x)
           {
            if(x!=X) Tb[y-1][xb++]=T[y][x];
           }
        }
      Suma+=(((X&1)<<1)-1)*T[0][X]*Det(Tb);
     }
   return(Suma);
  }

double Det(double T[][4])
  {
   double Tb[3][3];
   double Suma=0;
   for(int X=0;X<4;++X)
     {
      for(int y=1;y<4;++y)
        {
         for(int x=0,xb=0;x<4;++x)
           {
            if(x!=X) Tb[y-1][xb++]=T[y][x];
           }
        }
      Suma+=(((X&1)<<1)-1)*T[0][X]*Det(Tb);
     }
   return(Suma);
  }


