Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C# by Andrei ( 7 years ago )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Accerman
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(GetA(1, 2));

            int m = 3;

            int[,] a = new int[,] { { 1, 2, 3 }, 
                                    { 4, 5, 6 },
                                    { 7, 8, 9 }};
            //int[,] a = new int[m, m];
            //Random random = new Random();
            //for (int i = 0; i < m - 1; i++)
            //{
            //    for (int j = 0; j < m - 1; j++)
            //    {
            //        a[i, j] = random.Next(100);
            //    }
            //}
            //int[,] b = CopyArray(a, 1, 1);
            //Console.WriteLine(a.GetLength(0));
            //Console.WriteLine(a.GetLength(1));
            Console.WriteLine(GetDeterminant(a));
        }

        public static int GetDeterminant(int[,] matrix)
        {
            if (matrix.GetLength(0) == 2)
            {
                return (matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]);
            }

            int determinant = 0;
            for (int i = 0; i < matrix.GetLength(1); i++)
            {
                for (int j = 0; j < matrix.GetLength(0); j++)
                {
                    determinant += matrix[i, j] * GetDeterminant(CopyArray(matrix, i, j));
                }
            }
            return determinant;
        }

        public static int[,] CopyArray(int[,] array, int a, int b)
        {
            int n = array.GetLength(0);
            int[,] newArray = new int[n - 1, n - 1];
            for (int i = 0, k = 0; i < n; i++)
            {
                if( i == a)
                {
                    continue;
                }
                for (int j = 0, m = 0; j < n; j++)
                {
                    if (j == b)
                    {
                        continue;
                    }
                    newArray[k, m] = array[i, j];
                    m++;
                }
                k++;
            }
            return newArray;

        }

        public static int GetA(int m, int n)
        {
            if( m == 0 && n > 0)
            {
                return n + 1;
            }
            if (n == 0 && m > 0)
            {
                return GetA(m - 1, 1);
            }
            if(m > 0 && n > 0)
            {
                return GetA(m - 1, GetA(m, n - 1));
            }
            return 0;
        }
    }
}

 

Revise this Paste

Your Name: Code Language: