Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Matrices in C#?
    text
    copied!<p>I'm trying to add two matrices together in C# using some simple for loops. I store the results in a data grid view. However, the last cell does not seem to add. I've been looking at this code for a while now and can't seem to figure it out. Did I do something wrong?</p> <pre><code> // Adds two matrices together using arrays. private void menuItemAdd_Click(object sender, EventArgs e) { // Create two 2-D arrays int[,] matrixOne = new int[dgvMatrixOne.RowCount, dgvMatrixOne.ColumnCount]; int[,] matrixTwo = new int[dgvMatrixTwo.RowCount, dgvMatrixTwo.ColumnCount]; // The rows of the total matrix match the rows of the first matrix. dgvMatrixTotal.RowCount = dgvMatrixOne.RowCount; // The columns of the total matrix match the columns of the first matrix. dgvMatrixTotal.ColumnCount = dgvMatrixOne.ColumnCount; // Fill matrix one with the data in the data grid matrix one. for (int i = 0; i &lt; dgvMatrixOne.RowCount; i++) { for (int j = 0; j &lt; dgvMatrixOne.ColumnCount; j++) { matrixOne[i, j] = Convert.ToInt32(dgvMatrixOne[i, j].Value); } } // Fill matrix two with the data in the data grid matrix two. for (int i = 0; i &lt; dgvMatrixTwo.RowCount; i++) { for (int j = 0; j &lt; dgvMatrixTwo.ColumnCount; j++) { matrixTwo[i, j] = Convert.ToInt32(dgvMatrixTwo[i, j].Value); } } // Set the total data grid to matrix one + matrix two. for (int i = 0; i &lt; dgvMatrixOne.RowCount; i++) { for (int j = 0; j &lt; dgvMatrixOne.ColumnCount; j++) { dgvMatrixTotal[i, j].Value = matrixOne[i, j] + matrixTwo[i, j]; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload