Data Structures- Array
Data Structures- Array
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
• 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
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
Answer: b
Explanation: Array contains elements only of the same
type.
Quiz
Answer: c
Explanation: This is the syntax to initialize an array in C.
Quiz
Answer: c
Explanation: The syntax to declare multidimensional array in
java is either int[][] arr; or int arr[][];
Quiz
Answer: d
Explanation: Arrays store elements of the same data type
and present in continuous memory locations.
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!