Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Lesson 3. Arrays

Hhh

Uploaded by

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

Lesson 3. Arrays

Hhh

Uploaded by

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

IT 107 – Data Structure and Algorithms

Lesson 3 – Arrays

LEARNING
OBJECTIVES
After studying this lesson, you as a student should be able to:
a. Use array according to its dimensionality
b. Use array in storing data in memory
c. Apply the different classification of array accordingly
d. Compute for the memory addresses of a given array element.
e. Apply storing and accessing array elements using a procedural language

TOPIC
OUTLINE
1. Arrays in Data Structures
2. Classification of Arrays based on Element Size
3. One-Dimensional Array
4. Multi-Dimensional Array
5. Two-Dimensional Array
6. Three-Dimensional Array
7. Applying Arrays in Programs
8. Initializing Arrays
9. Declaring Values of Array element
10. Storing and Accessing array values using a procedural language.
11. Array Memory Addressing

OVERVIEW
A way to store an ordered collection of items is an array. Array items are typically stored in a sequence
of computer memory locations, but to discuss them, we need a convenient way to write them down on paper. We
can just write the items in order, separated by commas and enclosed by square brackets. Thus, [1; 4; 17; 3; 90;
79; 4; 6; 81]
is an example of an array of integers. If we call this array a, we can write it as:
a = [1; 4; 17; 3; 90; 79; 4; 6; 81]

This array a has 9 items, and hence we say that its size is 9. In everyday life, we usually start counting
from 1. When we work with arrays in computer science, however, we more often (though not always) start from 0.
Thus, for our array a, its positions are 0; 1; 2; : : : ; 7; 8. The element in the 8th position is 81, and we use the
notation a[8] to denote this element. More generally, for any integer i denoting a position, we write a[i] to denote
the element in the ith position. This position i is called an index (and the plural is indices). Then, in the above
example, a[0] = 1, a[1] = 4, a[2] = 17, and so on.

It is worth noting at this point that the symbol = is quite overloaded. In mathematics, it stands for equality.
In most modern programming languages, = denotes assignment, while equality is expressed by ==. We will
typically use = in its mathematical meaning, unless it is written as part of code or pseudocode.

We say that the individual items a[i] in the array a are accessed using their index i, and one can move
sequentially through the array by incrementing or decrementing that index, or jump straight to a particular item
given its index value. Algorithms that process data stored as arrays will typically need to visit systematically all the
items in the array, and apply appropriate operations on them.

Lesson 3 – Arrays | Page 1 of 11


ACTIVATING PRIOR
KNOWLEDGE
In your previous encounter with arrays, recall the different types of arrays and how they are initialized and
used in the program. Give an example of each type by showing the syntax on how initialize that particular type of
array.

ARRAYS IN
DATA STRUCTURES
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the
data structures make use of arrays to implement their algorithms. Following are the important terms to understand
the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used to identify the
element.

Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at index 6 as 19.

CLASSIFICATION
OF ARRAYS
One-dimensional Array:
is an array in which the components are
arranged in a list form and with a single index.
Syntax:
data_type array_name[size];

Individual value of grade[4]


grade[0]=75; grade[2]=90;
grade[1]=88; grade[3]=89;

Lesson 3 – Arrays | Page 2 of 11


Two-dimensional Array:
0 1 2 3
0
1
2
3
4

Declaring a Two-dimensional Array:

Declaring a Multi-dimensional Array:

One-dimensional Array:
To determine the ith element of a One-dimension array:
A [ i ] = α + ( I ) * esize
Where:
α – base or starting address
i - element
esize - element size in bytes

Lesson 3 – Arrays | Page 3 of 11


Example: Determine the address of the 4th element of an integer array A with a starting address of 2000. And
esize of 4 bytes.
Correct answer: A [4] = 2016

Example:
Given A [7], α=2000, esize=3 bytes:
a. Find the total number of elements? Answer: 7
b. Find the address of the 6th element. Answer: 2018
Solution: A[6] = α + i * esize
= 2000 + 6 * 3
= 2000 + 18
= 2018
c. Find the address of the 8th element. Answer: 2024
Solution: A[8] = α + i * esize
= 2000 + 8 * 3
= 2000 + 24
= 2024

Two-dimensional Array:
To determine the ith element of a Two-dimension array:

Example:
Given E[6][5], α=2000, esize=4 bytes:
a. Find the total no. of elements? Answer: 30
b. Find the address of E[4][3]? Answer: 2092
Solution:
E[4][3] = α + [ (i)*(UB2) + (j)] * esize
= 2000 + [ (4)*(5) + 3] * 4
= 2000 + (23)*4
= 2000 + 92
= 2092
c. Find the address of the last element? Answer: 2140
Solution:
E[6][5] = α + [ (i)*(UB2) + (j)] * esize
= 2000 + [ (6)*(5) + 5] * 4
= 2000 + (35)*4
= 2000 + 140
= 2140
Three-dimensional Array:

To determine the ith element of a Three-dimension array:

Lesson 3 – Arrays | Page 4 of 11


Given B[8][6][4], α=3000, esize=3 bytes:
a. Find the total no. of elements? Answer: 192
b. Find the address of B[5][4][2]? Answer: 3414
Solution:
B[5][4][2] = α + [ (i)*(UB2)* (UB3) + (j) * (UB3) + (k) ] * esize
= 3000 + [ (5)*(6)*(4) + (4)*(4) + (2) ] * (3)
= 3000 + (120 + 16 + 2) * (3)
= 3000 + (138) * (3)
= 3000 + 414
= 3414

c. Find the address of B[0][1][4]? Answer: 3052


Solution:
B[0][1][4]= α + [ (i)*(UB2)* (UB3) + (j) * (UB3) + (k) ] * esize
= 3000 + [ (0)*(6)*(4) + (1)*(4) + (4) ] * (3)
= 3000 + (16)*4
= 3000 + 52
= 3052

LEARNING
ACTIVITY 1
Exercises
Objective: Based on the given values and initializations, give what is being required of each statement.
d. Given A[10], α=2000, esize=4 bytes:
a. Find the number of elements.
b. Find the address of the 6th element.
c. Find the address of the 8th element.
e. Given E[3][4], α=2020, esize=4 bytes:
a. Find the total no. of elements.
b. Find the address of the last element.
c. Find the address of the 10th element.
Tools and resources: Pen or pencil and paper.

BASIC
OPERATIONS
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.

Traverse Operation
This operation is to traverse through the elements of an array.

Example:
Following program traverses and prints the elements of an array:

Lesson 3 – Arrays | Page 5 of 11


#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}

When we compile and execute the above program, it produces the following result −

Output:
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new
element can be added at the beginning, end, or any given index of array.

Here, we see a practical implementation of insertion operation, where we add data at the end of the array −

Example:
Following is the implementation of the above algorithm –
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

n = n + 1;

while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}

LA[k] = item;

printf("The array elements after insertion :\n");

Lesson 3 – Arrays | Page 6 of 11


for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}

When we compile and execute the above program, it produces the following result –

Output:
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

The array elements after insertion :


LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8

Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.

Algorithm:
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.

Following is the algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J<N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example:
Following is the implementation of the above algorithm –
#include <stdio.h>

void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

Lesson 3 – Arrays | Page 7 of 11


j = k;

while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}

n = n -1;

printf("The array elements after deletion :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}

When we compile and execute the above program, it produces the following result −

Output:
The original array elements are:
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

The array elements after deletion:


LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8

Search Operation
You can perform a search for an array element based on its value or its index.

Algorithm:
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.

Following is the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example:
Following is the implementation of the above algorithm –
#include <stdio.h>

void main() {
int LA[] = {1,3,5,7,8};

Lesson 3 – Arrays | Page 8 of 11


int item = 5, n = 5;
int i = 0, j = 0;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

while( j < n){


if( LA[j] == item ) {
break;
}

j = j + 1;
}

printf("Found element %d at position %d\n", item, j+1);


}

When we compile and execute the above program, it produces the following result −

Output:
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3

Update Operation
Update operation refers to updating an existing element from the array at a given index.

Algorithm:
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the
algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop

Example:
Following is the implementation of the above algorithm –
#include <stdio.h>

void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

Lesson 3 – Arrays | Page 9 of 11


LA[k-1] = item;

printf("The array elements after updation :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}

When we compile and execute the above program, it produces the following result −

Output:
The original array elements are:
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

The array elements after updation:


LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8

LEARNING
ACTIVITY 2
Array application in programming
Objective: Apply arrays in solving problems in programming.
Task: Create a program using arrays to display the required output below.
Output:
The original array elements are:
LA[0] = 3
LA[1] = 1
LA[2] = 2
LA[3] = 4
LA[4] = 9
The array elements after insertion:
LA[0] = 3
LA[1] = 1
LA[2] = 11
LA[3] = 2
LA[4] = 4
LA[5] = 9

Congratulations! You can now proceed to the next module about Linked List and Expression Parsing.

Lesson 3 – Arrays | Page 10 of 11


SUMMARY
Let us see if you can remember the main points raised in this lesson. Below is a summary of these
points:
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the
data structures make use of arrays to implement their algorithms.

Following are the important terms to understand the concept of Array.


• Element
• Index

Array Representation
The following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index.

Classification of Arrays
• One-dimensional Arrays
• Two-dimensional Arrays
• Multi-dimensional Arrays

Basic Operations
• Traverse Operation
• Insertion Operation
• Deletion Operation
• Search Operation
• Update Operation

REFERENCES
Data Structures and Algorithms, Bullinaria (2019)
Data Structures and Algorithm offline Tutorial, Onan Mobile Software, 2019
Data Structures and Algorithms in C++ (2nd Edition), Goodrich et al. (2011)
Data Structures and Algorithm, Tutorials Point Simply Easy Learning, www.tutorialspoint.com

OpenDSA Data Structures and Algorithms Modules Collection, https://opendsa-


server.cs.vt.edu/ODSA/Books/Everything/html
Data Structures and Algorithms (Level 1/C), Dr John A. Bullinaria, https://www.cs.bham.ac.uk/~jxb/dsa.html

Compiled by:

JASMIN C. DE GUZMAN, LPT, MIT


Faculty, College of Information and
Technology Education

Lesson 3 – Arrays | Page 11 of 11

You might also like