0% found this document useful (0 votes)
16 views24 pages

Data Structures- Array

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
16 views24 pages

Data Structures- Array

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

DATA STRUCTURES- INTRODUCTION

TO ARRAY
Introduction
A data structure is a method of arranging and storing data in a computer that allows it to be used
efficiently. Array, LinkedList, Stack, Queue, Tree, Graph, and other data structures are all data
structures that store data in a specific form that allows us to access and use it efficiently. Each of
these data structures has a unique manner of organising data, thus we choose the data structure based
on our requirements. data structure allows faster data storing and retrieval.
Primary Types of Data Structures
• Primitive Data Structures
• Non-Primitive Data Structures
ARRAY
An array is a collection of variables where:
• Each variable in the collection is of the same data type
• The size of each variable is the same
• The variables are stored consecutively in memory
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items
of the same type together.
Properties of arrays:
• The array contains a fixed number elements
• Length of the array = number of elements in the array
• We can specify the length when you create the array
• Each array element is identified by an index
• The first element of an array has the index 0
• Because each variable is of the same size, if we know the location of the first element of
an array, we can locate any other element of the array through its index
• We define an array of double typed variables. Now, suppose the first element of this array
is located at address 1000. Because a double types variable uses 8 bytes of memory to
store the data, each "element" of the array is 8 bytes long.
Base Address:

Base Address of an array = the address of the first element of the array

Because we know the size of each array element, if we know the base address, we
can compute the address of any array element if we are given its array index.

• Therefore,
•Array element 0 is located at address 1000
•Array element 1 is located at address 1008
•Array element 2 is located at address 1016
•...
•Array element i is located at address 1000 + 8×I
The array definition:
double[] a;
a = new double[5];
It creates an array of 5 double typed variables

These 5 double typed variables have the following name:


a[0]
a[1]
a[2]
a[3]
a[4]
These 5 double typed variables are called array elements.

Each array element is a (simple) variable of the type double.


Basic Operations in Array
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.
• In C, when an array is initialized with size, then it assigns defaults values to its elements in following
order.

• Bool- false
• Char- 0
• Int- 0
• Float- 0.0
• Double- 0.0f
• Void-
• wchar_t- 0
Example of Traverse Operation

#include <iostream.h>
#include <stdio.h>
int main() {
int TA[] = {2,6,8,9,11};
int n = 5;
int i = 0, j = n;
printf("The array elements are :\n");
for(i = 0; i<n; i++) {
printf(”TA[%d] = %d \n", i, TA[i]);
}
}
When we compile and execute the above program, it produces the following result −

Output
The original array elements are :
TA[0] = 2
TA[1] = 6
TA[2] = 8
TA[3] = 9
TA[4] = 11
Example of Insertion Operation

Start

IF N = MAX, return
ELSE
N=N+1

For All Elements in A


Move to next adjacent location

A[FIRST] = New_Element

end
Deletion Operation
It is a process of deleting a particular element from an array. If an element to be deleted ith
location then all elements from the (i+1)th location we have to be shifted one step towards
left.
So (i+1)th element is copied to ith location and (i+2)th to (i+1)th location and so on.

Algorithm: In this algorithm a value is being deleted from ith location of an array Arr[N].
Let us assume that last element in the array is at Mth position.
Steps
1. Back=i
2. While (Back<M) repeat 3 and 4
3. Arr[Back]= Arr[Back+1]
4. Back= Back+1
5. M=M-1
6. End
Quiz

1. Which of these best describes an array?


a) A data structure that shows a hierarchical behavior
b) Container of objects of similar types
c) Arrays are immutable once initialised
d) Array is not a data structure
Quiz

Answer: b
Explanation: Array contains elements only of the same
type.
Quiz

How do you initialize an array in C?


a) int arr[3] = (1,2,3);
b) int arr(3) = {1,2,3};
c) int arr[3] = {1,2,3};
d) int arr(3) = (1,2,3);
Quiz

Answer: c
Explanation: This is the syntax to initialize an array in C.
Quiz

Which of the following is the correct way to declare a


multidimensional array in Java?
a) int[] arr;
b) int arr[[]];
c) int[][]arr;
d) int[[]] arr;
Quiz

Answer: c
Explanation: The syntax to declare multidimensional array in
java is either int[][] arr; or int arr[][];
Quiz

What are the advantages of arrays?


a) Objects of mixed data types can be stored
b) Elements in an array cannot be sorted
c) Index of first element of an array is 1
d) Easier to store elements of same data type
Quiz

Answer: d
Explanation: Arrays store elements of the same data type
and present in continuous memory locations.
Quiz

What are the disadvantages of arrays?


a) Data structure like queue or stack cannot be implemented
b) There are chances of wastage of memory space if elements
inserted in an array are lesser than the allocated size
c) Index value of an array can be negative
d) Elements are sequentially accessed
Quiz

Answer: b
Explanation: Arrays are of fixed size. If we insert elements
less than the allocated size, unoccupied positions can’t be
used again. Wastage will occur in memory.
THANK YOU!

You might also like