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);
}
}
}
我试图调试并找到错误,但我仍然无法找到错误的位置。
答案 0 :(得分:2)
C_sqr_Matrix_3x3
的构造函数是问题之一。数组是零索引的。我还看到其他行,您将此对象视为1索引。
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 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;
}
答案 1 :(得分:1)
你在这里弄错了(见下文)。你的数组大小是[3,3]意味着行:0,1,2列0,1,2。没有行:3或列:3,因此数组超出索引错误。
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
}