0% found this document useful (0 votes)
78 views

CSC 220 Data Structures and Algorithms: Lecture # 3

The document outlines the topics to be covered in a lecture on arrays. It will cover an overview of arrays including what arrays are, array notation in C#, array rank, accessing array elements, and comparing arrays to collections. It will also cover creating arrays such as creating array instances, initializing array elements, initializing multidimensional array elements, creating computed size arrays, and copying array variables. Finally, it will discuss using arrays and include array properties, methods, returning arrays from methods, passing arrays as parameters, and using arrays with foreach loops.

Uploaded by

doha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

CSC 220 Data Structures and Algorithms: Lecture # 3

The document outlines the topics to be covered in a lecture on arrays. It will cover an overview of arrays including what arrays are, array notation in C#, array rank, accessing array elements, and comparing arrays to collections. It will also cover creating arrays such as creating array instances, initializing array elements, initializing multidimensional array elements, creating computed size arrays, and copying array variables. Finally, it will discuss using arrays and include array properties, methods, returning arrays from methods, passing arrays as parameters, and using arrays with foreach loops.

Uploaded by

doha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

1

CSC 220
Data Structures and Algorithms

Lecture # 3

Arrays

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Lecture Outlines 2

Lecture Outlines
• Overview of Arrays
 What is an Array?
 Array Notation in C#
 Array Rank
 Accessing Array Elements
 Checking Array Bounds
 Comparing Arrays to Collections

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Lecture Outlines 3

Lecture Outlines (cont.)


• Creating Arrays
 Creating Array Instances
 Initializing Array Elements
 Initializing Multidimensional Array Elements
 Creating a Computed Size Array
 Copying Array Variables

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Lecture Outlines 4

Lecture Outlines (cont.)


• Using Arrays
 Array Properties
 Array Methods
 Returning Arrays from Methods
 Passing Arrays as Parameters
 Command-Line Arguments
 Demonstration: Arguments for Main
 Using Arrays with foreach
 Quiz: Spot the Bugs

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Overview of Arrays 5

 Overview of Arrays
• What is an Array?
• Array Notation in C#
• Array Rank
• Accessing Array Elements
• Checking Array Bounds
• Comparing Arrays to Collections

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
What is an Array? 6

What is an
Array?
• An array is a sequence of homogeneous elements.
 All elements in an array have the same type.
 Individual elements are accessed using integer indexes.
• Structs can have elements of different types.

Integer index 0 Integer index 4


(arr[0]) (arr[4])

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Notation in C# 7

Array Notation in C#
• You declare an array variable by specifying:
 The element type of the array.
 The rank of the array.
 The name of the array variable.

type[ ] name;

the name of the array variable


the rank of the array

the element type of the array

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Rank 8

Array Rank
• Rank: the number of indexes associated with each element.
• Rank is also known as the array dimension.

long[ ] row; int[,] grid;


Rank 1: One-dimensional Rank 2: Two-dimensional
Single index associates with each Two indexes associate with each
long element int element

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Accessing Array Elements 9

Accessing Array
Elements
• Supply an integer index for each rank.
• Indexes are zero-based.

long[ ] row; int[,] grid;


... ...
row[3]; grid[1,2];

2
3
1

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Checking Array Bounds 10

Checking Array
Bounds
• All array access attempts are bounds checked.
 A bad index throws an IndexOutOfRangeException.

• Use the Length property and the GetLength method.

row grid

grid.GetLength(0)==2
row.GetLength(0)==6
grid.GetLength(1)==4
row.Length==6
grid.Length==2*4

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Comparing Arrays to Collections 11

Arrays drawbacks

.An array cannot resize itself when full •


.An array is intended to store elements of one type •
.Elements of an array cannot have read-only access •
.In general, arrays are faster but less flexible •

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Creating Arrays 12

 Creating Arrays
• Creating Array Instances
• Initializing Array Elements
• Initializing Multidimensional Array Elements
• Creating a Computed Size Array
• Copying Array Variables

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Creating Array Instances 13

Creating Array
Instances
• Declaring an array variable does not create an array.
• You must use new to explicitly create the array
instance.
• Array elements have an implicit default value of zero.
row
long[ ] row = new long[4]; 0 0 0 0
Variable Instance

int[,] grid = new int[2,3]; 0 0 0


grid 0 0 0

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Initializing Array Elements 14

Initializing Array
Elements
• The elements of an array can be explicitly initialized.
• You can use a convenient shorthand.

long[ ] row = new long[4] {0, 1, 2, 3};

long[ ] row = {0, 1, 2, 3}; Equivalent

1
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Initializing Multidimensional Array Elements 15

Initializing Multidimensional Array Elements

• You can also initialize multidimensional array elements.


• All elements must be specified.

int[,] grid = { Implicitly a new int[2,3]


{5, 4, 3}, array
{2, 1, 0} 5 4 3
};  grid 2 1 0
int[,] grid = {
{5, 4, 3},
{2, 1 } 
};

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Creating a Computed Size Array 16

Creating a Computed Size


Array
• The array size does not need to be a compile-time constant.
 Any valid integer expression will work.

 Accessing elements is equally fast in all cases.

 Array size specified by compile-time integer constant:


long[ ] row = new long[4];

 Array size specified by run-time integer value:


string s = Console.ReadLine(); int size =
int.Parse(s); long[ ] row = new long[size];

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Copying Array Variables 17

Copying Array
• Copying an Variables
array variable copies the array variable only.
 It does not copy the array instance.

 Two array variables can refer to the same array instance.

long[ ] row = new long[4]; 0 0 0 0


long[ ] copy = row; row
...
row[0]++;
copy
long value = copy[0];
Console.WriteLine(value); Variable Instance

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Using Arrays 18

 Using Arrays
• Array Properties
• Array Methods
• Returning Arrays from Methods
• Passing Arrays as Parameters
• Command-Line Arguments
• Demonstration: Arguments for Main
• Using Arrays with foreach
• Quiz: Spot the Bugs
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Properties 19

Array Properties

long[ ] row = new long[4];


row.Rank 1
0 row.Length 4

0
int[,] grid = new int[2,3];
0 grid.Rank 2
0 0 0 grid.Length 6
grid 0
row 0 0 0

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Array Methods 20

Array Methods

• Commonly used methods:


 Sort: sorts the elements in an array of rank 1.

 Clear: sets a range of elements to zero or null.

 Clone: creates a copy of the array.

 GetLength: returns the length of a given dimension.

 IndexOf: returns the index of the first occurrence of a value.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Returning Array from Methods 21

Returning Array from


Methods
• You can declare methods to return arrays:

class Example {
static void Main( ) {
int[ ] array = CreateArray(42);
...
}
static int[ ] CreateArray(int size) { int[ ] created = new
int[size]; return created;
}
}

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Passing Arrays as Parameters 22

Passing Arrays as
Parameters
• An array parameter is a copy of the array variable.
 Not a copy of the array instance.

class Example2 {
static void Main( ) {
int[ ] arg = {10, 9, 8, 7}; Method(arg);
System.Console.WriteLine(arg[0]);
}
static void Method(int[ ] parameter) { parameter[0]++;
}
} This method will modify
the original array
instance created in
Main
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Command-Line Arguments 23

Command-Line
Arguments
• The runtime passes command line arguments to Main.
 Main can take an array of strings as a parameter.

 The name of the program is not a member of the array.

class Example3 {
static void Main(string[ ] args) {
for (int i = 0; i < args.Length; i++) {
System.Console.WriteLine(args[i]);
}
}
}

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
Using Arrays with foreach 24

Using Arrays with


foreach
• The foreach statement abstracts away many details
of array handling.

class Example4 {
static void Main(string[ ] args) { foreach (string
arg in args) {
System.Console.WriteLine(arg);
}
}
}

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)
?How to delete an element from array
25

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99

To delete the 5’th cell 15


1 2 3 4 5 6 7 8 9 9

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99

Null
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 23 30 45 70 77 80 88 90 99 99
How to delete an element from array?
26

1. static void Main(string[] args)


2. { // where it’s location is known
3. int[] id = { 10, 12, 11, 45 };

4. // to delete the value of index 2


5. for (int i =2 ; i < id.Length-1; i++)
6. id[i] = id[i + 1];

7. for (int k = 0; k < id.Length; k++)


8. Console.WriteLine(" "+id[k]);

9. Console.Read();
10. }
How to delete an element from array?
27

1. static void Main(string[] args)


2. {
3. int[] id = { 10, 12, 11, 45 };
4. // to delete the value 12 where it’s location is unknown
5. int i;
6. for ( i = 0; i < id.Length; i++) {
7. if (id[i] == 12) {
8. break; // finding the location
9. }
10. }
11. for (int j =i ; j < id.Length-1; j++)
12. id[j] = id[j + 1]; // replacing process

13. for (int k = 0; k < id.Length; k++)


14. Console.WriteLine(" "+id[k]); // printing the array
15. Console.Read();
16. }
28
How to insert a new element in array?

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99

To insert value in order 13

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 13 15 23 30 45 70 77 80 88 90 99
How to insert a new element in array?
29

1. static void Main(string[] args)


2. {
3. int[] salary = new int [6];
4. salary[0] = 1200;
5. salary[1] = 2000;
6. salary[2] = 1000;
7. salary[3] = 3210;
8. salary[4] = 4310;
9. // to add value 3000 in the position 3
10. for (int i = salary.Length - 1; i > 3; i--)
11. salary[i] = salary[i - 1];
12. salary[3] = 3000;
13. Console.WriteLine(" === Values of Array after inserting === ");
14. for (int j = 0; j < salary.Length; j++)
15. Console.WriteLine(" Value: "+salary[j]);
16. Console.Read();
17. }
How to insert a new element in array?
1. static void Main(string[] args) {
2. int[] salary = new int[6];
30
3. salary[0] = 100;
4. salary[1] = 200;
5. salary[2] = 300;
6. salary[3] = 1210;
7. salary[4] = 2310;
8. // To add value 250
9. int i;
10. for (i = 0; i < salary.Length; i++)
11. if (salary[i] > 250)
12. break;
13. for (int j = salary.Length - 1; j > i; j--)
14. salary[j] = salary[j - 1];
15. salary[i] = 250;
16. Console.WriteLine(" ");
17. Console.WriteLine(" === Values of Array after inserting === ");
18. for (int j = 0; j < salary.Length; j++)
19. Console.WriteLine(" Value: " + salary[j]);
20. Console.Read(); }
Quiz: Spot the Bugs 25

Quiz: Spot the Bugs

int [ ] array;
1 array = {0, 2, 4, 6};

int [ ] array; System.Console.WriteLine(array[0]);


2

int [ ] array = new int[3];


3 System.Console.WriteLine(array[3]);

4 int [ ] array = new int[ ];

5 int [ ] array = new int[3]{0, 1, 2, 3};

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (3)

You might also like