Lab10 - Arrays2 - Sec450 C#
Lab10 - Arrays2 - Sec450 C#
1. Two-Dimensional Arrays
Arrays that we have seen and used so far are one dimensional arrays, where each element is
indexed by a single integer. C# allows programmers to create and use multi-dimensional arrays,
meaning that each of their elements is indexed by a fixed number of integers (e.g., two, three
or four). However, two-dimensional arrays are the most common. In this case, the array looks
like a table that can store multiple items rather than just a single row of items. Consider the
two arrays:
2 57 12 90 3 21
1 32 88 9 10 90
3 10 57 90 12 0 3 10 57 90 12
0 1 2 3 4 0 1 2 3 4
(a) One-dimensional array of size 4 (b) Two-dimensional array of size 3x4
The array on the left is considered a one-dimensional array, so its elements are indexed by a
single integer. For instance, the element with index 2 has the value of 57. In contrast, the array
on the right is a two-dimensional array which requires a pair of integers to index an element.
For example, the element with index (1,3) has the value of 10, and the element with index (2,4)
has the value of 21.
A two-dimensional array can be declared as follows:
DataType [ ,] ArrayName ;
where ArrayName is the name of the array, and DataType is the data type of each array element.
Similar to one-dimensional arrays, two-dimensional arrays must be initialized to a specified
size after the declaration prior to their use, as shown in the following syntax.
ArrayName = new DataType [M , N ];
The above statement will create a two-dimensional array of type DataType with M ×N items.
However, the value of each item is not specified.
Example 1.1: Declare an array, students, which stores students’ names in accordance with
their seats (row, column) in a class. Then initialize it for storing 5×3 students.
string [ ,] students ;
students = new string [5 ,3];
or
string [ ,] students = new string [5 ,3];
Lab #10
204111 – Computer and Programming Section 450
We can also initialize a two-dimensional array using bracketed lists of values as follows:
ArrayName = new DataType [M , N ] {
{ value (0 ,0) , value (0 ,1) , ... , value (0 ,N -1) } ,
{ value (1 ,0) , value (1 ,1) , ... , value (1 ,N -1) } ,
: : :
{ value (M -1 ,0) , value (M -1 ,1) , ... , value (M -1 ,N -1) }
};
or simply
ArrayName = {
{ value (0 ,0) , value (0 ,1) , ... , value (0 ,N -1) } ,
{ value (1 ,0) , value (1 ,1) , ... , value (1 ,N -1) } ,
: : :
{ value (M -1 ,0) , value (M -1 ,1) , ... , value (M -1 ,N -1) }
};
Example 1.2: Create an array of integers, called A, to represent a 4×3 matrix with the fol-
lowing members
5 3 8
2 6 10
A=
1
8 25
12 3 30
Unlike one dimensional arrays, each element in a two-dimensional array must be referenced
using two integers. For example, to access an element located at row R and column C, we can
use:
ArrayName [R , C ]
Note: The reason A[2,1] is used instead of A[3,2] is due to the fact that array indices
(both row and column) start at 0, while row and column indices of a matrix start at 1.
Lab #10
204111 – Computer and Programming Section 450
Exercise 1.1: Typethe following program in SharpDevelop then answer the ques-
tions
1 using System ;
2 class Matrix {
3 public static void Main () {
4 int i , j ;
5 int [ ,] A = new int [4 ,3] {
6 { 5 , 3 , 8} ,
7 { 2 , 6 , 10} ,
8 { 1 , 8 , 25} ,
9 {12 , 3 , 30}
10 };
11
12 for ( i = 0; i < 4; i ++) {
13 for ( j = 0; j < 3; j ++) {
14 Console . Write ( " {0 ,4} " , A [i , j ]);
15 }
16 Console . WriteLine ();
17 }
18 }
19 }
Lab #10
204111 – Computer and Programming Section 450
Exercise 1.2: Write a program to read a matrix from the user, then display the
entire matrix on the screen. The size of the matrix is also specified by the user.
using System ;
class Matrix {
public static void Main () {
int i , j ;
int nrow , ncol ;
int [ ,] A ;
A = new int [ __ ( a ) __ , __ ( b ) __ ];
for ( i = 0; i < nrow ; i ++)
for ( j = 0; j < ncol ; j ++) {
Console . Write ( " Enter A [{0} ,{1}]: " , i +1 , j +1);
___ ( c ) ___ = int . Parse ( Console . ReadLine ());
}
Lab #10
204111 – Computer and Programming Section 450
Position Expression/Statement
___(a)___
___(b)___
___(c)___
___(d)___
___(e)___
Sample output
Exercise 1.3: Enhance the program used in Exercise 1.2 so that it now also outputs
the transpose of A. Write the extended part in the box provided.
Lab #10
204111 – Computer and Programming Section 450
Sample output
Exercise 1.4: The following (incomplete) program intends to take two matrices from
the user. Then perform an addition operation on the two matrices and display the
result. Complete the program by filling in an appropriate expression/statement
for each of the positions marked as(a)–(g).
using System ;
class Matrix {
public static void Main () {
int i , j ;
int nrow , ncol ;
int [ ,] A ;
int [ ,] B ;
A = new int [ __ ( a ) __ , __ ( b ) __ ];
for ( i = 0; i < nrow ; i ++)
for ( j = 0; j < ncol ; j ++) {
Console . Write ( " Enter A [{0} ,{1}]: " , i +1 , j +1);
___ ( c ) ___ = int . Parse ( Console . ReadLine ());
}
B = new int [ __ ( d ) __ , __ ( e ) __ ];
for ( i = 0; i < nrow ; i ++)
for ( j = 0; j < ncol ; j ++) {
Console . Write ( " Enter B [{0} ,{1}]: " , i +1 , j +1);
___ ( f ) ___ = int . Parse ( Console . ReadLine ());
}
Lab #10
204111 – Computer and Programming Section 450
Sample output
Position Expression/Statement
___(a)___
___(b)___
___(c)___
___(d)___
___(e)___
___(f)___
___(g)___
Lab #10
204111 – Computer and Programming Section 450
2. Programming Exercises
Lab #10
204111 – Computer and Programming Section 450
Task 2.2: Write a program to multiply two 2×2 matrices and display the result.
Note that if Cm×o = Am×n × Bn×o , then:
n
X
cij = aik bkj
k=1
Sample output
Enter A[1,1]: 1
Enter A[1,2]: 2
Enter A[2,1]: 3
Enter A[2,2]: 4
Enter B[1,1]: 5
Enter B[1,2]: 6
Enter B[2,1]: 7
Enter B[2,2]: 8
Matrix A*B is:
19 22
43 50
Lab #10