Course to learn about matrix (two dimensional array) in C# with exercises and solution


Hi,please someone can give me link to learn about matrix (two dimensional array) in C# with exercises and solution. Thanks in advance.


Asked by:- LuneAgile
0
: 2938 At:- 7/7/2018 2:55:19 PM
c# c# multidimensional array matrix







2 Answers
profileImage Answered by:- jaiprakash

You should be familiar with one dimensional arrays. The data in arrays may be any type. While a one dimensional array works for a sequence of data, we need something more for a two dimensional table, where data values vary over both row and column.

If we have a table of integers, for instance with three rows and four columns:

2 4  7 55
3 1  8 10
6 0 49 12

We could declare an array variable of the right size as

int[,] table = new int[3, 4];

Multiple indices are separated by commas inside the square brackets. In declaring an array type, no indices are included so the [,] indicates a two dimensional array. Where the new object is being created, the values inside the square brackets give each dimension.

In general the notation for a two dimensional array declaration is:

type [, ] variableName

and to create a new array with default values:

new type [ intExpression1, intExpression2 ]

where the expressions evaluate to integers for the dimension

C# Basic program for 2-dimensional array

using System;

class Program
{
    static void Main()
    {
        // ... Create 2D array of strings.
        string[,] array = new string[,]
        {
            {"cat", "dog"},
            {"bird", "fish"},
        };
        // ... Print out values.
        Console.WriteLine(array[0, 0]);
        Console.WriteLine(array[0, 1]);
        Console.WriteLine(array[1, 0]);
        Console.WriteLine(array[1, 1]);
    }
}

Output:

cat
dog
bird
fish

C# Program to Perform Matrix Addition

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication8

{

    class Program

    {

        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  3
Enter the First Matrix : 
1 2 3
2 3 4
3 4 5
Enter the Second Matrix :
1 2 3
2 1 4
1 1 5
Matrix A :
1 2 3
2 3 4
3 4 5
Matrix B :
1 2 3
2 1 4
1 1 5
Sum Matrix :
2 4 6
4 4 8
4 5 10

C# Program to Perform Matrix Multiplication

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace matrix_multiplication {
 class Program {
  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 First Matrix
8
7
6
10
First Matrix is :
8       7
6       10
Enter the Second Matrix
4
3
2
1
Second Matrix is :
4       3
2       1
Matrix multiplication is :
46      31
44      28

C# Program to Generate the Transpose of a Given Matrix

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program {
 class Program {
  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  2
Enter the Matrix Elements :
1 2
3 4
Matrix A :
1 2
3 4
Transpose Matrix :
1 3
2 4

Reference links, which may be helpful for you

http://anh.cs.luc.edu/170/notes/CSharpHtml/twodim.html

https://www.dotnetperls.com/2d

https://www.sanfoundry.com/csharp-programming-examples-on-matrix/

https://www.infoworld.com/article/3123384/application-development/how-to-work-with-jagged-arrays-in-c.html

Hope it clear your knowledge of C# two-dimensional array and matrix.

2
At:- 7/7/2018 3:45:00 PM


profileImage Answered by:- LuneAgile

Thank you so much  Jai Prakash . 

0
At:- 7/7/2018 3:56:40 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use