Найти вектор матрицы: программа неправильно считает и выдает нули - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{//обьявление матриц
string str;
int[,] A = new int[4, 4];
int[,] B = new int[4, 4];
//заполнение с клавы
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
Console.WriteLine(" A[" + i + " , " + j + "] = ");
str=Console.ReadLine();
A[i, j] = Convert.ToInt32(str);
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{Console.WriteLine(" B[" + i + " , " + j + "] = ");
str = Console.ReadLine();
B[i, j] = Convert.ToInt32(str);
}
}
Vector(A, B);
Console.WriteLine(string.Join(" ", Vector(A, B)));
}
//функция вектора и поиска минимального элемента
public static int[] Vector (int[,] A, int[,] B)
{
int minA = A[0, 0];
int minB = B[0, 0];
int colA = 0;
int colB = 0;
for (int j = 0; j < 4; ++j)
{
if (minA > A[j, j]) { minA = A[j, j]; colA = j; }
if (minB > B[j, j]) { minB = B[j, j]; colB = j; }
}
int[] resultVector = new int[4];
for (int j = 0; j < j; ++j)
resultVector[j] = A[colA, j] - B[colB, j];
return resultVector;
}
}
}Решение задачи: «Найти вектор матрицы: программа неправильно считает и выдает нули»
textual
Листинг программы
using System;
namespace Matrixes
{
public class Matrix
{
public class MatrixException : SystemException
{
public MatrixException(string message) : base(message) { }
}
private double[,] table;
public Matrix(int width, int height)
{
if (width < 1)
throw new MatrixException("Invalid matrix width.");
if (height < 1)
throw new MatrixException("Invalid matrix height.");
table = new double[height, width];
}
public Matrix(double[,] array)
{
if ((array.GetLength(0) < 1) || (array.GetLength(1) < 1))
throw new MatrixException("Bad array input.");
table = (double[,])array.Clone();
}
public int Width
{
get
{
return table.GetLength(1);
}
}
public int Height
{
get
{
return table.GetLength(0);
}
}
public override string ToString()
{
var result = "";
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
result += "\t" + table[i, j];
if (i < Height - 1)
result += "\n";
}
return result;
}
public double this[int i, int j]
{
get
{
if ((i < 0) || (i >= Width))
throw new MatrixException("Width index out of range.");
if ((j < 0) || (j >= Height))
throw new MatrixException("Height index out of range.");
return table[j, i];
}
private set
{
table[j, i] = value;
}
}
private static Matrix Multiply(Matrix matrix, double value)
{
if (matrix == null)
throw new MatrixException("Null matrix reference.");
var result = new Matrix(matrix.Width, matrix.Height);
for (int i = 0; i < result.Width; i++)
for (int j = 0; j < result.Height; j++)
result[i, j] = matrix[i, j] * value;
return result;
}
private static Matrix Multiply(Matrix matrix0, Matrix matrix1)
{
if ((matrix0 == null) || (matrix1 == null))
throw new MatrixException("Null matrix reference.");
if (matrix0.Width != matrix1.Height)
throw new MatrixException("Invalid matrix sizes to multiply.");
Matrix result = new Matrix(matrix1.Width, matrix0.Height);
for (var i = 0; i < matrix0.Height; i++)
for (var j = 0; j < matrix1.Width; j++)
for (var k = 0; k < matrix0.Width; k++)
result[j, i] += matrix0[k, i] * matrix1[j, k];
return result;
}
private static Matrix Sum(Matrix matrix, double value)
{
if (matrix == null)
throw new MatrixException("Null matrix reference.");
var result = new Matrix(matrix.Width, matrix.Height);
for (int i = 0; i < result.Width; i++)
for (int j = 0; j < result.Height; j++)
result[i, j] = matrix[i, j] + value;
return result;
}
private static bool IsSame(Matrix matrix0, Matrix matrix1)
{
if (ReferenceEquals(matrix0, matrix1))
return true;
if (((object)matrix0 == null) || ((object)matrix1 == null))
return false;
if ((matrix0.Width != matrix1.Width) || (matrix0.Height != matrix1.Height))
return false;
for (var i = 0; i < matrix0.Width; i++)
for (var j = 0; j < matrix0.Height; j++)
if (matrix0[i, j] != matrix1[i, j])
return false;
return true;
}
private static Matrix Pow(Matrix matrixBase, int exp)
{
if (matrixBase == null)
throw new MatrixException("Null matrix reference.");
if (matrixBase.Width != matrixBase.Height)
throw new MatrixException("Can not pow matrix with different width and height.");
if (exp < 1)
throw new MatrixException("Invalid exponental value.");
var result = new Matrix(matrixBase.table);
for (int i = 1; i < exp; i++)
result *= matrixBase;
return result;
}
public Matrix Transpose()
{
var result = new Matrix(Height, Width);
for (var i = 0; i < result.Width; i++)
for (var j = 0; j < result.Height; j++)
result[i, j] = table[i, j];
return result;
}
public static Matrix Identity(int size)
{
if (size < 1)
throw new Exception("Invalid matrix size.");
var result = new Matrix(size, size);
for (int i = 0; i < size; i++)
result[i, i] = 1;
return result;
}
public static Matrix Random(int width, int height, double minValue, double maxValue, bool roundToInt = true)
{
if (width < 1)
throw new MatrixException("Invalid matrix width.");
if (height < 1)
throw new MatrixException("Invalid matrix height.");
var result = new Matrix(width, height);
Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
if (!roundToInt)
for (var i = 0; i < width; i++)
for (var j = 0; j < height; j++)
result[i, j] = minValue + (maxValue - minValue) * (float)random.NextDouble();
else
for (var i = 0; i < width; i++)
for (var j = 0; j < height; j++)
result[i, j] = (int)(minValue + (maxValue - minValue) * (float)random.NextDouble() + .5);
return result;
}
public static bool operator ==(Matrix left, Matrix right)
{
return IsSame(left, right);
}
public static bool operator !=(Matrix left, Matrix right)
{
return !IsSame(left, right);
}
public static Matrix operator *(Matrix left, double right)
{
return Multiply(left, right);
}
public static Matrix operator *(double left, Matrix right)
{
return Multiply(right, left);
}
public static Matrix operator *(Matrix left, Matrix right)
{
return Multiply(left, right);
}
public static Matrix operator +(Matrix left, double right)
{
return Sum(left, right);
}
public static Matrix operator +(double left, Matrix right)
{
return Sum(right, left);
}
public static Matrix operator ^(Matrix left, int right)
{
return Pow(left, right);
}
}
}