banner



How To Create Matrix In C#

In this article, we will learn:

  • Create a Matrix in C#
  • Matrix Addition in C#
  • Matrix Subtraction in C#
  • Matrix Multiplication in C#
  • Transpose a Matrix in C#

Let's start with how to create a matrix in C#.

1. Creation Of Matrix in C# :

            class MatrixProgram     {         public static void Main(string[] args)         {              {                 int i, j;                 int[,] arr1 = new int[3, 3];                  Console.Write("\n\nRead a 2D array of size 3x3 and print the matrix :\n");                 Console.Write("------------------------------------------------------\n");                  /* Stored values into the array*/                 Console.Write("Input elements in the matrix :\n");                 for (i = 0; i < 3; i++)                 {                     for (j = 0; j < 3; j++)                     {                         Console.Write("element - [{0},{1}] : ", i, j);                         arr1[i, j] = Convert.ToInt32(Console.ReadLine());                     }                 }                  Console.Write("\nThe matrix is : \n");                 for (i = 0; i < 3; i++)                 {                     Console.Write("\n");                     for (j = 0; j < 3; j++)                         Console.Write("{0}\t", arr1[i, j]);                 }                 Console.Write("\n\n");             }         }     }          

Output:

Read a 2D array of size 3×3 and print the matrix :
—————————————————–
Input element in the matrix :
element – [0,0] : 1
element – [0,1] : 2
element – [0,2] : 3
element – [1,0] : 4
element – [1,1] : 5
element – [1,2] : 6
element – [2,0] : 7
element – [2,1] : 8
element – [2,2] : 9

The matrix is:

1 2 3
4 5 6
7 8 9

Press any key to continue…

Matrix Operations

2. Matrix addition in C# :

            class MatrixAddition     {         public static void Main(string[] args)         {             int m, n, i, j;             Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");             m = Convert.ToInt16(Console.ReadLine());             n = Convert.ToInt16(Console.ReadLine());             int[,] A = new int[10, 10];             Console.Write("\nEnter The First Matrix : ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     A[i, j] = Convert.ToInt16(Console.ReadLine());                 }             }              int[,] B = new int[10, 10];             Console.Write("\nEnter The Second Matrix:");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     B[i, j] = Convert.ToInt16(Console.ReadLine());                 }             }             Console.Clear();             Console.WriteLine("\nMatrix A : ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(A[i, j] + "\t");                  }                 Console.WriteLine();             }             Console.WriteLine("\nMatrix B: ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(B[i, j] + "\t");                  }                 Console.WriteLine();             }             int[,] C = new int[10, 10];             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     C[i, j] = A[i, j] + B[i, j];                 }             }             Console.Write("\nSum Matrix :");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(C[i, j] + "\t");                  }                 Console.WriteLine();             }             Console.Read();         }     }          

Output:

Enter Number Of Rows And Columns Of Matrices A and B :3
2
Enter The First Matrix :5
4
8
6
1
3
Enter The Second Matrix :9
2
8
6
3
0
Sum Matrix:14 6
16 12
4 3

3. Matrix Subtraction in C# :

class MatrixSubtraction     {         public static void Main(string[] args)         {             int m, n, i, j;             Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");             m = Convert.ToInt16(Console.ReadLine());             n = Convert.ToInt16(Console.ReadLine());             int[,] A = new int[10, 10];             Console.Write("\nEnter The First Matrix : ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     A[i, j] = Convert.ToInt16(Console.ReadLine());                 }             }              int[,] B = new int[10, 10];             Console.Write("\nEnter The Second Matrix:");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     B[i, j] = Convert.ToInt16(Console.ReadLine());                 }             }             Console.Clear();             Console.WriteLine("\nMatrix A : ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(A[i, j] + "\t");                  }                 Console.WriteLine();             }             Console.WriteLine("\nMatrix B: ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(B[i, j] + "\t");                  }                 Console.WriteLine();             }             int[,] C = new int[10, 10];             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     C[i, j] = A[i, j] - B[i, j];                 }             }             Console.Write("\nDifference Matrix :");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(C[i, j] + "\t");                  }                 Console.WriteLine();             }             Console.Read();         }     }          

Output:

Enter Number Of Rows And Columns Of Matrices A and B : 2
3
Enter The First Matrix :7
6
9
4
8
2
Enter The Second Matrix :6
9
1
2
5
7
Matrix A:
7 6 9
4 8 2
Matrix B:
6 9 1
2 5 7
Difference Matrix:
1 -3 8
2 3 -5

4. Matrix Multiplication in C# :

            class MatrixMultiplication     {         public static void Main(string[] args)         {             int i, j, m, n;             Console.WriteLine("Enter the Number of Rows and Columns : ");             m = Convert.ToInt32(Console.ReadLine());             n = Convert.ToInt32(Console.ReadLine());             int[,] a = new int[m, n];             Console.WriteLine("Enter the First Matrix");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     a[i, j] = int.Parse(Console.ReadLine());                 }             }             Console.WriteLine("First matrix is:");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(a[i, j] + "\t");                 }                 Console.WriteLine();             }             int[,] b = new int[m, n];             Console.WriteLine("Enter the Second Matrix");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     b[i, j] = int.Parse(Console.ReadLine());                 }             }             Console.WriteLine("Second Matrix is :");             for (i = 0; i < 2; i++)             {                 for (j = 0; j < 2; j++)                 {                     Console.Write(b[i, j] + "\t");                 }                 Console.WriteLine();             }             Console.WriteLine("Matrix Multiplication is :");             int[,] c = new int[m, n];             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     c[i, j] = 0;                     for (int k = 0; k < 2; k++)                     {                         c[i, j] += a[i, k] * b[k, j];                     }                 }             }             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(c[i, j] + "\t");                 }                 Console.WriteLine();             }              Console.ReadKey();         }     }          

Output:

Enter the number of Rows and Columns:2
3
Enter the First Matrix:
7
6
1
2
3
8
First Matrix is :
7 6 1
2 3 8
Enter the Second Matrix:
4
9
1
7
3
8
Second Matrix is :
4 9
7 3
Matrix multiplication is :
70 81 55
29 27 26

5. Transpose of a matrix:

Transpose of a matrix is formed by turning all rows of a matrix into columns and columns into rows.

class MatrixTranspose     {         public static void Main(string[] args)         {             int m, n, i, j;             Console.Write("Enter the Order of the Matrix : ");             m = Convert.ToInt16(Console.ReadLine());             n = Convert.ToInt16(Console.ReadLine());             int[,] A = new int[10, 10];             Console.Write("\nEnter The Matrix Elements : ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     A[i, j] = Convert.ToInt16(Console.ReadLine());                 }             }             Console.Clear();             Console.WriteLine("\nMatrix A : ");             for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(A[i, j] + "\t");                  }                 Console.WriteLine();             }             Console.WriteLine("Transpose Matrix : ");              for (i = 0; i < m; i++)             {                 for (j = 0; j < n; j++)                 {                     Console.Write(A[j, i] + "\t");                  }                 Console.WriteLine();             }             Console.Read();         }     }          

Output:

Enter the Order of the Matrix :2
3
Enter the Matrix Elements:
7
2
9
1
6
3
Matrix A:
7 2 9
1 6 3
Transpose Matrix:
7 1 0
2 6 0
9 3 0

Thanks for visiting !!

© 2017, Csharp Star. All rights reserved.

How To Create Matrix In C#

Source: https://www.csharpstar.com/create-matrix-and-different-matrix-operations-in-csharp/

Posted by: perezuncer1996.blogspot.com

0 Response to "How To Create Matrix In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel