Найти вектор матрицы: программа неправильно считает и выдает нули - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication13
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {//обьявление матриц
  11. string str;
  12. int[,] A = new int[4, 4];
  13. int[,] B = new int[4, 4];
  14. //заполнение с клавы
  15. int i, j;
  16. for (i = 0; i < 4; i++)
  17. {
  18. for (j = 0; j < 4; j++)
  19. {
  20. Console.WriteLine(" A[" + i + " , " + j + "] = ");
  21. str=Console.ReadLine();
  22. A[i, j] = Convert.ToInt32(str);
  23. }
  24. }
  25.  
  26. for (i = 0; i < 4; i++)
  27. {
  28. for (j = 0; j < 4; j++)
  29. {Console.WriteLine(" B[" + i + " , " + j + "] = ");
  30. str = Console.ReadLine();
  31. B[i, j] = Convert.ToInt32(str);
  32. }
  33. }
  34. Vector(A, B);
  35. Console.WriteLine(string.Join(" ", Vector(A, B)));
  36. }
  37. //функция вектора и поиска минимального элемента
  38. public static int[] Vector (int[,] A, int[,] B)
  39. {
  40. int minA = A[0, 0];
  41. int minB = B[0, 0];
  42. int colA = 0;
  43. int colB = 0;
  44. for (int j = 0; j < 4; ++j)
  45. {
  46. if (minA > A[j, j]) { minA = A[j, j]; colA = j; }
  47. if (minB > B[j, j]) { minB = B[j, j]; colB = j; }
  48. }
  49. int[] resultVector = new int[4];
  50. for (int j = 0; j < j; ++j)
  51. resultVector[j] = A[colA, j] - B[colB, j];
  52. return resultVector;
  53. }
  54. }
  55. }

Решение задачи: «Найти вектор матрицы: программа неправильно считает и выдает нули»

textual
Листинг программы
  1. using System;
  2.  
  3. namespace Matrixes
  4. {
  5.     public class Matrix
  6.     {
  7.         public class MatrixException : SystemException
  8.         {
  9.             public MatrixException(string message) : base(message) { }
  10.         }
  11.  
  12.         private double[,] table;
  13.  
  14.         public Matrix(int width, int height)
  15.         {
  16.             if (width < 1)
  17.                 throw new MatrixException("Invalid matrix width.");
  18.             if (height < 1)
  19.                 throw new MatrixException("Invalid matrix height.");
  20.             table = new double[height, width];
  21.         }
  22.  
  23.         public Matrix(double[,] array)
  24.         {
  25.             if ((array.GetLength(0) < 1) || (array.GetLength(1) < 1))
  26.                 throw new MatrixException("Bad array input.");
  27.             table = (double[,])array.Clone();
  28.         }
  29.  
  30.         public int Width
  31.         {
  32.             get
  33.             {
  34.                 return table.GetLength(1);
  35.             }
  36.         }
  37.        
  38.         public int Height
  39.         {
  40.             get
  41.             {
  42.                 return table.GetLength(0);
  43.             }
  44.         }
  45.  
  46.         public override string ToString()
  47.         {
  48.             var result = "";
  49.             for (int i = 0; i < Height; i++)
  50.             {
  51.                 for (int j = 0; j < Width; j++)
  52.                     result += "\t" + table[i, j];
  53.                 if (i < Height - 1)
  54.                     result += "\n";
  55.             }
  56.             return result;
  57.         }
  58.  
  59.         public double this[int i, int j]
  60.         {
  61.             get
  62.             {
  63.                 if ((i < 0) || (i >= Width))
  64.                     throw new MatrixException("Width index out of range.");
  65.                 if ((j < 0) || (j >= Height))
  66.                     throw new MatrixException("Height index out of range.");
  67.                 return table[j, i];
  68.             }
  69.             private set
  70.             {
  71.                 table[j, i] = value;
  72.             }
  73.         }
  74.  
  75.         private static Matrix Multiply(Matrix matrix, double value)
  76.         {
  77.             if (matrix == null)
  78.                 throw new MatrixException("Null matrix reference.");
  79.             var result = new Matrix(matrix.Width, matrix.Height);
  80.             for (int i = 0; i < result.Width; i++)
  81.                 for (int j = 0; j < result.Height; j++)
  82.                     result[i, j] = matrix[i, j] * value;
  83.             return result;
  84.         }
  85.  
  86.         private static Matrix Multiply(Matrix matrix0, Matrix matrix1)
  87.         {
  88.             if ((matrix0 == null) || (matrix1 == null))
  89.                 throw new MatrixException("Null matrix reference.");
  90.             if (matrix0.Width != matrix1.Height)
  91.                 throw new MatrixException("Invalid matrix sizes to multiply.");
  92.  
  93.             Matrix result = new Matrix(matrix1.Width, matrix0.Height);
  94.             for (var i = 0; i < matrix0.Height; i++)
  95.                 for (var j = 0; j < matrix1.Width; j++)
  96.                     for (var k = 0; k < matrix0.Width; k++)
  97.                         result[j, i] += matrix0[k, i] * matrix1[j, k];
  98.             return result;
  99.         }
  100.  
  101.         private static Matrix Sum(Matrix matrix, double value)
  102.         {
  103.             if (matrix == null)
  104.                 throw new MatrixException("Null matrix reference.");
  105.             var result = new Matrix(matrix.Width, matrix.Height);
  106.             for (int i = 0; i < result.Width; i++)
  107.                 for (int j = 0; j < result.Height; j++)
  108.                     result[i, j] = matrix[i, j] + value;
  109.             return result;
  110.         }
  111.  
  112.         private static bool IsSame(Matrix matrix0, Matrix matrix1)
  113.         {
  114.             if (ReferenceEquals(matrix0, matrix1))
  115.                 return true;
  116.             if (((object)matrix0 == null) || ((object)matrix1 == null))
  117.                 return false;
  118.             if ((matrix0.Width != matrix1.Width) || (matrix0.Height != matrix1.Height))
  119.                 return false;
  120.  
  121.             for (var i = 0; i < matrix0.Width; i++)
  122.                 for (var j = 0; j < matrix0.Height; j++)
  123.                     if (matrix0[i, j] != matrix1[i, j])
  124.                         return false;
  125.             return true;
  126.         }
  127.  
  128.         private static Matrix Pow(Matrix matrixBase, int exp)
  129.         {
  130.             if (matrixBase == null)
  131.                 throw new MatrixException("Null matrix reference.");
  132.             if (matrixBase.Width != matrixBase.Height)
  133.                 throw new MatrixException("Can not pow matrix with different width and height.");
  134.             if (exp < 1)
  135.                 throw new MatrixException("Invalid exponental value.");
  136.             var result = new Matrix(matrixBase.table);
  137.             for (int i = 1; i < exp; i++)
  138.                 result *= matrixBase;
  139.             return result;
  140.         }
  141.  
  142.         public Matrix Transpose()
  143.         {
  144.             var result = new Matrix(Height, Width);
  145.             for (var i = 0; i < result.Width; i++)
  146.                 for (var j = 0; j < result.Height; j++)
  147.                     result[i, j] = table[i, j];
  148.             return result;
  149.         }
  150.  
  151.         public static Matrix Identity(int size)
  152.         {
  153.             if (size < 1)
  154.                 throw new Exception("Invalid matrix size.");
  155.             var result = new Matrix(size, size);
  156.             for (int i = 0; i < size; i++)
  157.                 result[i, i] = 1;
  158.             return result;
  159.         }
  160.  
  161.         public static Matrix Random(int width, int height, double minValue, double maxValue, bool roundToInt = true)
  162.         {
  163.             if (width < 1)
  164.                 throw new MatrixException("Invalid matrix width.");
  165.             if (height < 1)
  166.                 throw new MatrixException("Invalid matrix height.");
  167.             var result = new Matrix(width, height);
  168.             Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
  169.             if (!roundToInt)
  170.                 for (var i = 0; i < width; i++)
  171.                     for (var j = 0; j < height; j++)
  172.                         result[i, j] = minValue + (maxValue - minValue) * (float)random.NextDouble();
  173.             else
  174.                 for (var i = 0; i < width; i++)
  175.                     for (var j = 0; j < height; j++)
  176.                         result[i, j] = (int)(minValue + (maxValue - minValue) * (float)random.NextDouble() + .5);
  177.             return result;
  178.         }
  179.  
  180.         public static bool operator ==(Matrix left, Matrix right)
  181.         {
  182.             return IsSame(left, right);
  183.         }
  184.  
  185.         public static bool operator !=(Matrix left, Matrix right)
  186.         {
  187.             return !IsSame(left, right);
  188.         }
  189.  
  190.         public static Matrix operator *(Matrix left, double right)
  191.         {
  192.             return Multiply(left, right);
  193.         }
  194.  
  195.         public static Matrix operator *(double left, Matrix right)
  196.         {
  197.             return Multiply(right, left);
  198.         }
  199.  
  200.         public static Matrix operator *(Matrix left, Matrix right)
  201.         {
  202.             return Multiply(left, right);
  203.         }
  204.  
  205.         public static Matrix operator +(Matrix left, double right)
  206.         {
  207.             return Sum(left, right);
  208.         }
  209.  
  210.         public static Matrix operator +(double left, Matrix right)
  211.         {
  212.             return Sum(right, left);
  213.         }
  214.  
  215.         public static Matrix operator ^(Matrix left, int right)
  216.         {
  217.             return Pow(left, right);
  218.         }
  219.     }
  220. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.231 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы