create matrix multiplication in csharp

Here's one way to perform matrix multiplication in C# using nested loops and multidimensional arrays:

main.cs
// Define two input matrices and output matrix dimensions
int[,] matrixA = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
int[,] matrixB = new int[2, 3] { { 7, 8, 9 }, { 10, 11, 12 } };
int[,] matrixC = new int[3, 3];

// Perform matrix multiplication using nested loops
for (int i = 0; i < matrixA.GetLength(0); i++)
{
    for (int j = 0; j < matrixB.GetLength(1); j++)
    {
        for (int k = 0; k < matrixA.GetLength(1); k++)
        {
            matrixC[i, j] += matrixA[i, k] * matrixB[k, j];
        }
    }
}

// Output result matrix
for (int i = 0; i < matrixC.GetLength(0); i++)
{
    for (int j = 0; j < matrixC.GetLength(1); j++)
    {
        Console.Write(matrixC[i, j] + " ");
    }
    Console.WriteLine();
}
729 chars
27 lines

In this code, we first define the two input matrices (matrixA and matrixB) and the dimensions of the resulting output matrix (matrixC). We then use nested for loops to iterate over each element in matrixC, multiplying and accumulating the respective elements from matrixA and matrixB. Finally, we output the resulting matrixC to the console.

gistlibby LogSnag