Creation and use of matrixes in C++<\/span>\u00a0(multi – dimensional arrays)<\/p>\n The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y, you would write something as follows:<\/p>\n Where\u00a0type<\/strong>\u00a0can be any valid C++ data type and\u00a0arrayName<\/strong>\u00a0will be a valid C++ identifier.<\/p>\n A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array\u00a0a<\/strong>, which contains three rows and four columns can be shown as below:<\/p>\n Thus, every element in array a is identified by an element name of the form\u00a0a[ i ][ j ]<\/strong>, where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.<\/p>\n Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns.<\/p>\n The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example:<\/p>\n An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:<\/p>\n The above statement will take 4th element from the 3rd row of the array. You can verify it in the above digram.<\/p>\n When the above code is compiled and executed, it produces the following result:<\/p>\nTwo-Dimensional Arrays:<\/h2>\n
#d6d6d6;\">type arrayName <\/span>[<\/span> x <\/span>][<\/span> y <\/span>];<\/span><\/pre>\n
Initializing Two-Dimensional Arrays:<\/h2>\n
#d6d6d6;\">int<\/span> a<\/span>[<\/span>3<\/span>][<\/span>4<\/span>]<\/span> =<\/span> {<\/span> \n {<\/span>0<\/span>,<\/span> 1<\/span>,<\/span> 2<\/span>,<\/span> 3<\/span>}<\/span> ,<\/span> \/* initializers for row indexed by 0 *\/<\/span>\n {<\/span>4<\/span>,<\/span> 5<\/span>,<\/span> 6<\/span>,<\/span> 7<\/span>}<\/span> ,<\/span> \/* initializers for row indexed by 1 *\/<\/span>\n {<\/span>8<\/span>,<\/span> 9<\/span>,<\/span> 10<\/span>,<\/span> 11<\/span>}<\/span> \/* initializers for row indexed by 2 *\/<\/span>\n};<\/span><\/pre>\n
#d6d6d6;\">int<\/span> a<\/span>[<\/span>3<\/span>][<\/span>4<\/span>]<\/span> =<\/span> {<\/span>0<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>,<\/span>6<\/span>,<\/span>7<\/span>,<\/span>8<\/span>,<\/span>9<\/span>,<\/span>10<\/span>,<\/span>11<\/span>};<\/span><\/pre>\n
Accessing Two-Dimensional Array Elements:<\/h2>\n
#d6d6d6;\">int<\/span> val <\/span>=<\/span> a<\/span>[<\/span>2<\/span>][<\/span>3<\/span>];<\/span><\/pre>\n
#d6d6d6;\">#include<\/a><\/span>
#d6d6d6;\">a<\/span>[<\/span>0<\/span>][<\/span>0<\/span>]:<\/span> 0<\/span>\na<\/span>[<\/span>0<\/span>][<\/span>1<\/span>]:<\/span> 0<\/span>\na<\/span>[<\/span>1<\/span>][<\/span>0<\/span>]:<\/span> 1<\/span>\na<\/span>[<\/span>1<\/span>][<\/span>1<\/span>]:<\/span> 2<\/span>\na<\/span>[<\/span>2<\/span>][<\/span>0<\/span>]:<\/span> 2<\/span>\na<\/span>[<\/span>2<\/span>][<\/span>1<\/span>]:<\/span> 4<\/span>\na<\/span>[<\/span>3<\/span>