简体   繁体   中英

C# unhandled error: "Index was outside the bounds of the array"

using System;
using System.Collections.Generic;

namespace Matrix_Algebra
{
    public struct S_Matrix_size
    {
        public int size_R, size_C;
        public S_Matrix_size(int r, int c)
        {
            this.size_R = r;
            this.size_C = c;
        }
    }

    public class C_Matrix_entries
    {
        private S_Matrix_size _size;
        private double[,] _entry;
        public C_Matrix_entries()
        {
            int r, c;
            Console.WriteLine("Enter number of rows and columns ");

            r = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            _size = new S_Matrix_size(r, c);

            _entry = new double [_size.size_R,_size.size_C];

            Console.WriteLine("Enter the entries from first row [left to right] to the last row ");
            for (int i = 0; i<_size.size_R; i++)
            {
                Console.WriteLine("Enter the {0} row", i + 1);
                for (int j = 0; j<_size.size_C;j++)
                {
                    _entry[i, j] = Convert.ToDouble(Console.ReadLine());
                }
            }       
        }
        public S_Matrix_size Size { get { return _size; } }             //property
        public double[,] Entry { get { return _entry; } }
    }
}

namespace Matrix_process_Algebra
{
    using Matrix_Algebra;
    public class STARTHERE
    {
        static void Main()                                                                                      // Main
        {
            C_Matrix_entries matrix = new C_Matrix_entries();
            if (matrix.Size.size_C == matrix.Size.size_R)
            {
                if (matrix.Size.size_C == 2)
                {
                    Console.WriteLine("Determinant of the matrix? ");
                    Console.WriteLine("yes <y> or no <n> ?");
                    switch (Console.ReadLine())
                    {
                        case "n":
                        case "no":
                        case "N":
                        case "NO":
                        case "No":
                            Console.WriteLine("Goodbye!!");
                            break;
                        case "yes":
                        case "y":
                        case "Y":
                        case "YES":
                            Console.WriteLine("The determinant of the entered matrix is  ");
                            Sqr_Determinant process_2x2 = new Sqr_Determinant();
                            double result = process_2x2.Sqr_Determinant_2x2(matrix.Entry);
                            Console.Write(result);
                            Console.ReadLine();
                            break;

                    }
                }
                if (matrix.Size.size_C == 3)
                {
                    Console.WriteLine("Determinant of the matrix? ");
                    Console.WriteLine("yes <y> or no <n> ?");
                    switch (Console.ReadLine())
                    {
                        case "n":
                        case "no":
                        case "N":
                        case "NO":
                        case "No":
                            Console.WriteLine("Goodbye!!");
                            break;
                        case "yes":
                        case "y":
                        case "Y":
                        case "YES":
                            Console.WriteLine("The determinant of the entered matrix is  ");
                            Sqr_Determinant process_3x3 = new Sqr_Determinant();
                            double result = process_3x3.Sqr_Determinant_3x3(matrix.Entry);
                            Console.Write(result);
                            Console.ReadLine();
                            break;

                    }
                }

                if (matrix.Size.size_C == 4)
                {
                    Console.WriteLine("Determinant of the matrix? ");
                    Console.WriteLine("yes <y> or no <n> ?");
                    switch (Console.ReadLine())
                    {
                        case "n":
                        case "no":
                        case "N":
                        case "NO":
                        case "No":
                            Console.WriteLine("Goodbye!!");
                            break;
                        case "yes":
                        case "y":
                        case "Y":
                        case "YES":
                            Console.WriteLine("The determinant of the entered matrix is  ");
                            Sqr_Determinant process_4x4 = new Sqr_Determinant();
                            double result = process_4x4.Sqr_Determinant_4x4(matrix.Entry);
                            Console.Write(result);
                            Console.ReadLine();
                            break;

                    }
                }



            }
        }
    }
    public class Sqr_Determinant
    {
        public class C_sqr_Matrix_3x3                                                                //matrix 3x3
        {
            private double[,] _entries = new double[3, 3];
            public C_sqr_Matrix_3x3(double a,double b,double c,double d,double e,double f,double g,double h,double i)
            {
                this._entries[1, 1] = a;
                this._entries[1, 2] = b;
                this._entries[1, 3] = c;
                this._entries[2, 1] = d;
                this._entries[2, 2] = e;
                this._entries[2, 3] = f;
                this._entries[3, 1] = g;
                this._entries[3, 2] = h; 
                this._entries[3, 3] = i; 
            }
            public double[,] Entries { get { return _entries; } }
        }

        public class C_sqr_Matrix_2x2                                                               //matrix 2x2 
        {

            private double[,] _entries = new double[2,2];
            public C_sqr_Matrix_2x2(double a,double b, double c, double d)
            {
                this._entries[0, 0] = a;
                this._entries[0, 1] = b;
                this._entries[1, 0] = c;
                this._entries[1, 1] = d;
            }
            public double[,] Entries { get { return _entries; } }
        }

        public double Sqr_Determinant_2x2(double[,] entry)                                          //determinant 2x2
        {
            return entry[0, 0] * entry[1, 1] - entry[0, 1] * entry[1, 0];
        }

        public double Sqr_Determinant_3x3(double[,] entry)                                          //determinant 3x3
        {

            C_sqr_Matrix_2x2 _2233 = new C_sqr_Matrix_2x2(entry[1,1],entry[1,2],entry[2,1],entry[2,2]);
            C_sqr_Matrix_2x2 _1133 = new C_sqr_Matrix_2x2(entry[0,1],entry[0,2],entry[2,1],entry[2,2]);
            C_sqr_Matrix_2x2 _1122 = new C_sqr_Matrix_2x2(entry[0,1],entry[0,2],entry[1,1],entry[1,2]);

            return entry[0, 0] * Sqr_Determinant_2x2(_2233.Entries) - entry[1, 0] * Sqr_Determinant_2x2(_1133.Entries) + entry[2, 0] * Sqr_Determinant_2x2(_1122.Entries);
        }
        public double Sqr_Determinant_4x4(double[,] entry)
        {
            C_sqr_Matrix_3x3 _2244 = new C_sqr_Matrix_3x3(entry[1, 1], entry[1, 2], entry[1, 3], entry[2, 1], entry[2, 2], entry[2, 3], entry[3, 1], entry[3, 2], entry[3, 3]);
            C_sqr_Matrix_3x3 _1144 = new C_sqr_Matrix_3x3(entry[0, 1], entry[0, 2], entry[0, 3], entry[2, 1], entry[2, 2], entry[2, 3], entry[3, 1], entry[3, 2], entry[3, 3]);
            C_sqr_Matrix_3x3 _1144_ = new C_sqr_Matrix_3x3(entry[0, 1], entry[0, 2], entry[0, 3], entry[1, 1], entry[1, 2], entry[1, 3], entry[3, 1], entry[3, 2], entry[3, 3]);
            C_sqr_Matrix_3x3 _1133 = new C_sqr_Matrix_3x3(entry[0, 1], entry[0, 2], entry[0, 3], entry[1, 1], entry[1, 2], entry[1, 3], entry[2, 1], entry[2, 2], entry[2, 3]);
            return entry[0, 0] * Sqr_Determinant_3x3(_2244.Entries) - entry[1, 0] * Sqr_Determinant_3x3(_1144.Entries) +
                entry[2, 0] * Sqr_Determinant_3x3(_1144_.Entries) - entry[3, 0] * Sqr_Determinant_3x3(_1133.Entries);
        }
    }
}

I tried to debug and find the error but I still cannot find where is the error.

Your constructor for C_sqr_Matrix_3x3 is one of the issues. Arrays are zero-indexed. I also see other lines where you're treating this object as 1-indexed.

public C_sqr_Matrix_3x3(double a,double b,double c,double d,double e,double f,double g,double h,double i)
{
    this._entries[1, 1] = a;
    this._entries[1, 2] = b;
    this._entries[1, 3] = c;
    this._entries[2, 1] = d;
    this._entries[2, 2] = e;
    this._entries[2, 3] = f;
    this._entries[3, 1] = g;
    this._entries[3, 2] = h; 
    this._entries[3, 3] = i; 
}

should be

public C_sqr_Matrix_3x3(double a,double b,double c,double d,double e,double f,double g,double h,double i)
{
    this._entries[0, 0] = a;
    this._entries[0, 1] = b;
    this._entries[0, 2] = c;
    this._entries[1, 0] = d;
    this._entries[1, 1] = e;
    this._entries[1, 2] = f;
    this._entries[2, 0] = g;
    this._entries[2, 1] = h; 
    this._entries[2, 2] = i; 
}

You got it wrong here (see below). your array size is [3,3] means row: 0,1,2 column 0,1,2. There is no row:3 or column:3 and so the array out of index error.

public class sqr_Matrix_3x3                                                                
        {
            private double[,] _entries = new double[3, 3];
            public C_sqr_Matrix_3x3(double a,double b,double c,double d,double 
            e,double f,double g,double h,double i)
            {
                this._entries[1, 1] = a;
                this._entries[1, 2] = b;
                this._entries[1, 3] = c; <-- Error
                this._entries[2, 1] = d;
                this._entries[2, 2] = e;
                this._entries[2, 3] = f; <-- Error
                this._entries[3, 1] = g; <-- Error
                this._entries[3, 2] = h; <-- Error 
                this._entries[3, 3] = i; <-- Error 
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM