A newbie would appreciate if someone could give him a pseudocode or any kind of explanation for the part of the code where the actual algorithm for matrix determinant is written.

using System;

namespace Determinant
{
    class Program
    {           
        public static double DET(int n, double[,] Mat)
        {
            double d = 0;
            int k, i, j, subi, subj;
            double[,] SUBMat = new double[n, n];

            if (n == 2)
            {
                return ((Mat[0, 0] * Mat[1, 1]) - (Mat[1, 0] * Mat[0, 1]));
            }

        else
        {                
            for(k = 0; k<n; k++)
            {  
                subi = 0;  
            for(i = 1; i<n; i++)
                {  
                    subj = 0;
                    for(j = 0; j<n; j++)
                    {    
                        if (j == k)
                        {
                            continue;
                        }
                  SUBMat[subi,subj] = Mat[i,j];
                        subj++;
                    }
                    subi++;
                }
            d = d + (Math.Pow(-1 , k) * Mat[0,k] * DET(n - 1 , SUBMat));
            }
        }
        return d;
    }

        static void Main(string[] args)
        {

    int n;
    Console.WriteLine("Matrix dimension:");
    n = int.Parse(Console.ReadLine());
    double[,] Mat = new double[n,n];
    int i, j;

            Console.WriteLine("Matrix elements: ");
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write("M[{0},{1}] = ", i, j);
                    Mat[i, j] = int.Parse(Console.ReadLine());
                }
            }

            Console.WriteLine("\n");
            Console.WriteLine("Your Matrix:");
            for (i = 0; i < Mat.GetLength(0); i++)
            {
                for (j = 0; j < Mat.GetLength(1); j++)
                {

                    Console.Write(" " + Mat[i, j]);
                }

                Console.WriteLine();
                Console.ReadKey();
            }

    Console.WriteLine("Determinant: " + DET(n, Mat));
    Console.ReadKey(); ;
        }
    }
    }

Recommended Answers

All 3 Replies

I attached my working version of a matrix in C#, I did some time ago.
Maybe it helps.

Thank you all!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.