arrays multiplication! i need help asap!

Updated on February 7, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on February 7, 2018

hello

can you tell me whats wrong with my code?

//im not getting anything close to the right answer!

namespace Array_multiplication
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[2, 2] { {2, 4}, {3, 5} };
int[,] b = new int[2, 2] { {1, 6}, {4, 8} };
int[,] c = new int[2, 2];

for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 2; col++)
{
for (int i = 0; i < 2; i++)
{
c[row, col] = 0;
c[row, col] = c[row, col] + a[row, i] * b[i, col];
Console.Write(c[row, col] + ” “);
}
}
Console.WriteLine();
}
}
}
}

 

  • Liked by
Reply
0 on February 7, 2018

You don’t need the third loop. What is happening is that loop is firing twice before you change the index of array c causing you to overwrite the data stored at said index. Also you don’t need to assign the value 0 to the array index. Arrays when created if no value is given will be filled out with a default value for the data type of the array. You can also don’t need to include the value of the index in the math if you are all ways setting it to 0 to begin with.

  • Liked by
Reply
Cancel