In: Computer Science
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt]
PHP |
C# |
Python |
|
Create a two-dimensional array |
|||
Initializing elements in the array |
|||
Access an element in the array |
Create two-dimensional array Initializing elements in the array:
PHP:
$arr = array
(
array(22,18),
array(15,13),
array(5,2),
array(17,15)
);
C#:
int[,] arr = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
python:
from array import * arr = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
Access element in array:
PHP:
To get access to the elements of the $arr array we must point to the two indices (row and column):
$arr[0, 0] value is 22
$arr[2,1] value is 2
$arr[3, 0] value is 17
C#:
To get access to the elements of the arr array we must point to the two indices (row and column):
arr[0, 0] value is 0
arr[2, 1] value is 4
arr[3,0] value is 3
python:
To get access to the elements of the arr array we must point to the two indices (row and column):
arr[2][1] value is 8
arr[1][1] value is 6
arr[3][3] vlaue is 6