SlideShare a Scribd company logo
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

C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
Osama Ghandour Geris
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
Deep Prajapati Microplacer
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Array
ArrayArray
ArrayHajar
 
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
 
Arrays
ArraysArrays
Arrays
ArraysArrays
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 
Array
ArrayArray
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)
Imdadul Himu
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
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
LATHA LAKSHMI
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 

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 Competencies
Alberto 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 Responsibilities
Alberto Rocha
 
Reversting system
Reversting systemReversting system
Reversting system
Frenki Niken
 
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 Association
Clean 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 Entry
jackojgy
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Soran University
 
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 aliens
creativebloq
 
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعالشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
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 iPad
Chris 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 .pdf
aroraopticals15
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
adityavarte
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
Prabu U
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
Gem WeBlog
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
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
KirubelWondwoson1
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
maznabili
 
Arrays
ArraysArrays
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
worldchannel
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
PankajKumar497975
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Pf presntation
Pf presntationPf presntation
Pf presntation
Roshan Roshan Ansari
 

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

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

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

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 

Recently uploaded (20)

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 

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