[go: up one dir, main page]

0% found this document useful (0 votes)
29 views3 pages

Dsa Fab Assign2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Renz Paul Fababeir 11/13/2022

BSIT 2-1 Mr. Ian Avena

HOMEWORK #2

1. What is Array?

 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.
o Element − Each item stored in an array is called an element.
o Index − Each location of an element in an array has a numerical index,
which is used to identify the element.

2. How to define or represent array?

 Arrays can be declared in various ways in different languages. For illustration,


let’s take C array declaration.

Array size
Elements
Array data type
Int array [10] = {10,12, 14, 16, 18, 20, 22, 24, 26, 28}
Array name

elements 10 12 14 16 18 20 22 24 26 28
index 0 1 2 3 4 5 6 7 8 9

 As per above illustration, following are the important points to be considered.


o Arrays are typically defined with square brackets with the size of the
arrays as its argument.
o Index starts with 0.
o Array length is 10 which means it can store 10 elements.
o Each element can be accessed via its index. For example, we can fetch
an element at index 6 as 9

3. What are the basic operations of array?

 Basic operations supported by an array are the following:


o Deletion – deletes an element at the given index,
o Insertion – adds an element at the given index,
o Search – searches an element using the given index or by the value,
o Traverse – print all the array elements one by one and,
o Update – updates an element at the given index.

Data Structures and Algorithm


Renz Paul Fababeir 11/13/2022
BSIT 2-1 Mr. Ian Avena

4. Discuss each array operations.

 Deletion refers to removing an existing element from the array and re-organizing
all elements of an array.
o For example, from the elements on question number 2, we will delete the
value 16
o Therefore, the output will be = 10, 12, 14, 18, 20, 22, 24, 26, 28.

 Insertion 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.
o Following the same elements, we can insert a value from any given index
in of array, for example we will insert a value 8 in the first index or the
beginning.
o Therefore, the output will be = 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28.
o Note that the first index is Zero (0).

 Search operation, you can perform a search for any array element based on its
value or its index.
o Following the same elements, we will be searching for a specific value,
and it will tell us at where index will we find it. For example, we search for
the value 22.
o Therefore, the output will be = The value 22 is at position 6.
o Whereas value is the element, and position is the index.

 Traverse operation, this operation is to traverse through the elements of an


array.
o Following the same elements, when we compile and execute a program
with the given elements, it will only display the value/element on its
index/position.
o Therefore, the output will be:
X [0] = 10
X [1] = 12
X [2] = 14 and so on until it reaches the final element…

 Update operation refers to updating an existing element from the array at a given
index.
o Following the same elements, when we update, we will input a new
element and replace it to a certain index. For example, we want to change
the element on the 9th index to 30.
o Therefore, the output will show all the unchanged elements, but on the 9 th
index it will be:
X [9] = 30

Data Structures and Algorithm


Renz Paul Fababeir 11/13/2022
BSIT 2-1 Mr. Ian Avena

 Note that upon using the operations, we must have a code first to compile and
execute, so that we can have the results or outputs as we use the operations.
Reference:

S, R. A. (2022, October 11). Arrays in data structure: A guide with examples [updated].
Simplilearn.com. from https://www.simplilearn.com/tutorials/data-structure-
tutorial/arrays-in-data-structure#:~:text=ScienceEnroll%20Now-,How%20Do
%20You%20Declare%20an%20Array%3F,Arrays%3A%20int%20arr%5Bn%5D
%3B

Data Structures and Algorithm

You might also like