Lecture 2 Arrays.pptx
Lecture 2 Arrays.pptx
and Algorithms
PREPARED BY
PROF. DR . AMANY SARHAN
2024
Types of Data Structures
What is the data
? structure
A data structure can be defined as a particular
scheme of organizing related data items.
The nature of the data items depends on the
problem at hand.
They can range from elementary data types
(e.g., integers or characters) to data structures
(e.g., a one-dimensional array of one-dimensional
arrays is often used for implementing matrices).
The Need for Data Structures
Goal: to organize data
Criteria: to facilitate efficient
1. storage of data
2. retrieval of data
3. manipulation of data
Design Issue:
◦ select and design appropriate data types
(This is the main motivation to learn and understand data structures)
◦ There are a few data structures that have proved to be particularly
important for computer algorithms.
Linear and Complex Data
Structures
The two most important elementary data
structures are the array and the linked list.
We will study also queue, stack, graph and
tree data structures which is higher in
complexity.
Types of Data Structures to study
Array
Linked List
Graph
Deletion
◦ Removing a data element from the structure
Sorting
◦ Arrange the data elements in a logical order
(ascending/descending)
Merging
◦ Combining data elements from two or more data
structures into one
Important functions on
any data structure
Build (x) => build the data structure
Get length => identify the no of items in DS
Get item => access specific item in DS
Set item => change the value of specific item
in DS
Iterate => navigate through the items in DS
Important functions on
any data structure
For example, if we have items x0, x1, …..,xn-1
Build (x) => create the data structure (array, linked list,…
etc)
Get length => return n (no of items)
Get item => return xi (i is the item index)
Set item => store a value at xi = w
Iterate => output x0, x1, ….., xn-1 in specific order
The most easiest way is to use arrays
Arrays
Arrays
It is a sequence of words placed in contiguous places in
the RAM, even it is multidimentional
An array starts at address x in RAM, for example.
To access the array in position i, this means we access
memory at position x+i.
Array[i] = array[memory address of array + i]
1 0 1 0
0 1 1 0
1 0 0 1
0 1 0 1
Contiguous Storage of
Array in RAM
If the first element of an array starts at memory address a
Each array element occupies b bytes,
Then, the second element starts at a + b , third starts at a
+ 2.b,….
In general, the i th element starts at a + i.b.
In an array that has N elements, it will be located from a
to a + N.b
Access time of Array
Elements
This means we can access each item at a constant time
We will call it O(1) (order of 1, means constant time)
Accessing time to array[h] = Accessing time to array[w]
This is called random accessing of array (as we know about the RAM)
Then get item and set item time is O(1) in arrays.
Building the array, however, is dependent on its size
Then Build (x) time is O(n) in arrays.
If you do not know the size of data, you tend to make large size of array
which you may use a part of it only. This is a waste of time and space!!!!!
Array Dimensions
Arrays are table-like structures. You can also think of them
as matrices in mathematics.
An array's dimension specifies how many indices an array
1 0 1 0
has. 1 0 1 0
0 1 1 0
1.2 1 0 1 0 1 00 11 10 0
1 00 111 100 0 0 1
2.1 0 1 1 0 1 0 0 1
0 11 10 01 0 1
6.5 1 0 0 1 00 10 01 1
1 00 10 01 1
17.0 0 1 0 1
0 1 0 1
1-dimensional 2-dimensional
array array 3-dimensional array
Two Dimensional Array
Representation
We typically represent a matrix or two-dimensional array
by one or more one dimensional arrays.
The two most common ways to store a matrix are:
◦ row-major and column-major order.
◦ Let’s consider an m X n matrix (m rows and n columns.
In row-major order, the matrix is stored row by row,
In column-major order, the matrix is stored column by
column.
Two Dimensional Array
Representation
For example, consider the 2 X 3 matrix
Eq. (10.1)
Row-major order stores the two rows 1 2 3 and 4 5 6,
Column-major order stores the three columns 1 4; 2 5;
and 3 6.
Figure 10.1 shows how to store this matrix using a single
one-dimensional array.
Basic usages of
arrays
long[
long[ ]] row
row == new
new long[4];
long[4]; row.Rank
row.Rank 11
0 0 0 0 row.Length
row.Length 44
row
int[,]
int[,] grid
grid == new
new int[2,3];
int[2,3];
grid.Rank
grid.Rank 22
0 0 0 grid.Length
grid.Length 66
grid 0 0 0
Methods and Properties for Retrieving Array Metadata
a.Length is 5.
Basic usages of arrays
0 a[1]
int a[5];
0 a[2]
a 0 a[3]
0 a[4]
Small practice
What is the output of the following program?
int a[3];
a[0] = 5; a[1] = 10; a[2] = 150;
Cout<<a[0]<< a[1]<< a[2]);
a[0] += 5;
a[1] = 20;
a[2] = a[0] + a[1];
Cout <<“The new values”<<a[0]<< a[1]<< a[2]);
5 10 150
10 20 30
Array one-dimensional
array
void Main() {
int sample[10];
int i;
for(i = 0; i < 10; i = i+1) {
sample[i] = i;
}
for(i = 0; i < 10; i = i+1) {
cout<<"sample[" << i<< "]: “<< sample[i];
}
}
}
Declare an integer array
Quiz of 5 elements where the values
of each element is equal to its
index multiplied by 3
(i.e. first element=0, the
second=3,.. etc)
Then print them
Compute the average of a
set of values
void Main() {
int nums[10]; int avg = 0;
Two-dimensional structure
33
Two-Dimensional Arrays
Declaration format
type identifier[integral value, integral value];
Two integral values are required for a two-dimensional
array
Number of rows is listed first
Data values placed in array must be of the same base
type
Example (create a 7x3 matrix)
◦ int calories [7, 3];
34
Two-Dimensional Arrays
(continued)
calories
reference
s address
of
calories[
0,0]
Names[2] = "Raymond";
Sales[19] = 23123;
myName = names[2];
Set the value of an array element
names.SetValue[2, "Raymond"];
sales.SetValue[19, 23123];
illustrates how to initialize
arrays
void Main() {
int intArray[5] = {10, 20, 30, 40, 50};
for (int counter = 0; counter < intArray.Length; counter++)
{
Console.WriteLine("intArray[" + counter + "] = " + intArray[counter]);
}
// char arrays
char[] charArray = new char[] {'h', 'e', 'l', 'l', 'o'};
for (int counter = 0; counter < charArray.Length; counter++)
{
Console.WriteLine("charArray[" + counter + "] = " + charArray[counter]);
}
// string arrays
string[] stringArray = {"Hello", "World"};
foreach (string myString in stringArray)
{
Console.WriteLine("myString = " + myString);
}
}
}
Comparing Strings
String Contains String
gets last array element
for-loops on array
72
Multidimensional Arrays
int[,] grades = new int[4,5];
declares a two-dimensional array,