Sahil-Pattni
12/1/2016 - 8:01 PM

Lab13

Lab13

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by spattni on 12/1/16.
 */

public class Recursion
{

    public static int determinant(int[][] matrix)
    {

        if (matrix.length == 1 && matrix[0].length == 1)
        {
            return matrix[0][0];
        }
        else if (matrix.length == 2)
        {
            return ((matrix[0][0]*matrix[1][1])-(matrix[1][0]*matrix[0][1]));
        }
        else if (matrix.length == matrix[0].length && matrix.length > 2)
        {

            int d = 0;

            for (int i = 0; i < matrix.length; i++)
            {
                int[][] A = new int[matrix.length - 1][];

                for (int j = 0; j < A.length; j++)
                {
                    A[j] = new int[matrix.length - 1];
                }
                for(int k = 1; k < matrix.length; k++)
                {
                    int i2 = 0;
                    for (int l = 0; l < matrix.length; l++)
                    {
                        if (l == i)
                            continue;
                        A[k-1][i2] = matrix[k][l];
                        i2++;
                    }
                }


                if (i % 2 == 0)
                {
                    d += matrix[0][i] * determinant(A);
                }
                else
                {
                    d -= matrix[0][i] * determinant(A);
                }
            }
            return d;
        }
        return 69;
    }

    public static int filecount (File f)
    {
        int counter = 0;

        if(f.isDirectory())
        {
            File[] fileArray = f.listFiles();

            if (fileArray != null)
            {
                
                for (int i = 0; i < fileArray.length; i++)
                {
                    counter += filecount(fileArray[i]);
                }
            }
        }

        else
            counter++;

        return counter;
    }

    public static void main(String[] args) {
        int[][] mat = new int[3][3];
        mat[0][0] = 4;
        mat[0][1] = 8;
        mat[0][2] = 9;
        mat[1][0] = 23;
        mat[1][1] = 53;
        mat[1][2] = 63;
        mat[2][0] = 53;
        mat[2][1] = 32;
        mat[2][2] = 2;

        //DETERMINANT SUPPOSED TO BE 47
        System.out.print(determinant(mat));
    }
}