SlideShare a Scribd company logo
DR ATIF SHAHZAD
Computer Applications
in Industrial Engg.-I
IE-322
LECTURE #6
Computer Applications in
Industrial Engg.-I
IE322
…Recap
9/27/2018
Dr.AtifShahzad
What we will see…
9/27/2018
Dr.AtifShahzad
Introduction toArrays
Declaration & Definition
Initialization
Arrays in loops Foreach loop
AccessingArray Elements
Array Methods and
properties
Multidimensional arrays Passing by ref
Passing by value
Listbox with arrays
Random number arrays
Arrays
IE322
Array
a data structure
used to store a collection of data.
a collection of variables of the same type.
Remember, an Array is areference type.
9/27/2018
Dr.AtifShahzad
6
Array use case
For example, consider a situation where
you need to store 100 numbers.
Rather than declare 100 different variables,
you can
just declare an array that stores
100 elements.
9/27/2018
Dr.AtifShahzad
7
Array Declaration
int[ ] myArray; // an array of integers
9/27/2018
Dr.AtifShahzad
8
Array Definition
int[ ] myArray = new int[5]; // an array
of integers that can hold 5 integers
Since arrays are objects, we need to
instantiate them with the new keyword
9/27/2018
Dr.AtifShahzad
9
Array Definition: Question
Fill in the blanks to instantiate an array of
42 elements of type double:
_____ [ ] a = ___ double[ __];
9/27/2018
Dr.AtifShahzad
10
Array Definition:Answer
Fill in the blanks to instantiate an array of
42 elements of type double:
double [ ] a = new double[ 42];
9/27/2018
Dr.AtifShahzad
11
Array Initialization
string[ ] names = new string[3] {"John",
"Mary", "Jessica"};//an array of strings
that can hold 3 strings and is initialized as
well
Note the use of [ ]and { }
9/27/2018
Dr.AtifShahzad
12
Array Initialization: Question
Fill in the blanks to instantiate the array
with initial values:
int[ ] a = _1, 2, 3_ ;
Note the use of [ ]and { }
9/27/2018
Dr.AtifShahzad
13
Array Initialization:Answer
Fill in the blanks to instantiate the array
with initial values:
int[ ] a = {1, 2, 3} ;
Note the use of [ ]and { }
9/27/2018
Dr.AtifShahzad
14
Array Initialization
double[ ] prices = new double[4] {3.6,
9.8, 6.4, 5.9};// an array of doubles that
can hold 4 numbers and is initialized
9/27/2018
Dr.AtifShahzad
15
Array Initialization
string[ ] names = new string[ ] {"John",
"Mary", "Jessica"};
double[ ] prices = new double[ ] {3.6, 9.8,
6.4, 5.9};
We canomit the size declaration when
the number of elements are provided in
the curly braces
9/27/2018
Dr.AtifShahzad
16
Array Initialization
string[ ] names = {"John", "Mary",
"Jessica"};
double[ ] prices = {3.6, 9.8, 6.4, 5.9};
We can evenomit the new operator.
9/27/2018
Dr.AtifShahzad
17
Array Initialization
string[] daysOfWeek =
{ "Monday", "Tuesday",
"Wednesday","Thursday", "Friday",
"Saturday", "Sunday" };
We can evenomit the new operator.
9/27/2018
Dr.AtifShahzad
18
Summarizing in code…
9/27/2018
Dr.AtifShahzad
19
Int32[] iArray1; //Declaration Only
iArray1 = new Int32[5];//Definition
Int32[] iArray2 = new Int32[5]; //Declaration and Definition
Int32[] iArray3 = new Int32[5] { 21, 22, 23, 24, 25 };//Declaration,
Definition & Initialization
Int32[] iArray4 = new Int32[] { 11,12,13,14,15}; //Declaration, Definit
ion & Initialization (can omit size value only if initialization)
Int32[] iArray5 = { 31,32,33,34,35};//Declaration, Definition & Initial
ization (can omit new keyword only if initialization)
Accessing Array Elements
What is the value of x after these
statements execute?
int[ ] a = {4, 7, 2};
int x = a[0]+a[2];
9/27/2018
Dr.AtifShahzad
20
Remember that
the first element
has index 0.
Accessing Array Elements
Each element of an array has an index
number.
int[ ] b = {11, 45, 62, 70, 88};
Console.WriteLine(b[2]);
//Outputs 62
Console.WriteLine(b[3]);
//Outputs 70
9/27/2018
Dr.AtifShahzad
21
Remember that
the first element
has index 0.
Accessing Array Elements
Each element of an array has an index
number.
int[ ] b = {11, 45, 62, 70, 88};
Console.WriteLine(b[2]);
//Outputs 62
Console.WriteLine(b[3]);
//Outputs 70
9/27/2018
Dr.AtifShahzad
22
Remember that
the first element
has index 0.
Using loops with Arrays
9/27/2018
Dr.AtifShahzad
23
Int32[] iArray3 = new Int32[5] { 21, 22, 23, 24, 25 };//Declaration
, Definition & Initialization
for (Int32 i=1;i<5;i++)
{
Console.WriteLine(iArray3[i]);
}
Using foreach loops with Arrays
9/27/2018
Dr.AtifShahzad
24
Int32[] iArray3 = new Int32[5] { 21, 22, 23, 24, 25 };//Declaration
, Definition & Initialization
foreach (Int32 j in iArray3)
{
Console.WriteLine(j);
} always through all
elements – from
the start to the end
order of iteration
is consecutive
Multidimensional Arrays
Declaration and Definition of 2D array
Declaration and Definition of 3D array
9/27/2018
Dr.AtifShahzad
25
int[,] iArray55 = new int[5,5];
int[,,] iArray345 = new int[3, 4 , 5];
Multidimensional Arrays
Declaration,Definition & Initialization of
2D array
9/27/2018
Dr.AtifShahzad
26
int[,] iArray23 = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
int[,] iArray32 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
Multidimensional Arrays
Accessing elements of 2D array
9/27/2018
Dr.AtifShahzad
27
Console.WriteLine("Element in third row
and first column of iArray23:" + iArray2
3[2, 0]);
Multidimensional Arrays: Example
Printing a Matrix
9/27/2018
Dr.AtifShahzad
28
// Declare and initialize a matrix of size 2 x 4
int[,] matrix =
{
{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8}, // row 1 value
};
// Print the matrix on the console
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write(matrix[row, col]);
}
Console.WriteLine();
}
Some Arrays Properties
Length
// Total number of elements in all dimensions
Rank
// Number of dimensions
9/27/2018
Dr.AtifShahzad
29
Arrays Properties
Console.WriteLine("Rank of iArray5 is:"
+ iArray5.Rank); // Dimensions
Console.WriteLine("Length of
iArray5 is:" + iArray5.Length);// size
9/27/2018
Dr.AtifShahzad
30
Some Arrays Methods
Min()
Max()
Sum()
Average()
GetLength(int dim)
9/27/2018
Dr.AtifShahzad
31
Arrays Methods
Console.WriteLine("Min of iArray5 is:" +
iArray5.Min());
Console.WriteLine("Max of
iArray5 is:" + iArray5.Max());
Console.WriteLine("Sum of
iArray5 is:" + iArray5.Sum());
9/27/2018
Dr.AtifShahzad
32
Question
What will be the output
int [ ] array={1, 2, 3, 4, 5};
int total=0;
for (int i=1; i<array.Length; i=i+2)
total=total+array[i];
Console.Write(total);
9/27/2018
Dr.AtifShahzad
33
Question
What will be the output
int [ , ] d = { { 1, 2, 1 }, { 1, 2, 1 }, {2, 0, 1} } ;
for ( int j= 1; j < d.GetLength(1); j++ )
{
int total=0;
for ( int i = 0; i< d.GetLength(0); i++ )
{
total=total + d[ i, j];
}
Console.WriteLine( "{0} ", total);
}9/27/2018
Dr.AtifShahzad
34
Question
What is the error?
This code should sum up all the elements in the array x
int i = 1;
int [ ] x = { 2, 3, 4, 5 };
int total=0;
while (i < 4)
{
total += x[i];
i++;
}
Console.WriteLine(total);
9/27/2018
Dr.AtifShahzad
35
Question
Make a program to generate 500 random
numbers between 1 and 1000. Find
Minimum, maximum, Sum and Average of
these numbers using Array Methods.
Display these numbers in a listbox.
9/27/2018
Dr.AtifShahzad
36
Passing Arrays to Methods
Variables that store an
object (like an array) DO
NOT store the object
itself, rather just a
reference to the object
9/27/2018
Dr.AtifShahzad
37
Passing Arrays to Methods
So we have:
•ValueType variables
•ReferenceTypeVariables
9/27/2018
Dr.AtifShahzad
38
ValueType
• Some examples are int, char,
and float, which stores
numbers, alphabets, and
floating point numbers,
respectively.
• When you declare an int type,
the system allocates memory to
store the value.
The
value
types
directly
contain
data.
9/27/2018
Dr.AtifShahzad
39
ReferenceType
• In other words, they refer to a memory
location
• Example of built-in reference
typesafe: object, dynamic, and string.
• You can declare the reference parameters
using the ref keyword.
The reference
types do not
contain the
actual data
stored in a
variable, but
they contain a
reference to
the variables.
9/27/2018
Dr.AtifShahzad
40
Lecture06 computer applicationsie1_dratifshahzad
9/27/2018
Dr.AtifShahzad
42
NEVER hesitate to
contact should you
have any question
Dr.Atif Shahzad

More Related Content

What's hot (20)

PDF
Week06
hccit
 
PPT
Associative Learning
Indrajit Sreemany
 
PPT
arrays
teach4uin
 
PPT
Java Arrays
Jussi Pohjolainen
 
PPT
Array in Java
Shehrevar Davierwala
 
PDF
intorduction to Arrays in java
Muthukumaran Subramanian
 
PDF
An Introduction to Programming in Java: Arrays
Martin Chapman
 
PPTX
Array,MULTI ARRAY, IN C
naveed jamali
 
PPTX
Arrays
Chirag vasava
 
PPT
Data Structure Midterm Lesson Arrays
Maulen Bale
 
PPTX
Array in C# 3.5
Gopal Ji Singh
 
PPTX
Array in c#
Prem Kumar Badri
 
PPTX
Array in c
AnIsh Kumar
 
PPT
Mesics lecture 8 arrays in 'c'
eShikshak
 
PPTX
Java arrays
BHUVIJAYAVELU
 
PPTX
Array in C
adityas29
 
PPT
Array
Malainine Zaid
 
PPT
Lec 25 - arrays-strings
Princess Sam
 
Week06
hccit
 
Associative Learning
Indrajit Sreemany
 
arrays
teach4uin
 
Java Arrays
Jussi Pohjolainen
 
Array in Java
Shehrevar Davierwala
 
intorduction to Arrays in java
Muthukumaran Subramanian
 
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Array,MULTI ARRAY, IN C
naveed jamali
 
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Array in C# 3.5
Gopal Ji Singh
 
Array in c#
Prem Kumar Badri
 
Array in c
AnIsh Kumar
 
Mesics lecture 8 arrays in 'c'
eShikshak
 
Java arrays
BHUVIJAYAVELU
 
Array in C
adityas29
 
Lec 25 - arrays-strings
Princess Sam
 

Similar to Lecture06 computer applicationsie1_dratifshahzad (20)

PDF
Lecture05 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PDF
Chap 6 c++
Venkateswarlu Vuggam
 
PPTX
07+08slide.pptx
MURADSANJOUM
 
PPT
Chap 6 c++
Venkateswarlu Vuggam
 
PPT
SP-First-Lecture.ppt
FareedIhsas
 
PPT
C Arrays.ppt
ssuser0c1819
 
PDF
Java - Arrays Concepts
Victer Paul
 
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
PPT
Fp201 unit4
rohassanie
 
PPT
Csphtp1 07
HUST
 
PPTX
6_Array.pptx
shafat6712
 
PPTX
Array and its operation in C programming
Rhishav Poudyal
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPTX
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
PPTX
Arrays basics
sudhirvegad
 
Lecture05 computer applicationsie1_dratifshahzad
Atif Shahzad
 
07+08slide.pptx
MURADSANJOUM
 
SP-First-Lecture.ppt
FareedIhsas
 
C Arrays.ppt
ssuser0c1819
 
Java - Arrays Concepts
Victer Paul
 
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
Fp201 unit4
rohassanie
 
Csphtp1 07
HUST
 
6_Array.pptx
shafat6712
 
Array and its operation in C programming
Rhishav Poudyal
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Arrays basics
sudhirvegad
 
Ad

More from Atif Shahzad (20)

PDF
Lecture04 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PDF
Lecture03 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PDF
Lecture01 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PDF
Lecture02 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PDF
Lecture02 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PDF
Dr atif shahzad_sys_ management_lecture_agile
Atif Shahzad
 
PDF
Dr atif shahzad_sys_ management_lecture_10_risk management_fmea_vmea
Atif Shahzad
 
PDF
Dr atif shahzad_engg_ management_module_01
Atif Shahzad
 
PDF
Dr atif shahzad_engg_ management_lecture_inventory models
Atif Shahzad
 
PDF
Dr atif shahzad_engg_ management_lecture_inventory management
Atif Shahzad
 
PDF
Dr atif shahzad_engg_ management_cost management
Atif Shahzad
 
PDF
Dr atif shahzad_sys_ management_lecture_outsourcing managing inter organizati...
Atif Shahzad
 
PDF
Lecture17 ie321 dr_atifshahzad_js
Atif Shahzad
 
PDF
Lecture16 ie321 dr_atifshahzad_css
Atif Shahzad
 
PDF
Lecture15 ie321 dr_atifshahzad_html
Atif Shahzad
 
PDF
Lecture14 ie321 dr_atifshahzad -algorithmic thinking part2
Atif Shahzad
 
PDF
Lecture13 ie321 dr_atifshahzad -algorithmic thinking
Atif Shahzad
 
PDF
Lecture12 ie321 dr_atifshahzad - networks
Atif Shahzad
 
PDF
Lecture11 ie321 dr_atifshahzad -security
Atif Shahzad
 
PDF
Lecture10 ie321 dr_atifshahzad
Atif Shahzad
 
Lecture04 computer applicationsie1_dratifshahzad
Atif Shahzad
 
Lecture03 computer applicationsie1_dratifshahzad
Atif Shahzad
 
Lecture01 computer applicationsie1_dratifshahzad
Atif Shahzad
 
Lecture02 computer applicationsie1_dratifshahzad
Atif Shahzad
 
Lecture02 computer applicationsie1_dratifshahzad
Atif Shahzad
 
Dr atif shahzad_sys_ management_lecture_agile
Atif Shahzad
 
Dr atif shahzad_sys_ management_lecture_10_risk management_fmea_vmea
Atif Shahzad
 
Dr atif shahzad_engg_ management_module_01
Atif Shahzad
 
Dr atif shahzad_engg_ management_lecture_inventory models
Atif Shahzad
 
Dr atif shahzad_engg_ management_lecture_inventory management
Atif Shahzad
 
Dr atif shahzad_engg_ management_cost management
Atif Shahzad
 
Dr atif shahzad_sys_ management_lecture_outsourcing managing inter organizati...
Atif Shahzad
 
Lecture17 ie321 dr_atifshahzad_js
Atif Shahzad
 
Lecture16 ie321 dr_atifshahzad_css
Atif Shahzad
 
Lecture15 ie321 dr_atifshahzad_html
Atif Shahzad
 
Lecture14 ie321 dr_atifshahzad -algorithmic thinking part2
Atif Shahzad
 
Lecture13 ie321 dr_atifshahzad -algorithmic thinking
Atif Shahzad
 
Lecture12 ie321 dr_atifshahzad - networks
Atif Shahzad
 
Lecture11 ie321 dr_atifshahzad -security
Atif Shahzad
 
Lecture10 ie321 dr_atifshahzad
Atif Shahzad
 
Ad

Recently uploaded (20)

PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 

Lecture06 computer applicationsie1_dratifshahzad

  • 1. DR ATIF SHAHZAD Computer Applications in Industrial Engg.-I IE-322 LECTURE #6
  • 4. What we will see… 9/27/2018 Dr.AtifShahzad Introduction toArrays Declaration & Definition Initialization Arrays in loops Foreach loop AccessingArray Elements Array Methods and properties Multidimensional arrays Passing by ref Passing by value Listbox with arrays Random number arrays
  • 6. Array a data structure used to store a collection of data. a collection of variables of the same type. Remember, an Array is areference type. 9/27/2018 Dr.AtifShahzad 6
  • 7. Array use case For example, consider a situation where you need to store 100 numbers. Rather than declare 100 different variables, you can just declare an array that stores 100 elements. 9/27/2018 Dr.AtifShahzad 7
  • 8. Array Declaration int[ ] myArray; // an array of integers 9/27/2018 Dr.AtifShahzad 8
  • 9. Array Definition int[ ] myArray = new int[5]; // an array of integers that can hold 5 integers Since arrays are objects, we need to instantiate them with the new keyword 9/27/2018 Dr.AtifShahzad 9
  • 10. Array Definition: Question Fill in the blanks to instantiate an array of 42 elements of type double: _____ [ ] a = ___ double[ __]; 9/27/2018 Dr.AtifShahzad 10
  • 11. Array Definition:Answer Fill in the blanks to instantiate an array of 42 elements of type double: double [ ] a = new double[ 42]; 9/27/2018 Dr.AtifShahzad 11
  • 12. Array Initialization string[ ] names = new string[3] {"John", "Mary", "Jessica"};//an array of strings that can hold 3 strings and is initialized as well Note the use of [ ]and { } 9/27/2018 Dr.AtifShahzad 12
  • 13. Array Initialization: Question Fill in the blanks to instantiate the array with initial values: int[ ] a = _1, 2, 3_ ; Note the use of [ ]and { } 9/27/2018 Dr.AtifShahzad 13
  • 14. Array Initialization:Answer Fill in the blanks to instantiate the array with initial values: int[ ] a = {1, 2, 3} ; Note the use of [ ]and { } 9/27/2018 Dr.AtifShahzad 14
  • 15. Array Initialization double[ ] prices = new double[4] {3.6, 9.8, 6.4, 5.9};// an array of doubles that can hold 4 numbers and is initialized 9/27/2018 Dr.AtifShahzad 15
  • 16. Array Initialization string[ ] names = new string[ ] {"John", "Mary", "Jessica"}; double[ ] prices = new double[ ] {3.6, 9.8, 6.4, 5.9}; We canomit the size declaration when the number of elements are provided in the curly braces 9/27/2018 Dr.AtifShahzad 16
  • 17. Array Initialization string[ ] names = {"John", "Mary", "Jessica"}; double[ ] prices = {3.6, 9.8, 6.4, 5.9}; We can evenomit the new operator. 9/27/2018 Dr.AtifShahzad 17
  • 18. Array Initialization string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday" }; We can evenomit the new operator. 9/27/2018 Dr.AtifShahzad 18
  • 19. Summarizing in code… 9/27/2018 Dr.AtifShahzad 19 Int32[] iArray1; //Declaration Only iArray1 = new Int32[5];//Definition Int32[] iArray2 = new Int32[5]; //Declaration and Definition Int32[] iArray3 = new Int32[5] { 21, 22, 23, 24, 25 };//Declaration, Definition & Initialization Int32[] iArray4 = new Int32[] { 11,12,13,14,15}; //Declaration, Definit ion & Initialization (can omit size value only if initialization) Int32[] iArray5 = { 31,32,33,34,35};//Declaration, Definition & Initial ization (can omit new keyword only if initialization)
  • 20. Accessing Array Elements What is the value of x after these statements execute? int[ ] a = {4, 7, 2}; int x = a[0]+a[2]; 9/27/2018 Dr.AtifShahzad 20 Remember that the first element has index 0.
  • 21. Accessing Array Elements Each element of an array has an index number. int[ ] b = {11, 45, 62, 70, 88}; Console.WriteLine(b[2]); //Outputs 62 Console.WriteLine(b[3]); //Outputs 70 9/27/2018 Dr.AtifShahzad 21 Remember that the first element has index 0.
  • 22. Accessing Array Elements Each element of an array has an index number. int[ ] b = {11, 45, 62, 70, 88}; Console.WriteLine(b[2]); //Outputs 62 Console.WriteLine(b[3]); //Outputs 70 9/27/2018 Dr.AtifShahzad 22 Remember that the first element has index 0.
  • 23. Using loops with Arrays 9/27/2018 Dr.AtifShahzad 23 Int32[] iArray3 = new Int32[5] { 21, 22, 23, 24, 25 };//Declaration , Definition & Initialization for (Int32 i=1;i<5;i++) { Console.WriteLine(iArray3[i]); }
  • 24. Using foreach loops with Arrays 9/27/2018 Dr.AtifShahzad 24 Int32[] iArray3 = new Int32[5] { 21, 22, 23, 24, 25 };//Declaration , Definition & Initialization foreach (Int32 j in iArray3) { Console.WriteLine(j); } always through all elements – from the start to the end order of iteration is consecutive
  • 25. Multidimensional Arrays Declaration and Definition of 2D array Declaration and Definition of 3D array 9/27/2018 Dr.AtifShahzad 25 int[,] iArray55 = new int[5,5]; int[,,] iArray345 = new int[3, 4 , 5];
  • 26. Multidimensional Arrays Declaration,Definition & Initialization of 2D array 9/27/2018 Dr.AtifShahzad 26 int[,] iArray23 = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; int[,] iArray32 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  • 27. Multidimensional Arrays Accessing elements of 2D array 9/27/2018 Dr.AtifShahzad 27 Console.WriteLine("Element in third row and first column of iArray23:" + iArray2 3[2, 0]);
  • 28. Multidimensional Arrays: Example Printing a Matrix 9/27/2018 Dr.AtifShahzad 28 // Declare and initialize a matrix of size 2 x 4 int[,] matrix = { {1, 2, 3, 4}, // row 0 values {5, 6, 7, 8}, // row 1 value }; // Print the matrix on the console for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { Console.Write(matrix[row, col]); } Console.WriteLine(); }
  • 29. Some Arrays Properties Length // Total number of elements in all dimensions Rank // Number of dimensions 9/27/2018 Dr.AtifShahzad 29
  • 30. Arrays Properties Console.WriteLine("Rank of iArray5 is:" + iArray5.Rank); // Dimensions Console.WriteLine("Length of iArray5 is:" + iArray5.Length);// size 9/27/2018 Dr.AtifShahzad 30
  • 32. Arrays Methods Console.WriteLine("Min of iArray5 is:" + iArray5.Min()); Console.WriteLine("Max of iArray5 is:" + iArray5.Max()); Console.WriteLine("Sum of iArray5 is:" + iArray5.Sum()); 9/27/2018 Dr.AtifShahzad 32
  • 33. Question What will be the output int [ ] array={1, 2, 3, 4, 5}; int total=0; for (int i=1; i<array.Length; i=i+2) total=total+array[i]; Console.Write(total); 9/27/2018 Dr.AtifShahzad 33
  • 34. Question What will be the output int [ , ] d = { { 1, 2, 1 }, { 1, 2, 1 }, {2, 0, 1} } ; for ( int j= 1; j < d.GetLength(1); j++ ) { int total=0; for ( int i = 0; i< d.GetLength(0); i++ ) { total=total + d[ i, j]; } Console.WriteLine( "{0} ", total); }9/27/2018 Dr.AtifShahzad 34
  • 35. Question What is the error? This code should sum up all the elements in the array x int i = 1; int [ ] x = { 2, 3, 4, 5 }; int total=0; while (i < 4) { total += x[i]; i++; } Console.WriteLine(total); 9/27/2018 Dr.AtifShahzad 35
  • 36. Question Make a program to generate 500 random numbers between 1 and 1000. Find Minimum, maximum, Sum and Average of these numbers using Array Methods. Display these numbers in a listbox. 9/27/2018 Dr.AtifShahzad 36
  • 37. Passing Arrays to Methods Variables that store an object (like an array) DO NOT store the object itself, rather just a reference to the object 9/27/2018 Dr.AtifShahzad 37
  • 38. Passing Arrays to Methods So we have: •ValueType variables •ReferenceTypeVariables 9/27/2018 Dr.AtifShahzad 38
  • 39. ValueType • Some examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively. • When you declare an int type, the system allocates memory to store the value. The value types directly contain data. 9/27/2018 Dr.AtifShahzad 39
  • 40. ReferenceType • In other words, they refer to a memory location • Example of built-in reference typesafe: object, dynamic, and string. • You can declare the reference parameters using the ref keyword. The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. 9/27/2018 Dr.AtifShahzad 40
  • 42. 9/27/2018 Dr.AtifShahzad 42 NEVER hesitate to contact should you have any question Dr.Atif Shahzad