Hallo zusammen.
Ich habe ein kleines Problem bei meinem Programm zum Addieren, Subtrahieren und Multiplizieren von Matritzen. Das Aufrufen von “ReadMatrix” im Main funktioniert nicht. Vielleicht kann mir jemand helfen.
Danke schonmal
Lg
[CSHARP]
class Matrix
{
int n;
int m;
int[,] data;
public Matrix(int n, int m)
{
this.n = n;
this.m = m;
data = new int[n, m];
}
public void ReadMatrix1()
{
Console.WriteLine("
Please Enter Details of First Matrix");
Console.Write(“Number of Rows in First Matrix : “);
m = int.Parse(Console.ReadLine());
Console.Write(“Number of Columns in First Matrix : “);
n = int.Parse(Console.ReadLine());
int[,] A = new int[m, n];
Console.WriteLine(“Enter the elements of First Matrix : “);
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
try
{
Console.WriteLine(“Enter Element " + (1 + i).ToString() + " " + (1 + j).ToString());
A[i, j] = int.Parse(Console.ReadLine());
Console.WriteLine(” Value Accepted”);
}
catch
{
try
{
Console.WriteLine(“Enter Element " + (1 + i).ToString() + " " + (1 + j).ToString());
Console.WriteLine(”
Please Enter a Valid Value”);
A[i, j] = int.Parse(Console.ReadLine());
Console.WriteLine(” Value Accepted”);
}
catch
{
Console.WriteLine(”
Please Enter a Valid Value(Final Chance)”);
Console.WriteLine(“Enter Element " + (1 + i).ToString() + " " + (1 + j).ToString());
A[i, j] = int.Parse(Console.ReadLine());
Console.WriteLine(” Value Accepted");
}
}
}
}
}
public void ReadMatrix2()
{
Console.WriteLine(“Please Enter Details of Second Matrix”);
Console.Write(“Number of Rows in Second Matrix :”);
m = int.Parse(Console.ReadLine());
Console.Write(“Number of Columns in Second Matrix :”);
n = int.Parse(Console.ReadLine());
int[,] B = new int[m, n];
Console.WriteLine(“Please Enter Elements of Second Matrix:”);
for (int i = 0; i < B.GetLength(0); i++)
{
for (int j = 0; j < B.GetLength(1); j++)
{
try
{
Console.WriteLine(“Enter Element " + (1 + i).ToString() + " " + (1 + j).ToString());
B[i, j] = int.Parse(Console.ReadLine());
Console.WriteLine(” Value Accepted");
}
catch
{
try
{
Console.WriteLine("
Please Enter a Valid Value");
B[i, j] = int.Parse(Console.ReadLine());
Console.WriteLine(" Value Accepted");
}
catch
{
Console.WriteLine("
Please Enter a Valid Value(Final Chance)");
B[i, j] = int.Parse(Console.ReadLine());
Console.WriteLine(" Value Accepted");
}
}
}
}
}
public void Print()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(data[i, j] + " ");
}
Console.WriteLine();
}
}
public static Matrix operator +(Matrix A, Matrix B)
{
Matrix C= new Matrix(A.n, A.m);
for (int i = 0; i < A.n; i++)
{
for (int j = 0; j < A.m; j++)
{
C.data[i,j] = A.data[i,j] + B.data[i,j];
}
}
return C;
}
}
public static void Main(string[] args)
{
bool isRunning = true;
while (isRunning)
{
int n = int.Parse(Console.ReadLine());
int m = int.Parse(Console.ReadLine());
Matrix A = new Matrix(n, m);
A.ReadMatrix1();
Matrix B = new Matrix(n, m);
B.ReadMatrix2();
Console.WriteLine("**** MENU For Matrix Operations *****");
Console.WriteLine(" 1. Addition");
Console.WriteLine(" 2. Subtraction");
Console.WriteLine(" 3. Multiplication");
Console.WriteLine(" 4. End");
Console.WriteLine("Please Choose The Operation You Want.");
int choice = Convert.ToInt16(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine();
Matrix C = A + B;
C.Print();
break;
[/CSHARP]