Coding help... :(
Posted: February 26th, 2018, 7:10 pm
Hello guys. I was wondering if someone can help me out a bit. You're all experienced coders, and some of you have done stuff in C#, if I remember it right.
I'll try to include in short everything I know so far, and explain my code. I'm tinkering in visual studio (windows forms), and I'm trying to make matrix multiplication code. Note that only essential pieces of code are here, the rest works fine:
The result I get fro this code is for example, something like this:
if
A = 3, 2, 1
0, 2, 1
B = 1, 0
0, 1
1, 0
For C, I get:
C = 4, 2
4, 2
So, the first element of C[0,0] matrix is fine, the second also, but the second row of C, always ends out as a copy of the first row. I can't really spot the error.
I'll try to include in short everything I know so far, and explain my code. I'm tinkering in visual studio (windows forms), and I'm trying to make matrix multiplication code. Note that only essential pieces of code are here, the rest works fine:
Code: Select all
public partial class Form1 : Form
{
int[,] A = new int[2, 3];
int[,] B = new int[3, 2];
private void button1_Click(object sender, EventArgs e)
//this button creates elements of both matrices above randomly , and it works fine
private void button2_Click(object sender, EventArgs e)
{
int[,] C = new int[A.GetLength(0),B.GetLength(1)];
int n = 0;
lblMatc.Text = String.Empty; //this line later should be filled with elements of C array
for (int z = 0; z < A.GetLength(0); z++)
{
for (int y = 0; y < B.GetLength(1); y++)
{
for (int x = 0; x < A.GetLength(1); x++)
{
n = n + A[y, x] * B[x, y];
}
C[z, y] = n;
n = 0;
}
}
if
A = 3, 2, 1
0, 2, 1
B = 1, 0
0, 1
1, 0
For C, I get:
C = 4, 2
4, 2
So, the first element of C[0,0] matrix is fine, the second also, but the second row of C, always ends out as a copy of the first row. I can't really spot the error.