Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Lecture 9
1. Ar rays(II)
Lecture 9
Dr. Hakem Beitollahi
Computer Engineering Department
Soran University
2. Outline
Passing Arrays to Methods
Passing Arrays by Value and by
Reference
Multiple-Subscripted Arrays
foreach Repetition Structure
Arrays(II)— 2
4. Passing Arrays to a Methods (I)
Individual array elements can be passed
by value or by reference
Pass-by-value example:
public void printcard(int c)
{
if(c==1)
Console.Write("A“)
}
void Main() {
int[] cards = new int[5] ;
...
for(int n=0; n<5; n++)
printcard(card[n]);
}
Arrays(II)— 4
5. Passing Arrays to a Methods (II)
Pass-by-reference example:
Arrays(II)— 5
void swap(ref int x, ref int y) {
int temp;
if (x > y){
temp = x;
x = y;
y = temp;
}
}
void Main() {
int [] A = {9,8,7,6,5,4,3,2,1,0};
swap(A[3], A[5]);
}
7. Passing Arrays to a Methods (IV)
Arrays can be passed to methods in their
entirety.
All that is required is the name of the array
without using brackets.
For example: hourlyTemperatures is a
array declares as
the method call
Arrays(II)— 7
int[] hourlyTemperatures = new int[ 24 ];
ModifyArray( hourlyTemperatures );
8. Passing Arrays to a Methods (V)
Every array object “knows” its own size
(via the Length instance variable), so when
we pass an array object into a method, we
do not pass the size of the array as an
argument separately.
For a method to receive an array through
a method call, the method’s parameter list
must specify that an array will be received.
Arrays(II)— 8
public void ModifyArray( int[] b )
9. Passing Arrays to a Methods (VI)
Example: Find whether an array has value 10
and if yes which element(s) has this value?
Arrays(II)— 9
11. Passing Arrays by Reference (I)
Pass by value: The method receives a
copy of that argument’s value. Changes to
the local copy do not affect the original
variable that the program passed to the
method.
Pass by reference: The address of the
original variable is sent to the method and
the method affects the original variable.
C# also allows methods to pass
references with keyword ref.
Arrays(II)— 11
12. Passing Arrays by Reference (II)
Example: Multiplies the values of all the elements in the array by 2.
in one method user reference and in the second one use pass by
value
Arrays(II)— 12
13. Passing Arrays by Reference (III)
Conclusion: the variable array is actually a reference,
because int[] is a reference type. So array is a reference
that is passed by value.
In this case there is no difference between pass-by value
and pass by reference.
Question: How to keep the original value of an array that
is passed to a method without change?
Use Clone() method or Copyto() method
In previous example
Arrays(II)— 13
15. Multiple-Subscripted Arrays (I)
single-subscripted (or one-dimensional) arrays
contain single lists of values.
Now, we introduce multiple-subscripted (often
called multidimensional) arrays.
The most appilcable multidimensional array is 2-
D array such as matrix.
There are two types of multiple-subscripted
arrays:
Rectangular
jagged
Arrays(II)— 15
16. Multiple-Subscripted Arrays (II)
Rectangular array: Rectangular arrays with two subscripts often
represent tables of values consisting of information arranged in rows
and columns.
We must specify the two subscripts— by convention, the first
identifies the element’s row and the second identifies the element’s
column.
Arrays(II)— 16
17. Multiple-Subscripted Arrays (III)
Multiple-subscripted arrays can be
initialized in declarations like single-subscripted
arrays.
or this can be written on one line using an
initializer list as shown below:
Arrays(II)— 17
int[,] b = new int[ 2, 2 ];
b[ 0, 0 ] = 1;
b[ 0, 1 ] = 2;
b[ 1, 0 ] = 3;
b[ 1, 1 ] = 4;
int[,] b = { { 1, 2 }, { 3, 4 } };
18. Multiple-Subscripted Arrays (IV)
Method GetLength
Method GetLength returns the length of a particular array
dimension.
arrayName.GetLength(0) returns the zeroth dimension of arrayName.
arrayName.GetLength(1) returns the oneth dimension of arrayName.
arrayName.GetLength(n) returns the nth dimension of arrayName.
Example:
Arrays(II)— 18
20. Multiple-Subscripted Arrays (VI)
Jagged arrays are maintained as arrays of arrays.
Unlike in rectangular arrays, the arrays that compose jagged arrays
can be of different lengths.
int[][] c = new int[ 2 ][]; // allocate rows
// allocate and initialize elements in row 0
c[ 0 ] = new int[] { 1, 2 };
// allocate and initialize elements in
row 0
c[ 1 ] = new int[] { 3, 4, 5 };
creates integer array c with row 0 (which is an array itself)
containing two elements (1 and 2), and row 1 containing three
elements (3, 4 and 5).
The Length property of each subarray can be used to determine
the size of each column
c[ 0 ].Length, which is 2.
c.Length returns number of rows which is 2 Arrays(II)— 20
21. Multiple-Subscripted Arrays (VII)
An example: Show elements of a Jagged
array.
a.Length returns number of rows of the array
a[i].Length returns number of elements of row i
Arrays(II)— 21
22. Multiple-Subscripted Arrays (VIII)
Imagine a jagged array a, which contains
3 rows, or arrays.
The following for structure sets all the
elements in the third row of array a to
zero:
This statement demonstrates that each
row of a is an array in itself.
The program can access a typical array’s
properties, such as Length
Arrays(II)— 22
for ( int col = 0; col <
a[ 2 ].Length; col++ )
a[ 2 ][ col ] = 0;
25. foreach Repetition Structure (I)
C# provides the foreach retition structure for iterating
through values in data structures, such as arrays.
with one-dimensional arrays, foreach behaves like a for
structure that iterates through the range of indices from 0
to the array’s Length.
Instead of a counter, foreach uses a variable to
represent the value of each element.
The foreach structure iterates through all elements in
gradeArray, sequentially assigning each value to
variable grade.
See the example
Arrays(II)— 25
foreach ( int grade in gradeArray )
27. Common Programming Error
Type and identifier should be declared
inside the foreach repetition structure.
Type of grade should be declared inside
the foreach instruction.
Arrays(II)— 27