Произведение двух матриц и ошибка "Индекс за пределами диапазона" - C#
Формулировка задачи:
Ситуация такова. Имеются две матрицы размерностью 8 на 8. Количество строк и столбцов двух матрицах можно изменять, но не превышать 8. Как таковой код работает, но при произведении матрицы А (5 на 8) и матрицы Б (8 на 4) должна быть результативная матрица 5 на 4, но в ходи выполнения процесса вылетает ошибка
"Индекс за пределами диапазона. Индекс должен быть положительным числом, а его размер не должен превышать размер коллекции. Имя параметра: index"
. При этом матрицу А (Произвольную на 8) и Б(8 на Произвольную) он не решает. Т.е. программе не нравятся прямоугольные матрицы. Хотя, в консольном варианте все работает верно без критов, а с ДатойГридВью критует.Помогите плз
Ниже приведен код:
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace SapsayLab2
- {
- public partial class Form1 : Form
- {
- string[] row1 = new string[] { "1", "1", "0", "0", "0", "0", "0", "0", "0", "0" };
- string[] row2 = new string[] { "1", "0", "1", "1", "0", "0", "0", "0", "0", "0" };
- string[] row3 = new string[] { "0", "1", "0", "1", "1", "0", "0", "0", "0", "0" };
- string[] row4 = new string[] { "0", "0", "1", "0", "1", "0", "0", "0", "0", "0" };
- string[] row5 = new string[] { "0", "0", "0", "0", "1", "0", "1", "0", "0", "0" };
- string[] row6 = new string[] { "0", "0", "0", "0", "0", "0", "0", "0", "1", "0" };
- string[] row7 = new string[] { "0", "0", "0", "0", "0", "0", "0", "0", "1", "0" };
- string[] row8 = new string[] { "0", "0", "0", "0", "0", "0", "0", "0", "0", "1" };
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- FFM();
- FSM();
- }
- private void FFM(int cCount = 8, int rCount = 8)
- {
- dataGridView1.ColumnCount = cCount;
- List<string[]> rows = new List<string[]>();
- rows.Add(row1); rows.Add(row2); rows.Add(row3); rows.Add(row4); rows.Add(row5); rows.Add(row6); rows.Add(row7); rows.Add(row8);
- dataGridView1.AllowUserToAddRows = false;
- for (int i = 0; i < rows.Count(); i++)
- {
- if (i < rCount)
- {
- dataGridView1.Rows.Add(rows[i]);
- }
- }
- }
- private void FSM(int cCount = 8, int rCount = 8)
- {
- dataGridView2.ColumnCount = cCount;
- List<string[]> rows = new List<string[]>();
- rows.Add(row1); rows.Add(row2); rows.Add(row3); rows.Add(row4); rows.Add(row5); rows.Add(row6); rows.Add(row7); rows.Add(row8);
- dataGridView2.AllowUserToAddRows = false;
- for (int i = 0; i < rows.Count(); i++)
- {
- if (i < rCount)
- {
- dataGridView2.Rows.Add(rows[i]);
- }
- }
- }
- private void numericUpDown1_Click(object sender, EventArgs e)
- {
- dataGridView1.RowCount = 0;
- FFM((int)numericUpDown2.Value, (int)numericUpDown1.Value);
- }
- private void numericUpDown2_Click(object sender, EventArgs e)
- {
- dataGridView1.ColumnCount = 0;
- FFM((int)numericUpDown2.Value, (int)numericUpDown1.Value);
- }
- private void numericUpDown3_Click(object sender, EventArgs e)
- {
- dataGridView2.RowCount = 0;
- FSM((int)numericUpDown4.Value, (int)numericUpDown3.Value);
- }
- private void numericUpDown4_Click(object sender, EventArgs e)
- {
- dataGridView2.ColumnCount = 0;
- FSM((int)numericUpDown4.Value, (int)numericUpDown3.Value);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- int[,] M1;
- int[,] M2;
- M1 = new int[dataGridView1.RowCount, dataGridView1.ColumnCount];
- M2 = new int[dataGridView2.RowCount, dataGridView2.ColumnCount];
- dataGridView3.Rows.Clear();
- if (M1.GetLength(1) == M2.GetLength(0))
- {
- int[,] r = new int[M1.GetLength(0), M2.GetLength(1)];
- //dataGridView3.ColumnCount = M1.GetLength(1);
- //dataGridView3.RowCount = M2.GetLength(0);
- dataGridView3.RowCount = M1.GetLength(0);
- dataGridView3.ColumnCount = M2.GetLength(1);
- for (int i = 0; i < M1.GetLength(0); i++)
- {
- for (int j = 0; j < r.GetLength(1); j++)
- {
- for (int k = 0; k < r.GetLength(1); k++)
- {
- r[i, j] += Convert.ToInt32(dataGridView1[i, k].Value) * Convert.ToInt32(dataGridView2[k, j].Value);
- dataGridView3[i, j].Value = Convert.ToString(r[i, j]);
- textBox1.Text = Convert.ToString(numericUpDown1.Value);
- textBox2.Text = Convert.ToString(numericUpDown4.Value);
- }
- }
- }
- }
- else
- {
- MessageBox.Show("Югня не пашет");
- }
- }
- }
- }
Решение задачи: «Произведение двух матриц и ошибка "Индекс за пределами диапазона"»
textual
Листинг программы
- for (int j = 0; j < r.GetLength(1); j++)
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д