0% found this document useful (0 votes)
28 views9 pages

Lab10 - Arrays2 - Sec450 C#

lab10_Arrays2_sec450 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

Lab10 - Arrays2 - Sec450 C#

lab10_Arrays2_sec450 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

204111 – Computer and Programming Section 450

Lab #10– Multi-dimensional Arrays

Student ID Name Signature


Sheet’s Owner
Group partner

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

int [ ,] A = new int [4 ,3] {


{ 5, 3 , 8} ,
{ 2, 6 , 10} ,
{ 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 ]

Example 1.3: Consider the matrix A used in Example 1.2.

• Display the value of the element A[3, 2] (i.e., a3,2 )


Console . WriteLine ( A [2 ,1]);

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.

• Assign the value 33 to the element A[4, 3]


A [3 ,2] = 33;

Lab #10
204111 – Computer and Programming Section 450

• Initialize all the elements in the first column of matrix A to 1


for ( int i = 0; i < 4; i ++)
A [i ,0] = 1;

• Make the matrix A a zero matrix


for ( int i = 0; i < 4; i ++)
for ( int j = 0; i < 3; j ++)
A [i , j ] = 0;

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 }

• What is the output of the program?

Lab #10
204111 – Computer and Programming Section 450

• What is the output of the program when line 14 is changed to


Console.Write("{0}", A[i,j]); instead?

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 ;

Console . Write ( " Enter number of rows : " );


nrow = int . Parse ( Console . ReadLine ());
Console . Write ( " Enter number of columns : " );
ncol = int . Parse ( Console . ReadLine ());

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 ());
}

Console . WriteLine ( " Matrix A is " );


for ( i = 0; i < nrow ; i ++) {
for ( j = 0; j < ncol ; j ++) {
Console . Write ( " {0 ,4} " , ___ ( d ) ___ );
}
______ ( e ) ______
}
}
}

Lab #10
204111 – Computer and Programming Section 450

Position Expression/Statement
___(a)___
___(b)___
___(c)___
___(d)___
___(e)___

Sample output

Enter number of rows: 2


Enter number of columns: 3
Enter A[1,1]: 10
Enter A[1,2]: 4
Enter A[1,3]: 22
Enter A[2,1]: 65
Enter A[2,2]: 34
Enter A[2,3]: 18
Matrix A is
10 4 22
65 34 18

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

Enter number of rows: 3


Enter number of columns: 2
Enter A[1,1]: 1
Enter A[1,2]: 2
Enter A[2,1]: 3
Enter A[2,2]: 4
Enter A[3,1]: 5
Enter A[3,2]: 6
Matrix A is
1 2
3 4
5 6
Transpose of matrix A is
1 3 5
2 4 6

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 ;

Console . Write ( " Enter number of rows : " );


nrow = int . Parse ( Console . ReadLine ());
Console . Write ( " Enter number of columns : " );
ncol = int . Parse ( Console . ReadLine ());

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

Console . WriteLine ( " Matrix A + B is " );


for ( i = 0; i < nrow ; i ++) {
for ( j = 0; j < ncol ; j ++) {
Console . Write ( " {0 ,4} " , ___ ( g ) ___ );
}
Console . WriteLine ();
}
}
}

Sample output

Enter number of rows: 2


Enter number of columns: 2
Enter A[1,1]: 1
Enter A[1,2]: 2
Enter A[2,1]: 3
Enter A[2,2]: 4
Enter A[1,1]: 5
Enter A[1,2]: 6
Enter A[2,1]: 7
Enter A[2,2]: 8
Matrix A+B is
6 8
10 12

Position Expression/Statement
___(a)___
___(b)___
___(c)___
___(d)___
___(e)___
___(f)___
___(g)___

Lab #10
204111 – Computer and Programming Section 450

2. Programming Exercises

Task 2.1: Write a program to find the determinant of a 2×2 matrix.


Hint. For a matrix A = (ai,j )2×2 , its determinant is defined as:
det(A) = (a11 a22 − a12 a21 )
Sample output
Enter A[1,1]: 1
Enter A[1,2]: 2
Enter A[2,1]: 3
Enter A[2,2]: 4
The determinant of A is -2

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

You might also like