Guide)
Visual Studio 2015
Other Versions
Arrays can have more than one dimension. For example, the following declaration creates a twodimensional array of four rows and two columns.
C#
Array Initialization
You can initialize the array upon declaration, as is shown in the following example.
C#
// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };
// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);
System.Console.WriteLine(array3D[1, 1, 2]);
// Getting the total count of elements or the length of a given dimension.
var allLength = array3D.Length;
var total = 1;
for (int i = 0; i < array3D.Rank; i++) {
total *= array3D.GetLength(i);
}
System.Console.WriteLine("{0} equals {1}", allLength, total);
//
//
//
//
//
//
//
//
//
//
Output:
1
2
3
4
7
three
8
12
12 equals 12
You also can initialize the array without specifying the rank.
C#
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
If you choose to declare an array variable without initialization, you must use the new operator
to assign an array to the variable. The use of new is shown in the following example.
C#
int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}};
// Error
// OK
Similarly, the following example gets the value of a particular array element and assigns it to
variable elementValue.
C#
The following code example initializes the array elements to default values (except for jagged
arrays).
C#
int[,] array6 = new int[10, 10];
One
Dimensional Arrays
Ex
bool
TruthArray[12];
float angle[4];
angle[0] = 1.2;
angle[1] = 3.4;
angle[2] = 0.0;
angle[3] = 45.6;
angle[2] = 9.6;
cin
>> angle[2];
cout
<< angle[3];
y=
sqrt
( angle[1] );
Out
of
bounds array index:
float
tempurature
[] = { 0.0, 112.37, 98.6
5
Initializing Arrays
float
tempurature
[] = { 0.0, 112.37, 98.6 };
Aggregate Array Operations
Operation
I/O
Assignment
Arithmetic
Comparison
Argument Passage
Return from a
function
Allowed?
No
No
No
No
Reference only
No
Aggregate Operations
For example:
void
CopyArray
( const int x[], int y[], int size )
{
6
Aggregate Operations
For example:
void
CopyArray
( const int x[], int y[], int size )
{
for (int i=0; i<size; i++)
y[i
]=
x[i
];
}
Software Engineering
int
occupants[BUILDING_SIZE
];
This is pass
by
value by default
Example
float
alpha[NUM_ROWS][NUM_COLS
];
10
Processing 2
D Arrays
Sum Columns
for ( int
col
=0;
col
<NUM_COLS;
col
++ )
{
total = 0;
for ( int row=0; row<NUM_ROWS; row++ )
total +=
alpha[row][col
];
cout
<< Column sum: << total <<
\
n;
}
11
2
D Arrays and Function
To pass a 2
For example
void Copy( int X[][MAX_COL],
const int Y[][MAX_COL],
int MAX_ROW);
Activity