0

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(); ;
        }
    }
    }

Edited by Anel_1

3
Contributors
3
Replies
53
Views
9 Months
Discussion Span
Last Post by Anel_1
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.