Array Data Structure
Array Data Structure
Topperworld.in
Arrays
An array is a linear data structure that collects elements of the same data
type and stores them in contiguous and adjacent memory locations.
➔Need of Array :
Let's suppose a class consists of ten students, and the class has to publish
their results. If you had declared all ten variables individually, it would be
challenging to manipulate and maintain the data.
©Topperworld
Data Structure and Algorithm
If more students were to join, it would become more difficult to declare all
the variables and keep track of it. To overcome this problem, arrays came
into the picture.
➔Types of Arrays :
• One-Dimensional Arrays
• Multi-Dimensional Arrays
➔ One-Dimensional Arrays:
You can imagine a 1d array as a row, where elements are stored one after
another.
➔Multi-Dimensional Arrays:
1. Two-Dimensional Arrays :
©Topperworld
Data Structure and Algorithm
You can imagine it like a table where each cell contains elements.
2. Three-Dimensional Arrays:
You can imagine it like a cuboid made up of smaller cuboids where each
cuboid can contain an element.
In this "arrays in data structures" tutorial, you will work around one-
dimensional arrays.
➔Declaration of Array
Arrays are typically defined with square brackets with the size of the arrays
as its argument.
©Topperworld
Data Structure and Algorithm
➔Initialization of an Array
• Method 1:
• Method 2:
• Method 3:
int n;
scanf(“%d”,&n);
int arr[n];
for(int i=0;i<5;i++)
{
scanf(“%d”,&arr[i]);
}
• Method 4:
int arr[4];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
©Topperworld
Data Structure and Algorithm
You can access elements with the help of the index at which you stored
them. Let's discuss it with a code:
#include<stdio.h>
int main()
{
int a[5] = {2, 3, 5, 7, 11};
printf(“%d\n”,a[0]); // we are accessing
printf(“%d\n”,a[1]);
printf(“%d\n”,a[2]);
printf(“%d\n”,a[3]);
printf(“%d”,a[4]);
return 0;
}
OUTPUT :-
©Topperworld
Data Structure and Algorithm
➔Advantages of Arrays
• Arrays store multiple elements of the same type with the same name.
➔Disadvantages of Arrays
©Topperworld
Data Structure and Algorithm
➔Traversal Operation
This operation traverses through all the elements of an array. We use loop
statements to carry this out.
Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[5];
System.out.println("The array elements are: ");
for(int i = 0; i < 5; i++) {
A[i] = i + 2;
System.out.println("A[" + i + "] = " + A[i]);
}
}
}
OUTPUT:-
➔Insertion Operation
In the insertion operation, we are adding one or more elements to the
array. Based on the requirement, a new element can be added at the
beginning, end, or any given index of array. This is done using input
statements of the programming languages.
Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[3];
System.out.println("Array Before Insertion:");
for(int i = 0; i < 3; i++)
System.out.println("A[" + i + "] = " + A[i]); //prints
empty array
System.out.println("Inserting Elements..");
©Topperworld
Data Structure and Algorithm
OUTPUT:-
➔Deletion Operation
In this array operation, we delete an element from the particular index of
an array. This deletion operation takes place as we assign the value in the
consequent index to the current index.
Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[3];
int n = A.length;
System.out.println("Array Before Deletion:");
for(int i = 0; i < n; i++) {
A[i] = i + 3;
System.out.println("A[" + i + "] = " + A[i]);
}
for(int i = 1; i<n-1; i++) {
A[i] = A[i+1];
n = n - 1;
}
System.out.println("Array After Deletion:");
for(int i = 0; i < n; i++) {
System.out.println("A[" + i + "] = " + A[i]);
}
}
}
©Topperworld
Data Structure and Algorithm
OUTPUT:-
➔Search Operation
Searching an element in the array using a key; The key element
sequentially compares every value in the array to check if the key is present
in the array or not.
Example:-
public class ArrayDemo{
public static void main(String []args){
int A[] = new int[5];
System.out.println("Array:");
for(int i = 0; i < 5; i++) {
A[i] = i + 3;
System.out.println("A[" + i + "] = " + A[i]);
}
for(int i = 0; i < 5; i++) {
if(A[i] == 6)
System.out.println("Element " + 6 + " is found at index " + i);
}
}
}
OUTPUT:-
➔Update Operation
Update operation refers to updating an existing element from the array at
a given index.
©Topperworld
Data Structure and Algorithm
Example:-
OUTPUT:-
©Topperworld
Data Structure and Algorithm
➔Display Operation
This operation displays all the elements in the entire array using a print
statement.
Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[5];
System.out.println("The array elements are: ");
for(int i = 0; i < 5; i++) {
A[i] = i + 2;
System.out.println("A[" + i + "] = " + A[i]);
}
}
}
OUTPUT:-
©Topperworld