COMPUTER PROGRAMMING 2
CHAPTER 6 PART 2
ARRAY IN C# (SPECIAL VARIABLE) PART II
MULTI-DIMENSIONAL ARRAYS
MULTI-DIMENSIONAL ARRAY
WHAT IS A MULTI-DIMENSIONAL ARRAY
• Multidimensional arrays have more than one
dimension (2, 3, …)
• The most important multidimensional arrays
are the 2-dimensional known as matrices or
tables.
WHAT IS A MULTI-DIMENSIONAL ARRAY
• Exampleof a matrix of integers with 2 rows and
4 columns:
0 1 2 3
0
5 0 -2 4
1
5 6 7 8
DECLARING AND CREATING MULTIDIMENSIONAL ARRAYS
• Declaring multidimensional arrays:
int[,] intMatrix;
float[,] floatMatrix;
string[,,] strCube;
DECLARING AND CREATING MULTIDIMENSIONAL ARRAYS
• Creating a multidimensional array:
• Use new keyword
• Must specify the size of each dimension
int[,] intMatrix = new int[3, 4];
float[,] floatMatrix = new float[8, 2];
INITIALIZING MULTIDIMENSIONAL ARRAYS WITH VALUES
• Creating and initializing with values
multidimensional array:
int[,] matrix =
{
{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8}, // row 1 values
};
// The matrix size is 2 x 4 (2 rows, 4 cols)
INITIALIZING MULTIDIMENSIONAL ARRAYS WITH VALUES
• Matrices are represented by a list of rows
• Rows consist of list of values
• The first dimension comes first, the
second comes next (inside the first)
ACCESSING THE ELEMENTS OF MULTIDIMENSIONAL ARRAYS
• Accessing N-dimensional array element:
nDimensionalArray[index1, … , indexn]
• Getting element value example:
int[,] array = {{1, 2}, {3, 4}}
int element11 = array[1, 1];
// element11 = 4
ACCESSING THE ELEMENTS OF MULTIDIMENSIONAL ARRAYS
• Setting element value example:
Number of
rows
int[,] array = new int[3, 4];
for (int row=0; row<array.GetLength(0); row++)
for (int col=0; col<array.GetLength(1); col++)
array[row, col] = row + col;
Number of
columns
READING A MATRIX – EXAMPLE
• Reading a matrix from the console
int rows = int.Parse(Console.ReadLine());
int columns = int.Parse(Console.ReadLine());
int[,] matrix = new int[rows, columns];
String inputNumber;
for (int row=0; row<rows; row++)
{
for (int column=0; column<cols; column++)
{
Console.Write("matrix[{0},{1}] = ", row, column);
inputNumber = Console.ReadLine();
matrix[row, column] = int.Parse(inputNumber);
}
}
PRINTING MATRIX – EXAMPLE
• Printing a matrix on the console:
for (int row=0; row<matrix.GetLength(0); row++)
{
for (int col=0; col<matrix.GetLength(1); col++)
{
Console.Write("{0} ", matrix[row, col]);
}
Console.WriteLine();
}
THE ARRAY CLASS
THE ARRAY CLASS
• The System.Array class
• Parent of all arrays
• All arrays inherit from it
• All arrays have the same:
• Basic functionality
• Basic properties
• E.g. Length property
METHODS OF ARRAY
• Important methods and properties of System.Array
• Rank – number of dimensions
• Length – number of all elements through all
dimensions
• GetLength(index) – returns the number of elements in
the specified dimension
• Dimensions are numbered from 0
METHODS OF ARRAY
• GetEnumerator() – returns IEnumerator for the array elements
• BinarySearch(…) – searches for a given element into a sorted array
(uses binary search)
• IndexOf(…) – searches for a given element and returns the index of
the first occurrence (if any)
• LastIndexOf(…) – searches for a given element and returns the last
occurrence index
• Copy(src, dest, len) – copies array elements; has many overloads
METHODS OF ARRAY
• Reverse(…) – inverts the arrays
elements upside down
• Clear(…) – assigns value 0 (null) for each elements
• CreateInstance(…) – creates an array
• Accepts as parameters the number of dimensions, start index and
number of elements
• Implements ICloneable, IList, ICollection and
IEnumerable interfaces
EXERCISE
• Write a program that fills and prints a matrix of size
(n, n) as shown below: (examples for n = 4)