lecture 9

27
Arrays(II) Dr. Hakem Beitollahi Computer Engineering Department Soran University Lecture 9

Upload: soran-university

Post on 29-Jun-2015

25 views

Category:

Engineering


2 download

DESCRIPTION

OOP with C# by Dr.hakim

TRANSCRIPT

Page 1: Lecture 9

Arrays(II)

Dr. Hakem Beitollahi

Computer Engineering DepartmentSoran University

Lecture 9

Page 2: Lecture 9

Outline Passing Arrays to Methods Passing Arrays by Value and by

Reference Multiple-Subscripted Arrays foreach Repetition Structure

Arrays(II)— 2

Page 3: Lecture 9

Passing Arrays to a Methods

Arrays(II)— 3

Page 4: Lecture 9

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

Page 5: Lecture 9

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

Page 6: Lecture 9

Passing Arrays to a Methods (III)

Before

After

Arrays(II)— 6

Page 7: Lecture 9

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

Page 8: Lecture 9

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 )

Page 9: Lecture 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

Page 10: Lecture 9

Passing Arrays by Reference

Arrays(II)— 10

Page 11: Lecture 9

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

Page 12: Lecture 9

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

Page 13: Lecture 9

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

Page 14: Lecture 9

Multiple-Subscripted Arrays

Arrays(II)— 14

Page 15: Lecture 9

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

Page 16: Lecture 9

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

Page 17: Lecture 9

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

Page 18: Lecture 9

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

Page 19: Lecture 9

Multiple-Subscripted Arrays (V)

Arrays(II)— 19

Page 20: Lecture 9

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.

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

int[][] c = new int[ 2 ][]; // allocate rows// allocate and initialize elements in row 0c[ 0 ] = new int[] { 1, 2 };

// allocate and initialize elements in row 0c[ 1 ] = new int[] { 3, 4, 5 };

Page 21: Lecture 9

Multiple-Subscripted Arrays (VII)

An example: Show elements of a Jagged array.

Arrays(II)— 21

a.Length returns number of rows of the array

a[i].Length returns number of elements of row i

Page 22: Lecture 9

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 LengthArrays(II)— 22

for ( int col = 0; col < a[ 2 ].Length; col++ ) a[ 2 ][ col ] = 0;

Page 23: Lecture 9

Multiple-Subscripted Arrays (IX)

Example: totaling the elements of a jogged array.

Arrays(II)— 23

Page 24: Lecture 9

foreach Repetition Structure

Arrays(II)— 24

Page 25: Lecture 9

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 )

Page 26: Lecture 9

foreach Repetition Structure (II)

Arrays(II)— 26

Page 27: Lecture 9

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