Reader Level:
ARTICLE

Two Dimensional Array in TypeScript

Posted by Nitin Bhardwaj Articles | TypeScript November 26, 2012
In this article I will explain what a 2-Dimensional array is and how to use them in TypeScript with an example.
  • 0
  • 0
  • 8403

2-Dimensional Array in TypeScript

An array is an object that contains one or more items called elements. Each of these elements can be a primitive data type or an object. For instance, you can store numbers, strings, and Date objects in the same array. A two-dimensional arras is really nothing more than an array of arrays. It is usually represented with rows and columns. Two-dimensional arrays are used to store information that we normally represent in table format. All of the data in a two-dimensional array is of the same type.

Example

The following example describes how to use a 2-dimensional array in TypeScript. This example add two matrices i.e. computes the sum of two matrices and then prints it. The first user will be asked to enter the order of the matrix (such as the numbers of rows and columns) and then enter the elements of the two matrices. It works as follows. For example if the user entered the order as 2, 2 then there are two rows and two columns for the two matrices as in:

First Matrix Elements:
1 3
5 7
Second Matrix Elements:
1 1
4 3
Then the output of the program (the sum of the First and Second matrix) will be:
2 4
9 10

To see how it works use the following instructions.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is opened. Provide the name of your application, like "2_demensional_array", then click on the Ok button.

Step 2

After Step 1 your project has been created. The Solution Explorer, which is at the right side of your project, contains the js file, ts file, css file and html files.

Step 3

Add_matrix.ts

class two_dimensional

{

    addmatrix()

    {

        var first:number[][] = [[10],[10]];

        var second:number[][] = [[10],[10]];

        var i, j,m,n;

        var sum:number[][] = [[10],[10]];

        document.write("<h2>Two Dimension Array</h2><hr>");

        m = parseInt(prompt("Enter the number of Rows of Matrix"));

        n = parseInt(prompt("Enter the number of Columns of Matrix"));

       

        document.write("<b>1st Matrix Elements :</b>");

        for(i = 0; i < m; ++i)

        {

            for(j = 0; j < n; ++j)

            {

                first[i][j]=parseInt(prompt("Enter the element of First Matrix -> Row " + (i) + " And Coloumn " + (j)));

            }

        }      

        document.write("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;");

            for(i = 0; i < m; i++ )

            {

                for(j = 0; j < n; j++)

                {

                    document.write(" &nbsp;&nbsp;&nbsp;&nbsp;"+first[i][j]);

                }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

            }

       

        document.write("<br>");

        document.write("<b>2nd matrix Elements :</b>");

       

        for(i = 0; i < m; i++)

        {

           for(j = 0; j < n; j++)

            {

                 second[i][j]=parseInt(prompt("Enter the element of Second Matrix -> Row " + (i) + " And Coloumn " + (j)));

            }

        }      

         document.write("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;");

 

         for (i = 0; i < m; i++)

         {

             for (j = 0; j < n; j++)

             {

                 document.write(" &nbsp;&nbsp;&nbsp;&nbsp;" + second[i][j]);

 

             }

             document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

         }

 

            for(i = 0; i < m; i++ )

            {

                for (j = 0; j < n; j++)

                {

                    sum[i][j] = first[i][j] + second[i][j];

                }

         }

 

        document.write("<br>");

        document.write("<b>Sum of Enter Matrices :</b>");

        document.write("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;");

 

         for (i = 0; i < m; i++)

         {

             for (j = 0; j < n; j++)

             {

                 document.write(" &nbsp;&nbsp;&nbsp;&nbsp;" + sum[i][j]);

             }

             document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

         }

    }

}

window.onload = () => {

    var matrix = new two_dimensional();

    matrix.addmatrix();

};


Demo.html

 

<!DOCTYPEhtml>

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>2-Dimensional Array</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h1>Add Two Matrix in TypeScript</h1>

    <divid="content"/>

</body>

</html>


app.js

 

var two_dimensional = (function () {

    function two_dimensional() { }

    two_dimensional.prototype.arraysum = function () {

        var first = [

            [

                10

            ],

            [

                10

            ]

        ];

        var second = [

            [

                10

            ],

            [

                10

            ]

        ];

        var i;

        var j;

        var m;

        var n;

 

        var sum = [

            [

                10

            ],

            [

                10

            ]

        ];

        document.write("<h2>Two Dimension Array</h2><hr>");

        m = parseInt(prompt("Enter the number of Rows of Matrix"));

        n = parseInt(prompt("Enter the number of Columns of Matrix"));

        document.write("<b>1st Matrix Elements :</b>");

        for(i = 0; i < m; ++i) {

            for(j = 0; j < n; ++j) {

                first[i][j] = parseInt(prompt("Enter the element of First Matrix -> Row " + (i) + " And Coloumn " + (j)));

            }

        }

        document.write("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;");

        for(i = 0; i < m; i++) {

            for(j = 0; j < n; j++) {

                document.write(" &nbsp;&nbsp;&nbsp;&nbsp;" + first[i][j]);

            }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        }

        document.write("<br>");

        document.write("<b>2nd matrix Elements :</b>");

        for(i = 0; i < m; i++) {

            for(j = 0; j < n; j++) {

                second[i][j] = parseInt(prompt("Enter the element of Second Matrix -> Row " + (i) + " And Coloumn " + (j)));

            }

        }

        document.write("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;");

        for(i = 0; i < m; i++) {

            for(j = 0; j < n; j++) {

                document.write(" &nbsp;&nbsp;&nbsp;&nbsp;" + second[i][j]);

            }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        }

        for(i = 0; i < m; i++) {

            for(j = 0; j < n; j++) {

                sum[i][j] = first[i][j] + second[i][j];

            }

        }

        document.write("<br>");

        document.write("<b>Sum of Enter Matrices :</b>");

        document.write("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;");

        for(i = 0; i < m; i++) {

            for(j = 0; j < n; j++) {

                document.write(" &nbsp;&nbsp;&nbsp;&nbsp;" + sum[i][j]);

            }

            document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;");

        }

    };

    return two_dimensional;

})();

window.onload = function () {

    var matrix = new two_dimensional();

    matrix.arraysum();

};

 

Output 1

Enter the number of rows.


enter-row.jpg


Enter the number of columns.


enter-column.jpg


Output 2

Enter the elements of the First Matrix.


first-matrix-element-1.jpg


first-matrix-element-2.jpg


first-matrix-element-3.jpg


first-matrix-element-4.jpg


Output 3

Then enter the elements of the Second Matrix.


second-matrix-element-1.jpg


second-matrix-element-2.jpg


second-matrix-element-3.jpg


second-matrix-element-4.jpg


Then click on Ok.


Output 4

The sum of the specified matrices is:


final-result.jpg

COMMENT USING

Trending up