Answer the question
In order to leave comments, you need to log in
How does the Gauss method work in C#?
Hello. There is a C# code that solves SLAE using the Gaussian method. The problem is that I don't fully understand how it works. I only see what is being created, it seems, a flip matrix, but nothing more. I can't figure out what's going on. I would really appreciate a clarification.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Gauss_CommandLine
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Решение СЛАУ методом Гаусса");
Console.WriteLine();
Console.WriteLine("Для запуска программы введите 'Запуск'.");
Console.WriteLine("Для выхода из программы введите 'Выход'.");
Console.WriteLine("Примечание! Ввод чувствителен к регистру введнных символов.");
Console.WriteLine();
string Control;
Control = Console.ReadLine();
while (true)
{
if (Control == "Запуск")
{
Console.WriteLine();
Console.Write("Введите размерность матрицы: ");
int RowAm = int.Parse(Console.ReadLine());
double[,] MatrixCoef = new double[RowAm, RowAm];
double[] FreeCoef = new double[RowAm];
double Multi1, Multi2;
double[] Result = new double[RowAm];
Console.WriteLine();
Console.WriteLine("Введите коэффициенты и свободные члены матрицы:");
Console.WriteLine();
for (int i = 0; i < RowAm; i++)
{
for (int j = 0; j < RowAm; j++)
{
Console.Write($"A[{i + 1}][{j + 1}] = ");
MatrixCoef[i, j] = double.Parse(Console.ReadLine());
}
Console.Write($"B[{i + 1}] = ");
FreeCoef[i] = double.Parse(Console.ReadLine());
Console.WriteLine();
}
Console.WriteLine("Начальный вид матрицы:");
Console.WriteLine();
int MatrixRow = MatrixCoef.GetLength(0);
int MatrixCol = MatrixCoef.GetLength(1);
for (int i = 0; i < MatrixRow; i++)
{
for (int j = 0; j < MatrixCol; j++)
{
Console.Write(MatrixCoef[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Свободные члены матрицы:");
Console.WriteLine();
for (int i = 0; i < RowAm; i++)
{
Console.WriteLine($"B[{i + 1}] = {FreeCoef[i]}");
}
Console.WriteLine();
for (int k = 0; k < RowAm; k++)
{
for (int j = k + 1; j < RowAm; j++)
{
Multi1 = MatrixCoef[j, k] / MatrixCoef[k, k];
for (int i = k; i < RowAm; i++)
{
MatrixCoef[j, i] = MatrixCoef[j, i] - Multi1 * MatrixCoef[k, i];
}
FreeCoef[j] = FreeCoef[j] - Multi1 * FreeCoef[k];
}
}
for (int k = RowAm - 1; k >= 0; k--)
{
Multi1 = 0;
for (int j = k; j < RowAm; j++)
{
Multi2 = MatrixCoef[k, j] * Result[j];
Multi1 += Multi2;
}
Result[k] = (FreeCoef[k] - Multi1) / MatrixCoef[k, k];
}
Console.WriteLine("Конечный вид матрицы:");
Console.WriteLine();
for (int i = 0; i < MatrixRow; i++)
{
for (int j = 0; j < MatrixCol; j++)
{
Console.Write(MatrixCoef[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Корни матрицы:");
Console.WriteLine();
for (int i = 0; i < RowAm; i++)
{
Console.WriteLine($"X[{i + 1}] = {Result[i]}");
}
Console.WriteLine();
Console.WriteLine("Программа завершила решение.");
Console.WriteLine();
Console.WriteLine("Для повторного запуска программы введите 'Запуск'.");
Console.WriteLine("Для выхода из программы введите 'Выход'.");
Console.WriteLine();
Control = Console.ReadLine();
}
if (Control == "Выход")
{
Console.WriteLine();
Console.WriteLine("Программа завершила работу.");
Thread.Sleep(1000);
Environment.Exit(0);
break;
}
else
{
Console.WriteLine();
Console.WriteLine("Ошибка! Ввод недопустимого значения.");
Console.WriteLine();
Control = Console.ReadLine();
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question