c-sharp
  1. c-sharp-program-to-multiply-two-matrices-by-passing-matrix-to-a-function

Program to Multiply Two Matrices by Passing Matrix to a Function - (C# Basic Programs)

Multiplying two matrices is a common operation in linear algebra. In this tutorial, we will discuss a program to multiply two matrices by passing them to a function in C# programming language.

Syntax

The syntax for passing matrices to a function in C# is as follows:

public static int[,] MatrixMultiply(int[,] matrix1, int[,] matrix2)
{
    // multiplication logic
    return resultMatrix;
}

Example

using System;

class Program
{
    public static int[,] MatrixMultiply(int[,] matrix1, int[,] matrix2)
    {
        int rows1 = matrix1.GetLength(0);
        int cols1 = matrix1.GetLength(1);
        int rows2 = matrix2.GetLength(0);
        int cols2 = matrix2.GetLength(1);

        int[,] resultMatrix = new int[rows1, cols2];

        for (int i = 0; i < rows1; i++)
        {
            for (int j = 0; j < cols2; j++)
            {
                int sum = 0;
                for (int k = 0; k < cols1; k++)
                {
                    sum += matrix1[i, k] * matrix2[k, j];
                }
                resultMatrix[i, j] = sum;
            }
        }

        return resultMatrix;
    }

    static void Main(string[] args)
    {
        int[,] matrix1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
        int[,] matrix2 = new int[,] { { 7, 8 }, { 9, 10 }, { 11, 12 } };

        int[,] resultMatrix = MatrixMultiply(matrix1, matrix2);

        for (int i = 0; i < resultMatrix.GetLength(0); i++)
        {
            for (int j = 0; j < resultMatrix.GetLength(1); j++)
            {
                Console.Write("{0} ", resultMatrix[i, j]);
            }
            Console.WriteLine();
        }
    }
}

Output:

58 64
139 154

Explanation

The program begins by defining a function MatrixMultiply that takes two integer matrices matrix1 and matrix2 as input and returns their matrix product as an int array. Inside the function, we first determine the number of rows and columns for each matrix using the GetLength method. We then create a new matrix resultMatrix with the dimensions of rows1 by cols2.

We then use nested loops to iterate over each row and column in the matrices and perform the matrix multiplication by taking the dot product of each row in matrix1 with each column in matrix2, and storing the result in resultMatrix. Finally, the function returns resultMatrix.

In the Main method, we define two matrices matrix1 and matrix2 and pass them to the MatrixMultiply function. We then output the resulting matrix using nested loops.

Use

Multiplying matrices is an important operation in many mathematical and scientific applications, such as computer graphics, physics simulations, and machine learning. This program demonstrates how to multiply two matrices by passing them to a function in C#, which is a fundamental concept in linear algebra.

Summary

In this tutorial, we have discussed a program to multiply two matrices by passing them to a function in C#. We have seen the syntax, example, explanation, use, and importance of matrix multiplication. By understanding and practicing matrix multiplication, programmers can develop a better understanding of linear algebra and apply this concept to a variety of scientific and mathematical problems.

Published on: