2D arrays
 code { font-family: Consolas,"courier new"; background: #f4f4f4; border: 1px solid #ddd; border-left: 3px solid #f36d33; color: #666; page-break-inside: avoid; font-size: 15px; line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; overflow: auto; padding: 1em 1.5em; display: block; word-wrap: break-word; } 11.5.2.1 use the technical terms associated with arrays including upper and lower bounds  11.5.2.3 write program code using 1D and 2D arrays  Two dimentional arrays (Matrix) A two-dimensional array (2D array) is an array where each element is represented by a one-dimensional array. Each element of a two-dimensional array has two indices: row number (i) and column number (j). Programmers usually indicate the line number of the variable i , the number of the column of the variable j. You can get the value of an element by two indexes. For example, arr[2][3] A two-dimentional arra is used to create a table of data in rows and columns with the same data type. Create 2D Array We can create the two-dimensional array shown above: \$grid= array(     array("", "", "", "A", ""),     array("", "", "", "", " C "),     array("", "", "", "", ""),     array("", "", "B", "", ""),     array("", "", "", "", "") ); An array \$grid we can visually represent in a table: \$grid[0][3] = 'A' \$grid[3][2] = 'B' \$grid[1][4] = 'C' 2D iteration Nested loops are used to iterate over all elements of a two-dimensional array. FOR i = 0 to N       FOR j = 0 to N              ...      endFOR endFOR Examples: Fill 2D array random number (PHP) for (\$row = 0; \$row < 6; \$row++){       for (\$col = 0; \$col < 5; \$col++) {             \$grid[\$row][\$col]=rand(1, 100);       } } Output 2D array in table (PHP) echo "";   for (\$row = 0; \$row < 6; \$row++) {     echo "
Row number \$row ".\$grid[\$row][\$col]."
";       echo "";       for (\$col = 0; \$col < 5; \$col++) {             echo "";       }       echo ""; } echo ""; Next line print "
";   Questions: Exercises: Tasks: .u-star-rating-13 { list-style:none; margin:0px; padding:0px; width:65px; height:13px; position:relative; background: url('/.s/t/2001/rating13.png') top left repeat-x } .u-star-rating-13 li{ padding:0px; margin:0px; float:left } .u-star-rating-13 li a { display:block;width:13px;height: 13px;line-height:13px;text-decoration:none;text-indent:-9000px;z-index:20;position:absolute;padding: 0px;overflow:hidden } .u-star-rating-13 li a:hover { background: url('/.s/t/2001/rating13.png') left center;z-index:2;left:0px;border:none } .u-star-rating-13 a.u-one-star { left:0px } .u-star-rating-13 a.u-one-star:hover { width:13px } .u-star-rating-13 a.u-two-stars { left:13px } .u-star-rating-13 a.u-two-stars:hover { width:26px } .u-star-rating-13 a.u-three-stars { left:26px } .u-star-rating-13 a.u-three-stars:hover { width:39px } .u-star-rating-13 a.u-four-stars { left:39px } .u-star-rating-13 a.u-four-stars:hover { width:52px } .u-star-rating-13 a.u-five-stars { left:52px } .u-star-rating-13 a.u-five-stars:hover { width:65px } .u-star-rating-13 li.u-current-rating { top:0 !important; left:0 !important;margin:0 !important;padding:0 !important;outline:none;background: url('/.s/t/2001/rating13.png') left bottom;position: absolute;height:13px !important;line-height:13px !important;display:block;text-indent:-9000px;z-index:1 } Категория: Algorithms | Добавил: bzfar77 (09.02.2023) Просмотров: 721 | Теги: array, nested loops, php | Рейтинг: 0.0/0
 Всего комментариев: 0