SlideShare a Scribd company logo
1 of 27
Ar rays(II) 
Lecture 9 
Dr. Hakem Beitollahi 
Computer Engineering Department 
Soran University
Outline 
 Passing Arrays to Methods 
 Passing Arrays by Value and by 
Reference 
 Multiple-Subscripted Arrays 
 foreach Repetition Structure 
Arrays(II)— 2
Passing Arrays to a Methods 
Arrays(II)— 3
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
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]); 
}
Passing Arrays to a Methods (III) 
 Before 
 After 
Arrays(II)— 6
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 );
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 )
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
Passing Arrays by Reference 
Arrays(II)— 10
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
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
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
Multiple-Subscripted Arrays 
Arrays(II)— 14
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
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
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 } };
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
Multiple-Subscripted Arrays (V) 
Arrays(II)— 19
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
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
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;
Multiple-Subscripted Arrays (IX) 
 Example: totaling the elements of a jogged 
array. 
Arrays(II)— 23
foreach Repetition Structure 
Arrays(II)— 24
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 )
foreach Repetition Structure (II) 
Arrays(II)— 26
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

More Related Content

What's hot (20)

C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Array
ArrayArray
Array
 
Array in c++
Array in c++Array in c++
Array in c++
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Array in c
Array in cArray in c
Array in c
 
Array
ArrayArray
Array
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 

Viewers also liked

Alberto Rocha - Key Core Competencies
Alberto Rocha - Key Core CompetenciesAlberto Rocha - Key Core Competencies
Alberto Rocha - Key Core CompetenciesAlberto Rocha
 
CLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and ResponsibilitiesCLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and ResponsibilitiesAlberto Rocha
 
Tequipalcatalogo
TequipalcatalogoTequipalcatalogo
Tequipalcatalogotishval
 
"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association
"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association
"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity AssociationClean Energy Canada
 
U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...
U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...
U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...Alberto Rocha
 
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...Dr. Lydia Kostopoulos
 
Gym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good EntryGym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good Entryjackojgy
 
LinkedIn company pagesplaybook6 11-13
LinkedIn company pagesplaybook6 11-13LinkedIn company pagesplaybook6 11-13
LinkedIn company pagesplaybook6 11-13AMComms
 
The 7 greatest cg aliens
The 7 greatest cg aliensThe 7 greatest cg aliens
The 7 greatest cg alienscreativebloq
 
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعالشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعnawal al-matary
 
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิตความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิตประกายทิพย์ แซ่กี่
 
Diapositiva punto 1.
Diapositiva punto 1.Diapositiva punto 1.
Diapositiva punto 1.YuyeMendoza
 
Universal Design for Learning & the iPad
Universal Design for Learning & the iPadUniversal Design for Learning & the iPad
Universal Design for Learning & the iPadChris Walton
 

Viewers also liked (20)

A&a v2
A&a v2A&a v2
A&a v2
 
Appilicious
AppiliciousAppilicious
Appilicious
 
ตาราง
ตารางตาราง
ตาราง
 
Alberto Rocha - Key Core Competencies
Alberto Rocha - Key Core CompetenciesAlberto Rocha - Key Core Competencies
Alberto Rocha - Key Core Competencies
 
CLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and ResponsibilitiesCLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and Responsibilities
 
Reversting system
Reversting systemReversting system
Reversting system
 
Tequipalcatalogo
TequipalcatalogoTequipalcatalogo
Tequipalcatalogo
 
"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association
"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association
"Future Proofing Canada's Grids," Jim Burpee, Canadian Electricity Association
 
U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...
U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...
U.S. Department of Labor - OFFCP Contracts Compliance Officer Roles and Respo...
 
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
 
Gym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good EntryGym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good Entry
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
силіцій
силіційсиліцій
силіцій
 
LinkedIn company pagesplaybook6 11-13
LinkedIn company pagesplaybook6 11-13LinkedIn company pagesplaybook6 11-13
LinkedIn company pagesplaybook6 11-13
 
เนื้อเยื่อและเมมเบรน
เนื้อเยื่อและเมมเบรนเนื้อเยื่อและเมมเบรน
เนื้อเยื่อและเมมเบรน
 
The 7 greatest cg aliens
The 7 greatest cg aliensThe 7 greatest cg aliens
The 7 greatest cg aliens
 
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعالشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
 
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิตความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
 
Diapositiva punto 1.
Diapositiva punto 1.Diapositiva punto 1.
Diapositiva punto 1.
 
Universal Design for Learning & the iPad
Universal Design for Learning & the iPadUniversal Design for Learning & the iPad
Universal Design for Learning & the iPad
 

Similar to Lecture 9

Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptadityavarte
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsPrabu U
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D arrayGem WeBlog
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.pptAqeelAbbas94
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfKirubelWondwoson1
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysEng Teong Cheah
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyworldchannel
 

Similar to Lecture 9 (20)

Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Arrays
ArraysArrays
Arrays
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Unit 2
Unit 2Unit 2
Unit 2
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Pooja
PoojaPooja
Pooja
 
2D Array
2D Array 2D Array
2D Array
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
Arrays
ArraysArrays
Arrays
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 

More from Soran University (7)

Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Administrative
AdministrativeAdministrative
Administrative
 

Recently uploaded

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
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
  • 3. Passing Arrays to a Methods Arrays(II)— 3
  • 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]); }
  • 6. Passing Arrays to a Methods (III)  Before  After Arrays(II)— 6
  • 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
  • 10. Passing Arrays by Reference Arrays(II)— 10
  • 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;
  • 23. Multiple-Subscripted Arrays (IX)  Example: totaling the elements of a jogged array. Arrays(II)— 23
  • 24. foreach Repetition Structure Arrays(II)— 24
  • 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 )
  • 26. foreach Repetition Structure (II) Arrays(II)— 26
  • 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