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

PPT
Lecture 8
PPTX
Arrays in CPP
PPT
Java arrays
PPT
Arrays Class presentation
PDF
Lecture 6 - Arrays
DOCX
Chapter 4
PDF
Lecture17 arrays.ppt
PPT
Chandan
Lecture 8
Arrays in CPP
Java arrays
Arrays Class presentation
Lecture 6 - Arrays
Chapter 4
Lecture17 arrays.ppt
Chandan

What's hot (20)

PDF
Week06
PPT
Associative Learning
PPT
arrays
PPT
Java Arrays
PPT
Array in Java
PDF
intorduction to Arrays in java
PDF
An Introduction to Programming in Java: Arrays
PPTX
Array,MULTI ARRAY, IN C
PPTX
PPT
Data Structure Midterm Lesson Arrays
PPTX
Array in C# 3.5
PPTX
Array in c#
PPTX
Array in c
PPT
Mesics lecture 8 arrays in 'c'
PPTX
Java arrays
PPTX
Array in C
PPT
PPT
Lec 25 - arrays-strings
Week06
Associative Learning
arrays
Java Arrays
Array in Java
intorduction to Arrays in java
An Introduction to Programming in Java: Arrays
Array,MULTI ARRAY, IN C
Data Structure Midterm Lesson Arrays
Array in C# 3.5
Array in c#
Array in c
Mesics lecture 8 arrays in 'c'
Java arrays
Array in C
Lec 25 - arrays-strings
Ad

Similar to Lecture06 computer applicationsie1_dratifshahzad (20)

PDF
Lecture05 computer applicationsie1_dratifshahzad
PDF
PPTX
07+08slide.pptx
PPT
PPT
SP-First-Lecture.ppt
PPT
C Arrays.ppt
PDF
Java - Arrays Concepts
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPT
Fp201 unit4
PPT
Csphtp1 07
PPTX
6_Array.pptx
PPTX
Array and its operation in C programming
DOCX
Class notes(week 4) on arrays and strings
PDF
Class notes(week 4) on arrays and strings
PPTX
Computer programming 2 Lesson 13
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPTX
Arrays basics
Lecture05 computer applicationsie1_dratifshahzad
07+08slide.pptx
SP-First-Lecture.ppt
C Arrays.ppt
Java - Arrays Concepts
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Fp201 unit4
Csphtp1 07
6_Array.pptx
Array and its operation in C programming
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Computer programming 2 Lesson 13
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arrays basics
Ad

More from Atif Shahzad (20)

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

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Cost to Outsource Software Development in 2025
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Oracle Fusion HCM Cloud Demo for Beginners
Complete Guide to Website Development in Malaysia for SMEs
Autodesk AutoCAD Crack Free Download 2025
Patient Appointment Booking in Odoo with online payment
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Download FL Studio Crack Latest version 2025 ?
Wondershare Filmora 15 Crack With Activation Key [2025
Monitoring Stack: Grafana, Loki & Promtail
Cost to Outsource Software Development in 2025
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Why Generative AI is the Future of Content, Code & Creativity?
Reimagine Home Health with the Power of Agentic AI​
Design an Analysis of Algorithms II-SECS-1021-03
Embracing Complexity in Serverless! GOTO Serverless Bengaluru

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