Перемножение двух матриц - C#
Формулировка задачи:
Здравствуйте, выполняю для саморазвития не большой пример по перемножению двух матриц на C#. И когда пытаюсь занести в массив рандомные значение, у меня прекращается работа программы и выдает ошибку о том,что "Индекс находился вне границ массива". Не могу понять в чем проблема,прощу помочь разобраться в этом.
Листинг программы
- class Program
- {
- static void Main(string[] args)
- {
- Random r = new Random();
- int a_rows, a_collumns, b_rows, b_collumns;
- Console.ForegroundColor = ConsoleColor.White;
- for (;;)
- {
- Console.WriteLine("Используйте свойство,что кол. столбцов первой матрицы \n должно равняться кол.строк второй матрицы");
- try
- {
- Console.WriteLine("Введите кол.строк первой матрицы");
- a_rows = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите кол.столбцов первой матрицы");
- a_collumns = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите кол.строк второй матрицы");
- b_rows = Convert.ToInt32(Console.ReadLine());
- if (a_collumns != b_rows)
- continue;
- Console.WriteLine("Введите кол.столбцов второй матрицы");
- b_collumns = Convert.ToInt32(Console.ReadLine());
- break;
- }
- catch
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Вы можете вводить только цифры.");
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- Matrix a = new Matrix(a_rows, a_collumns);
- Matrix b = new Matrix(b_rows, b_collumns);
- Matrix c = new Matrix(a_collumns, b_rows);
- for (int i = 0; i < a_rows; i++)
- for (int j = 0; j < a_collumns; i++)
- a[i, j] = r.Next(4,100);
- for (int i = 0; i < b_rows; i++)
- for (int j = 0; j < b_collumns; i++)
- b[i, j] = r.Next(4,100);
- a.show();
- b.show();
- c.show();
- }
- }
- class Matrix
- {
- int[,] array;
- int rows, collumns;
- public int Collumns
- {
- get { return collumns; }
- set
- {
- if (value > 0)
- collumns = value;
- }
- }
- public int Rows
- {
- get
- {return rows;}
- set
- {
- if (value > 0)
- rows = value;
- }
- }
- public int this[int i, int j] // индексатор
- {
- get { return array[i, j]; }
- set {
- array[i, j] = value; }
- }
- public Matrix(int countofRows, int countofCollumns)
- {
- Rows = countofRows;
- Collumns = countofCollumns;
- array = new int[Rows, Collumns];
- }
- public void show()
- {
- for (int i = 0; i < Rows; i++)
- {
- for (int j = 0; j < Collumns; j++)
- {
- Console.Write("{0,6}",array[i, j]);
- }
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
Решение задачи: «Перемножение двух матриц»
textual
Листинг программы
- i++
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д