Arrays
Arrays
Objectives
▸Single Dimension arrays
▸Multidimensional Arrays
- Two-Dimensional Arrays
- Jagged Array
Fundamentals of Programming
▸The Array class
▸Advanced topics
Arrays
1 2
Arrays
Introduction
▸An array is a group of like-typed variables that are referred to by a common
name.
- Each data item is called an element of the array.
- The data types of the elements may be any valid data type like char, int, float, etc.
- The elements are stored in a contiguous location.
- Length of the array specifies the number of elements present in the array.
- The allocation of memory for the arrays is done dynamically.
- Arrays are kinds of objects;
• Passing by reference
- The variables in the array are ordered and each has an index beginning from 0.
Single Dimension Arrays
▸Types of arrays:
- Single Dimension
- Multiple Dimension
3 4
1
16/09/2024
[email protected] 5 [email protected] 6
5 6
Access Item
Access Item
using iterator
[email protected] 7 [email protected] 8
7 8
2
16/09/2024
9 [email protected] 10
9 10
Steps
1. Each step compares the search key with the value of the middle searching the value of 37 in array
element of the array.
2. The keys matching in step 1 means a matching element has
been found, and its index (or position) is returned. Else step 3
or 4.
3. If the search key is less than the middle element, then the
algorithm repeats its action on the sub-array to the left of the
middle element or,
4. If the search key is greater than the middle element, then the
algorithm repeats its action on the sub-array to the right of the
middle element.
5. If the search key does not match any of the subsequent left or
right arrays, it means that the key is not present in the array,
and a special "Nil" indication can be returned.
Animation: https://yongdanielliang.github.io/animation/web/BinarySearchNew.html
[email protected] 11 [email protected] 12
11 12
3
16/09/2024
13 14
Algorithm
1. Find the smallest element in an array, replace it
with the first element.
2. Find the smallest element in the unsorted part of
an array, replace it with the second element.
3. Repeat this process until the end and array will
be sorted at the end.
[email protected] 15 [email protected] 16
15 16
4
16/09/2024
[email protected] 17 [email protected] 18
17 18
Multidimensional Arrays
[email protected] 19 20
19 20
5
16/09/2024
[email protected] 21 [email protected] 22
21 22
▸Initialization
[email protected] 23 [email protected] 24
23 24
6
16/09/2024
▸Looping through a 2-D Array Loop through all elements in 2-D arrays
[email protected] 25 [email protected] 26
25 26
[email protected] 27 [email protected] 28
27 28
7
16/09/2024
▸Example
[email protected] 29 [email protected] 30
29 30
[email protected] 31 [email protected] 32
31 32
8
16/09/2024
[email protected] 33 [email protected] 34
33 34
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-8.0
[email protected] 35 [email protected] 36
35 36
9
16/09/2024
37
10